问题:
每次播放时,音频都会落后视频 1-2 秒。
设置:
这些资源是通过媒体流中的 AVURLAssets 加载的。
为了编写合成,我使用具有不对称时间尺度的 AVMutableCompositions 和 AVMutableCompositionTracks。音频和视频均传输至设备。音频的时间尺度是44100;视频的时间刻度为 600。
播放是用AVPlayer完成的。
尝试过的解决方案:
- Using
videoAssetTrack.timeRange
for [composition insertTimeRange]
.
- Using
CMTimeRangeMake(kCMTimeZero, videoAssetTrack.duration);
- Using
CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration);
代码:
+(AVMutableComposition*)overlayAudio:(AVURLAsset*)audioAsset
withVideo:(AVURLAsset*)videoAsset
{
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVAssetTrack* audioTrack = [self getTrackFromAsset:audioAsset withMediaType:AVMediaTypeAudio];
AVAssetTrack* videoTrack = [self getTrackFromAsset:videoAsset withMediaType:AVMediaTypeVideo];
CMTime duration = videoTrack.timeRange.duration;
AVMutableCompositionTrack* audioComposition = [self composeTrack:audioTrack withComposition:mixComposition andDuration:duration andMedia:AVMediaTypeAudio];
AVMutableCompositionTrack* videoComposition = [self composeTrack:videoTrack withComposition:mixComposition andDuration:duration andMedia:AVMediaTypeVideo];
[self makeAssertionAgainstAudio:audioComposition andVideo:videoComposition];
return mixComposition;
}
+(AVAssetTrack*)getTrackFromAsset:(AVURLAsset*)asset withMediaType:(NSString*)mediaType
{
return [[asset tracksWithMediaType:mediaType] objectAtIndex:0];
}
+(AVAssetExportSession*)configureExportSessionWithAsset:(AVMutableComposition*)composition toUrl:(NSURL*)url
{
AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
exportSession.outputFileType = @"com.apple.quicktime-movie";
exportSession.outputURL = url;
exportSession.shouldOptimizeForNetworkUse = YES;
return exportSession;
}
-(IBAction)playVideo
{
[avPlayer pause];
avPlayerItem = [AVPlayerItem playerItemWithAsset:mixComposition];
avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
[avPlayerLayer setFrame:CGRectMake(0, 0, 305, 283)];
[avPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[playerView.layer addSublayer:avPlayerLayer];
[avPlayer seekToTime:kCMTimeZero];
[avPlayer play];
}
评论:
我不太了解 AVFoundation 框架。我完全有可能只是滥用了我提供的片段。 (即为什么组合使用“insertTimeRange”?)
我可以提供解决问题所需的任何其他信息 - 包括调试资产跟踪属性值、网络遥测、流信息等。
如果一致,则表明正确采样音频时存在强制延迟。
苹果的指南通常比其随附的书籍更容易阅读,但这里有关于延迟的具体说明。
https://developer.apple.com/library/ios/technotes/tn2258/_index.html https://developer.apple.com/library/ios/technotes/tn2258/_index.html
编程指南将详细说明原因/内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)