使用AVFoundation在视频之间快速切换 [英] Fast switching between videos using AVFoundation

查看:99
本文介绍了使用AVFoundation在视频之间快速切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,用户最多可以录制6个视频片段,每个片段的持续时间为2秒。当录制视频剪辑时,用户可以使用6个按钮进行播放 - 每个剪辑一个。然后,用户可以通过在6个剪辑之间切换来录制电影。问题是,当用户按下按钮时,我需要在6个剪辑之间进行近乎即时的切换 - 否则会丢失播放剪辑的错觉 - 该功能有点类似于App Store中名为CamBox的应用程序。

I'm writing an application where the user can record up to 6 video clips each with a duration of 2 seconds. When the video clips are recorded the user can play with them using 6 buttons - one for each clip. The user can then record a movie by switching between the 6 clips. The problem is that I need near instantaneous switching between the 6 clips when the user presses a button - otherwise the illusion of playing with the clips is lost - the functionality is somewhat similar to the app called CamBox in the App Store.

每当用户按下按钮时,我首先尝试使用AVPlayer中的AvPlayerItem和AVAsset初始化每个剪辑。播放器的输出指向我主视图中的AVPlayerLayer。问题是加载和开始播放所花费的时间很长,这意味着当用户快速连续按下按钮时视频会滞后。

I first tried initializing every clip with and AVAsset in an AvPlayerItem in an AVPlayer every time the user pressed a button. The output of the player was directed at a an AVPlayerLayer in my main view. The problem is that the time it takes to load and start playing is quite long, meaning the the video lags when the user presses the buttons in rapid succession.

我决定尝试使用5个AVPlayers和5个AVPlayerLayers预加载所有剪辑。 5 PlayerLayers被插入到我的主视图中,当用户按下按钮时,当前正在播放的AVPlayer被暂停并倒回,并且当前可见的AVPlayerLayer被隐藏。启动新的AVPlayer并显示相应的AVPlayerLayer。它工作得很好,比我的第一个解决方案要快得多,虽然不是瞬间但问题是我只能预加载4个剪辑,这意味着当用户按下播放最后两个按钮的按钮时它会滞后很长时间。下面是我预装片段的代码

I the decided to try to preload all the clips using 5 AVPlayers and 5 AVPlayerLayers. The 5 PlayerLayers are inserted into my main view and when the user presses a button the currently playing AVPlayer is paused and rewound and the the currently visible AVPlayerLayer is hidden. The new AVPlayer is started and the corresponding AVPlayerLayer is shown. It works pretty ok being much faster than my first solution although not instantaneous but the problem is that I can only preload 4 clips meaning than when the user presses the button that play the last two the it lags big time. Below is my code to preload the clips

-(void)loadVideos
{
  layers = [[NSMutableArray alloc] initWithCapacity:6];
  players = [[NSMutableArray alloc] initWithCapacity:6];

  for(int i = 1; i < 7; i++)
  {
      NSURL* fileURL = [NSURL fileURLWithPath:[self getFileName:i]];        
      AVPlayerItem* avPlayerItem = [[[AVPlayerItem alloc] initWithURL:fileURL] autorelease];
      [avPlayerItem addObserver:self forKeyPath:@"status" options:0 context:nil];   

      AVPlayer *avPlayer = [[[AVPlayer alloc] initWithPlayerItem:avPlayerItem] autorelease];

      [avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];   
      [avPlayer addObserver:self forKeyPath:@"currentItem" options:0 context:nil];   
      AVPlayerLayer* layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
      layer.frame = self.playerView.bounds;
      [playerView.layer addSublayer:layer];
      [layers addObject:layer];
      [players addObject:avPlayer];
      layer.hidden = YES;
  }    
}

6个按钮的事件处理程序如下所示:

The event handler for the 6 buttons looks like this:

- (IBAction)takeBtnClicked:(id)sender {
int tag = ((UIButton*)sender).tag;
AVPlayer* player;
AVPlayerLayer* layer;
if (layerIndex > -1) {
    player = [players objectAtIndex:layerIndex];
    layer = [layers objectAtIndex:layerIndex];
    [player pause];
    layer.hidden = YES;
    [player seekToTime:kCMTimeZero];
}
layerIndex = tag-1;
player = [players objectAtIndex:layerIndex];
layer = [layers objectAtIndex:layerIndex];
[player play];
layer.hidden = NO;    
}

我确定4个预装视频剪辑的限制是硬件限制,但替代方案是什么。有人有什么想法吗?
提前致谢。

I'm prette sure that the limitation of 4 preloaded video clips is a hardware limitation, but what is the alternative. Does anybody have any ideas? Thanks in advance.

推荐答案

请参阅我对 iphone-smooth-transition-from-one-video-to-another ,它显示了一个库你可以用来实现这个逻辑和一个带有3个按钮的示例应用程序启动动画剪辑。每个片段也有相关的音效。

See my answer for iphone-smooth-transition-from-one-video-to-another, it shows a library you can use to implement this logic and an example app with 3 buttons that kick off animated clips. Each clip also has an associated sound effect.

这篇关于使用AVFoundation在视频之间快速切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆