Tyl*_*ick 7 objective-c avfoundation ios avplayer audio-video-sync
问题:
在每次播放期间,音频都在视频后1-2秒.
设置:
资产从媒体流加载AVURLAssets.
为了编写合成,我使用了具有不对称时间尺度的AVMutableCompositions和AVMutableCompositionTracks.音频和视频都流式传输到设备.音频的时间刻度是44100; 视频的时间刻度是600.
使用AVPlayer完成播放.
尝试的解决方案:
videoAssetTrack.timeRange了[composition insertTimeRange].CMTimeRangeMake(kCMTimeZero, videoAssetTrack.duration);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];
}
Run Code Online (Sandbox Code Playgroud)
评论:
我对AVFoundation框架了解不多.完全有可能我只是滥用我提供的片段.(也就是为什么"insertTimeRange"用于作曲?)
我可以提供解析所需的任何其他信息 - 包括调试资产跟踪属性值,网络遥测,流信息等.
如果一致,则表明正确采样音频时存在强制延迟。苹果的指南通常比其随附的书籍更容易阅读,但这里有关于延迟的具体说明。
https://developer.apple.com/library/ios/technotes/tn2258/_index.html
编程指南将详细说明原因/内容。
| 归档时间: |
|
| 查看次数: |
2407 次 |
| 最近记录: |