在AVPlayer中切换视频时更改时会创建Flash [英] Switching Videos in AVPlayer Creates Flash When Changing
问题描述
我正在使用AVFoundation的AVPlayer播放由1个较长视频制作的2个视频片段(因此第一个片段的结尾与第二个片段的开头相匹配)
I am using AVFoundation's AVPlayer to play 2 video clips made from 1 longer video (so the end of the first matches the beginning of the second)
当第一个视频结束,用户点击,我创建一个新的AVPlayer并将其分配给我的PlayerView,并开始播放第二个剪辑。
When the first video ends and the user taps, I create a new AVPlayer and assign it to my PlayerView, and start playing the second clip.
这一切都有效,但有一个突出屏幕闪烁。
This all works, however, there is a prominent screen "flicker".
我的假设是,这是由播放器视图删除第一个剪辑然后显示第二个剪辑引起的。
My assumption is that this is caused by the player view removing the first clip and then showing the second clip.
我需要的是这个闪烁没有出现,所以在两个剪辑之间进行无缝连接。
What I need is for this flicker to no appear, so that going between the two clips is seamless.
有人知道是否有办法通过AVPlayer *类来阻止这个flickr,或者通过某种东西来伪装它以使其不可见。
Do anyone know if there is a way to stop this flickr, either via the AVPlayer* classes, or a way to "fake" it by doing something to make it so this isn't visible.
谢谢
以下是我的加载和播放方法的代码:
Below is the code of my load and play method:
- (void)loadAssetFromFile
{
NSURL *fileURL = nil;
switch (playingClip)
{
case 1:
fileURL = [[NSBundle mainBundle] URLForResource:@"wh_3a" withExtension:@"mp4"];
break;
case 2:
fileURL = [[NSBundle mainBundle] URLForResource:@"wh_3b" withExtension:@"mp4"];
break;
case 3:
fileURL = [[NSBundle mainBundle] URLForResource:@"wh_3c" withExtension:@"mp4"];
break;
case 4:
fileURL = [[NSBundle mainBundle] URLForResource:@"wh_3d" withExtension:@"mp4"];
break;
default:
return;
break;
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
^{
// The completion block goes here.
NSError *error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded)
{
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
[playerView setPlayer:player];
[self.player seekToTime:kCMTimeZero];
[self play];
}
else {
// Deal with the error appropriately.
NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
}];
}
推荐答案
你不需要重新-create AVPlayer
执行此任务。您可以只有多个 AVPlayerItem
s,然后通过 [AVPlayer replaceCurrentItemWithPlayerItem:item]
切换哪一个是当前的。
You do not need to re-create AVPlayer
for this task. You can just have multiple AVPlayerItem
s and then switch which one is current via [AVPlayer replaceCurrentItemWithPlayerItem:item]
.
此外,您可以通过以下代码观察当前项目的更改时间。
Also, you can observe for when current item has changed with the code below.
static void* CurrentItemObservationContext = &CurrentItemObservationContext;
...
创建播放器后,注册观察者:
... After creating a player, register the observer:
[player1 addObserver:self
forKeyPath:kCurrentItemKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:CurrentItemObservationContext];
...
- (void)observeValueForKeyPath:(NSString*) path
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context {
if (context == CurrentItemObservationContext) {
AVPlayerItem *item = [change objectForKey:NSKeyValueChangeNewKey];
if (item != (id)[NSNull null]) {
[player1 play];
}
}
}
这篇关于在AVPlayer中切换视频时更改时会创建Flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!