iOS 音乐的截取
最近接触一个音频的项目,之前接触过,不过都是用系统自带的去实现,没有仔细研究
在网上找到了一个demo,挺好用,反正效果是实现,非常不错。具体链接忘记了。
#pragma mark - 音视频剪辑,如果是视频把下面的类型换为AVFileTypeAppleM4V
// assetURL 本地路径地址
//startTime 开始时间
//endTime 结束时间
//outputPath 裁剪完成后的地址
+ (void)cutAudioVideoResourcePath:(NSURL *)assetURL startTime:(CGFloat)startTime endTime:(CGFloat)endTime complition:(void (^)(NSURL *outputPath,BOOL isSucceed)) completionHandle{
// 素材
AVAsset *asset = [AVAsset assetWithURL:assetURL];
// 导出素材
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetAppleM4A];
//剪辑(设置导出的时间段)
CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
CMTime duration = CMTimeMakeWithSeconds(endTime - startTime,asset.duration.timescale);
exporter.timeRange = CMTimeRangeMake(start, duration);
// 输出路径
NSURL *outputPath = [self exporterPath];
exporter.outputURL = [self exporterPath];
// 输出格式
exporter.outputFileType = AVFileTypeAppleM4A;
exporter.shouldOptimizeForNetworkUse= YES;
// 合成后的回调
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch ([exporter status]) {
case AVAssetExportSessionStatusFailed: {
NSLog(@"合成失败:%@",[[exporter error] description]);
completionHandle(outputPath,NO);
} break;
case AVAssetExportSessionStatusCancelled: {
completionHandle(outputPath,NO);
} break;
case AVAssetExportSessionStatusCompleted: {
completionHandle(outputPath,YES);
} break;
default: {
completionHandle(outputPath,NO);
} break;
}
}];
}
#pragma mark - 输出路径
+ (NSURL *)exporterPath {
NSInteger nowInter = (long)[[NSDate date] timeIntervalSince1970];
NSString *fileName = [NSString stringWithFormat:@"output%ld.mp4",(long)nowInter];
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *outputFilePath =[documentsDirectory stringByAppendingPathComponent:fileName];
if([[NSFileManager defaultManager]fileExistsAtPath:outputFilePath]){
[[NSFileManager defaultManager]removeItemAtPath:outputFilePath error:nil];
}
return [NSURL fileURLWithPath:outputFilePath];
}