iOS音频裁剪

2019-02-11  本文已影响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);

                }

            }];

        }

    }];

}

上一篇下一篇

猜你喜欢

热点阅读