objective-c - Cocoa:从文件加载的 AVAsset 有 0 个轨道

标签 objective-c macos cocoa audio avfoundation

我正在尝试使用显示的技术连接一些音频文件 here .我的音频文件是 .m4a,我可以验证它们在 Quicktime 中播放得很好。这是我试图用来连接它们的代码:

 [currFile.audioContent writeToFile:tempOldFilePath atomically:NO];

    AVURLAsset *oldAudioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempOldFilePath] options:nil];
    AVURLAsset *newAudioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:tempInputFilePath] options:nil];

    NSLog(@"oldAsset num tracks = %lu",(unsigned long)oldAudioAsset.tracks.count);
    NSLog(@"newAsset num tracks = %lu",(unsigned long)newAudioAsset.tracks.count);
    AVAssetTrack *oldTrack = [[oldAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
    AVAssetTrack *newTrack = [[newAudioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    AVMutableComposition *mutableComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    NSError *error=nil;
    [compTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, oldTrack.timeRange.duration) ofTrack:oldTrack atTime:kCMTimeZero error:&error];
    if (error) {
        NSLog(@"%@",error.localizedDescription);
        error=nil;
    }
    [compTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, newTrack.timeRange.duration) ofTrack:newTrack
                        atTime:oldTrack.timeRange.duration error:&error];

    if (error) {
        NSLog(@"%@",error.localizedDescription);
        error=nil;
    }

    exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetAppleM4A];


    exporter.outputURL = [NSURL URLWithString:tempCompFilePath];

    exporter.outputFileType = AVFileTypeAppleM4A;

    [exporter exportAsynchronouslyWithCompletionHandler:^{
        NSLog(@"handller");
        NSError *error=nil;
        NSData *newData = [NSData dataWithContentsOfFile:tempCompFilePath options:0 error:&error];
        NSLog(@"%lu",(unsigned long)newData.length);
        if (error) {
            NSLog(@"%@",error.localizedDescription);
        }

        currFile.audioContent = newData;
        [[AppDelegate sharedDelegate] saveAction:nil];

    }];

我注意到的第一个问题是从未调用导出器的处理程序方法。我猜这是我注意到的另一个问题的原因:从 URL 创建我的 AVAssets 后,日志语句显示它们包含 0 个轨道。 Apple 的示例并未准确显示 AVAssets 的加载方式。

关于如何让它工作有什么建议吗?

最佳答案

正如您已经发现的那样,您需要使用 fileURLWithPath: 而不是 URLWithString: 来创建您的 URL。

URLWithString: 需要一个描述 URL 的字符串,例如 @"file:///path/to/file"@"http://example.com/"。当您的字符串单独描述一个路径时,例如 @"/path/to/file",您必须使用 fileURLWithPath:,它将正确填充缺失的部分。

从技术上讲,URLWithString: 会将路径解释为只有路径的 URL,但没有特定的方案,您可以继续使用相对于基础的方案任何面向文件的方案中的 URL,例如 HTTP (GET/path/to/file)。 fileURLWithPath: 会将路径解释为本地文件路径,并相应地返回一个 file: URL。

关于objective-c - Cocoa:从文件加载的 AVAsset 有 0 个轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463250/

相关文章:

iphone - 如何在不将值作为子项添加到场景的情况下更新值

linux - Mac OSx PKG 预安装脚本问题

macos - 使用 Swift 使用 MASShortcut 进行简单回调

macos - 事件不在 cocoa 应用程序中调用

ios - 如何切换 ViewController?

objective-c - 循环提示是或否?

objective-c - 检查 Objective-C 中的相等性

macos - 如何检测弹出窗口是否已完成?

macos - 如何在Mac应用程序中编辑Main Menu菜单栏?

c# - 用 C 编写的带有 Cocoa 前端的 OpenGL 游戏我想移植到 Windows