【问题标题】:Play AVMutableComposition with AVPlayer?用 AVPlayer 播放 AVMutableComposition?
【发布时间】:2011-10-15 01:00:38
【问题描述】:

我正在尝试让两个视频按顺序播放。我试过 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 的东西吗?

【问题讨论】:

  • 嘿@gngwzrd 我也遇到了这个问题,但我没有太多经验,而且我是 iPhone 开发的新手。你能帮我解决这个问题吗?我想流畅地播放来自网络的多个视频,剪辑中没有任何间隙,然后我还需要正确地寻找它们。你能帮我解决这个问题吗?
  • 我最终没有使用视频进行无缝播放,最好的解决方案是使用带有单独音频文件的 PNG 序列。
  • 哦,好吧,寻找问题呢,你解决了吗,比如寻找正在流式传输的视频而不下载它?
  • 不,对于我们的项目,资产要么在设备上本地,要么在播放之前下载并保存在本地设备上。而且由于我们使用 PNG 序列,因此搜索不再是问题。在应用商店中结帐“Ozgood”。不确定这是否正是您要寻找的,但您可以挑选一个像“会说话的汤姆”这样的应用程序来看看它是如何完成的。在 iTunes 中将应用程序复制到您的桌面,显示包内容并开始四处窥探,您会看到他们是如何做到的。
  • 谢谢兄弟 :) ...所以现在没有解决方案可以在 iphone 应用程序中流畅地流式传输多个文件而没有间隙。

标签: ios avfoundation


【解决方案1】:

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

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 */

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多