ios – 使用AVPlayer播放AVMutableComposition?

我想要按顺序播放两个视频.我尝试过AVQueuePlayer,但两个片段之间有一个巨大的“打嗝”.我需要他们不间断地玩.

所以我正在尝试使用AVMutableComposition和AVPlayer,但无法正确使用它.

这是我的代码(忽略内存泄漏,只是在空项目中测试..):

composition = [[AVMutableComposition alloc] init];

Nsstring * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
NSURL * url = [NSURL fileURLWithPath:path];
AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:url options:nil];

NSError * error = NULL;
[composition insertTimeRange:CMTimeRangeMake(CMTimeMake(0,1000),CMTimeMake(4,1000)) ofAsset:asset atTime:CMTimeMake(0,1000) error:&error];
if(error) NSLog(@"error: %@",error);

path = [[NSBundle mainBundle] pathForResource:@"chug1" ofType:@"mp4"];
url = [NSURL fileURLWithPath:path];
asset = [[AVURLAsset alloc] initWithURL:url options:nil];

error = NULL;
[composition insertTimeRange:CMTimeRangeMake(CMTimeMake(0,CMTimeMake(3,1000)) ofAsset:asset atTime:CMTimeMake(4.1,error);

AVPlayerItem * item = [[AVPlayerItem alloc] initWithAsset:composition];
AVPlayer * player = [AVPlayer playerWithPlayerItem:item];
AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer:player];

[layer setFrame:CGRectMake(0,320,480)];
[[[self view] layer] addSublayer:layer];
[player play];

代码似乎对我而言.每个视频的第一帧实际上呈现在屏幕上.但视频根本不播放.我错过了什么?我是否需要弄清楚如何使用MutableTrack?

解决方法

也许你使用错误的时间插入点和持续时间,两者都取决于实际的视频资产.我会写这样的东西:
CMTime insertionPoint = kCMTimeZero;
NSError * error = nil;
composition = [AVMutableComposition composition];
asset = /* obtain asset #1 */
if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.duration) 
                          ofAsset:asset 
                           atTime:insertionPoint 
                            error:&error]) 
{
    NSLog(@"error: %@",error);
}
insertionPoint = CMTimeAdd(insertionPoint,asset.duration);

asset = /* obtain asset #2 */
if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero,error);
}
...
/* playback stuff */

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...