使用 AVAssetExportSession 导出音频和视频,但 iphone4 4.2.1 中音频未播放
我正在导出audio.caf 音频文件和video.mp4 视频并创建一个新的output.mp4 合成。 它在 iOS 5.0 上运行良好,但在 iPhone4 4.2.1 上,该组合无法播放音频。
NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = outputURL;
_assetExport.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0.25, 600), videoAsset.duration);
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:^{
我检查了这款 iPhone 中的原始曲目,都没有问题,只是合成没有添加音频。
这就是我将音轨添加到合成中的方法:
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(CMTimeMake(startRecording_,1),
audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error];
它在许多 iOS5 设备中都有效:S
提前致谢!
I'm exporting audio.caf audio file with video.mp4 video and creating a new output.mp4 composition.
It works fine with iOS 5.0, but with an iPhone4 4.2.1, the composition doesn't play the audio.
NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = outputURL;
_assetExport.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(0.25, 600), videoAsset.duration);
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:^{
I've checked the original tracks in this iphone and both are ok, only the composition doesn't add the audio.
This is how I'm adding the audio track to the composition:
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioURL options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(CMTimeMake(startRecording_,1),
audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:&error];
It worked in many iOS5 devices :S
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将音频更改为 .m4a 音频,现在可以使用了!
I changed audio to .m4a audio and it's working now!
我发现我需要将文件扩展名更改为“mov”,然后进行导出(我使用 PassThrough 预设),然后将其更改回 mp3(在我的情况下)。看起来像一个错误......
I found that I needed to change the file extension to 'mov' then do my export (im using PassThrough preset), then change it back to mp3 (in my case). Seems like a bug....