關(guān)于IOS15后 推送系統(tǒng)語音播報(bào)無聲解決方案(親測(cè)可行)

iOS15之前用之前那套方案就可以了,
也可以在之前的那套方案進(jìn)行優(yōu)化,
1.優(yōu)化方案
這篇作為參考
https://developer.aliyun.com/article/855389
我只想說我這邊遇到的問題,和解決方案
首先需要申請(qǐng)groupsapp 方便本地合成播放
如果想要用本地通知播放需要申請(qǐng)Time Sensitive notification 很簡(jiǎn)單
我想說下合成這一塊代碼

-(void) complateFileTosound:(NSArray *)soudsArray{
  NSMutableArray *audioPathArray = [NSMutableArray new];
  AVMutableComposition *composition = [AVMutableComposition composition];
  NSMutableArray *rangesArray = [NSMutableArray new];
  NSMutableArray *audioAssetTrackArray = [NSMutableArray new];
  AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:0];
  //這一句很關(guān)鍵,它獲取的是push的路徑 所以我們需要截取
  NSString *bundlePath = [NSBundle mainBundle].bundlePath;
  NSArray *appPathArray = [bundlePath componentsSeparatedByString:@"PlugIns"];
  NSString *appPath = appPathArray[0];
  
  for (int i = 0; i< soudsArray.count; i++) {
      NSString *string = soudsArray[i];
      NSString *audioPath = [NSString stringWithFormat:@"%@%@.mp3",appPath,string];
      AVURLAsset *audioAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:audioPath]];
  //必須要保存 不然gg   有知道為什么大神可以 給我說一下 坑了很長(zhǎng)時(shí)間
    [audioPathArray addObject:audioAsset];
     AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];

     CMTimeRange range = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);

    [rangesArray addObject:[NSValue valueWithCMTimeRange:range]];
    [audioAssetTrackArray addObject:audioAssetTrack];

  }
  //多音頻合成方案
  [audioTrack insertTimeRanges:rangesArray ofTracks:audioAssetTrackArray atTime:kCMTimeZero error:nil];
  
  
  AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
      
      NSString *outPutFilePath = [[self.filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"report.m4a"];
      
      if ([[NSFileManager defaultManager] fileExistsAtPath:outPutFilePath]) {
          [[NSFileManager defaultManager] removeItemAtPath:outPutFilePath error:nil];
      }
      session.outputURL = [NSURL fileURLWithPath:self.filePath];
      session.outputFileType = AVFileTypeAppleM4A; //與上述的`present`相對(duì)應(yīng)
      session.shouldOptimizeForNetworkUse = YES;   //優(yōu)化網(wǎng)絡(luò)
      [session exportAsynchronouslyWithCompletionHandler:^{
          if (session.status == AVAssetExportSessionStatusCompleted) {
              NSLog(@"合并成功----%@", outPutFilePath);
             //有了這個(gè)你可以選擇本地 通知播報(bào)  或者直接 sound賦值
          } else {

          }
      }];
  
}

- (NSString *)filePath {
    if (!_filePath) {
      //獲取group路徑
      NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:kGroupDefaultSuiteName];
             NSURL * sounds = [groupURL URLByAppendingPathComponent:@"/Library/Sounds/" isDirectory:YES];
             if (![[NSFileManager defaultManager] contentsOfDirectoryAtPath:sounds.path error:nil]) {
                BOOL isCreateSuccess =  [[NSFileManager defaultManager] createDirectoryAtPath:sounds.path withIntermediateDirectories:YES attributes:nil error:nil];
                if (isCreateSuccess) _filePath = [sounds.path stringByAppendingPathComponent:@"report.m4a"];
               
             }else{
               _filePath = [sounds.path stringByAppendingPathComponent:@"report.m4a"];
             }
              
    }
  
    return _filePath;
}

東西不多 但是 要升級(jí)系統(tǒng),升級(jí)Xcode 升級(jí)手機(jī),花了2天時(shí)間,終于弄完了,記錄一下,也希望遇到同樣的小伙伴少走點(diǎn)彎路。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容