Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用AVAssetExportSession从3gp视频文件导出音频_Iphone_Objective C_Ios_3gp_Avassetexportsession - Fatal编程技术网

Iphone 使用AVAssetExportSession从3gp视频文件导出音频

Iphone 使用AVAssetExportSession从3gp视频文件导出音频,iphone,objective-c,ios,3gp,avassetexportsession,Iphone,Objective C,Ios,3gp,Avassetexportsession,我正在尝试从3gpp视频文件导出音频,但它不工作。。。有人知道我可能做错了什么吗?以下是我正在使用的代码: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"newFile.m4a"]; NSSt

我正在尝试从3gpp视频文件导出音频,但它不工作。。。有人知道我可能做错了什么吗?以下是我正在使用的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"newFile.m4a"];
NSString *tempFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"oldFile.3gp"];

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^ {

    //HERE IS THE PROBLEM. THE ARRAY OF TRACKS IS EMPTY FOR SOME REASON.
    AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    AVMutableComposition* audioComposition = [AVMutableComposition composition];
    AVMutableCompositionTrack* audioCompositionTrack = [audioComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [audioCompositionTrack insertTimeRange:[audioTrack timeRange] ofTrack:audioTrack atTime:CMTimeMake(0, 1) error:nil];


    AVAssetExportSession *exprortSession = [AVAssetExportSession exportSessionWithAsset:audioComposition presetName:AVAssetExportPresetAppleM4A];
    NSURL *toFileURL = [NSURL URLWithString:filePath];
    exprortSession.outputURL = toFileURL;
    exprortSession.outputFileType = @"com.apple.m4a-audio";

    NSLog(@"exportAsynchronouslyWithCompletionHandler will start");

    [exprortSession exportAsynchronouslyWithCompletionHandler: ^(void) {

        if (exprortSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"Export success");
        }
        else {
            NSLog(@"Export failed");
        }
    }];
}];

尝试用

[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempFilePath] options:nil];
而不是

[AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempFilePath] options:nil];

由于您的任务是直接操作音频样本缓冲区,因此您应该使用AVFoundation提供的第二种变体:成对Avassetrader和AVAssetWriter设置。您将在Apple developer source中的AVReaderWriterOSX中找到合适的示例代码。除了有不同的I/O格式设置外,这也适用于iOS。应给出将音频解压缩为PCM并写回未压缩的.wav或音频文件的可用性

是。我不敢相信我看得太多了。也在输出文件中。谢谢