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)彎路。