iOS 音樂的截取

最近接觸一個音頻的項目,以前接觸過,不過都是用系統自帶的去實現,沒有仔細研究ide

在網上找到了一個demo,挺好用,反正效果是實現,很是不錯。具體連接忘記了。spa

#pragma mark - 音視頻剪輯,若是是視頻把下面的類型換爲AVFileTypeAppleM4Vorm

// assetURL 本地路徑地址視頻

//startTime 開始時間ip

//endTime 結束時間rem

//outputPath 裁剪完成後的地址string

+ (void)cutAudioVideoResourcePath:(NSURL *)assetURL startTime:(CGFloat)startTime endTime:(CGFloat)endTime complition:(void (^)(NSURL *outputPath,BOOL isSucceed)) completionHandle{it

    //    素材io

    AVAsset *asset = [AVAsset assetWithURL:assetURL];ast


    //    導出素材

    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];

}

相關文章
相關標籤/搜索