iOS15之前用之前那套方案就可以了,
也可以在之前的那套方案进行优化,
1.优化方案
这篇作为参考
https://developer.aliyun.com/article/855389
我只想说我这边遇到的问题,和解决方案
首先需要申请groupsapp 方便本地合成播放
如果想要用本地通知播放需要申请Time Sensitive notification 很简单
我想说下合成这一块代码
-(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];
//这一句很关键,它获取的是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 有知道为什么大神可以 给我说一下 坑了很长时间
[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`相对应
session.shouldOptimizeForNetworkUse = YES; //优化网络
[session exportAsynchronouslyWithCompletionHandler:^{
if (session.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"合并成功----%@", outPutFilePath);
//有了这个你可以选择本地 通知播报 或者直接 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;
}
东西不多 但是 要升级系统,升级Xcode 升级手机,花了2天时间,终于弄完了,记录一下,也希望遇到同样的小伙伴少走点弯路。