美文网首页
iOS音频裁剪

iOS音频裁剪

作者: 飞行的理想 | 来源:发表于2019-02-11 11:13 被阅读0次

/**

 裁剪音乐

 */

+ (void)dM_VideoManager_clipeMusicStartUrl:(NSURL*)videoUrl Time:(float)start endTime:(float)endTime completion:(void(^)(NSString*data))completionHandle{

    AVURLAsset *videoAsset = [AVURLAsset assetWithURL:videoUrl];

    NSArray *keys = @[@"duration",@"tracks"];

    [videoAssetloadValuesAsynchronouslyForKeys:keys completionHandler:^{

        NSError*error =nil;

        AVKeyValueStatusstatus = [videoAssetstatusOfValueForKey:@"tracks"error:&error];

        if(status ==AVKeyValueStatusLoaded) {//数据加载完成

            //音频输出会话

            AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:videoAsset presetName:AVAssetExportPresetAppleM4A];

            NSString *andomStr = [NSString stringWithFormat:@"myVideo%d.m4a",arc4random() % 1000000];

            NSURL*url = [DM_YCUtilsdm_getVideoUlrInFile:andomStr];

            exportSession.outputURL= url;

            exportSession.outputFileType = AVFileTypeAppleM4A;

            exportSession.timeRange = CMTimeRangeFromTimeToTime(CMTimeMake(start, 1), CMTimeMake(endTime, 1));

            [exportSessionexportAsynchronouslyWithCompletionHandler:^{

                if (exportSession.status == AVAssetExportSessionStatusCompleted) {

                    //                    NSLog(@"file:%@",myPathDocs);

                    completionHandle(andomStr);

                }else{

                    completionHandle(nil);

                    NSLog(@"失败了,原因是:%@",exportSession.error);

                }

            }];

        }

    }];

}

相关文章

网友评论

      本文标题:iOS音频裁剪

      本文链接:https://www.haomeiwen.com/subject/bttqeqtx.html