gpt4 book ai didi

ios - 使用 AVPlayer 播放 AVMutableComposition?

转载 作者:可可西里 更新时间:2023-11-01 03:09:42 27 4
gpt4 key购买 nike

我正在尝试让两个视频按顺序播放。我试过 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,1000),CMTimeMake(3,1000)) ofAsset:asset atTime:CMTimeMake(4.1,1000) error:&error];
if(error) NSLog(@"error: %@",error);

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

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

代码对我来说似乎是正确的。每个视频的第一帧实际上被渲染到屏幕上。但是视频根本播放不了。我错过了什么?我需要弄清楚如何使用 MutableTrack 东西吗?

最佳答案

也许您使用了错误的时间插入点和持续时间,两者都取决于实际的视频 Assets 。我会这样写:

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, asset.duration)
ofAsset:asset
atTime:insertionPoint
error:&error])
{
NSLog(@"error: %@",error);
}
...
/* playback stuff */

关于ios - 使用 AVPlayer 播放 AVMutableComposition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7775040/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com