我正在使用 AVFoundation 的 AVPlayer 播放由 1 个较长视频制作的 2 个视频剪辑(因此第一个的结尾与第二个的开头相匹配)
当第一个视频结束并且用户点击时,我创建一个新的 AVPlayer 并将其分配给我的 PlayerView,然后开始播放第二个剪辑。
这一切正常,但是,有一个突出屏幕“闪烁”。
我的假设是,这是由播放器 View 移除第一个剪辑然后显示第二个剪辑引起的。
我需要的是不出现这种闪烁,以便两个剪辑之间的切换是无缝的。
有谁知道是否有办法通过 AVPlayer* 类来停止此 flickr,或者通过某事来“伪造”它以使其不可见.
谢谢
下面是我加载和播放方法的代码:
- (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]);
}
}];
}
最佳答案
您不需要为此任务重新创建 AVPlayer
。您可以只拥有多个 AVPlayerItem
,然后通过 [AVPlayer replaceCurrentItemWithPlayerItem:item]
切换哪个是当前的。
此外,您还可以使用以下代码观察当前项目何时更改。
static void* CurrentItemObservationContext = &CurrentItemObservationContext;
... 创建播放器后,注册观察者:
[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];
}
}
}
关于ios - 在 AVPlayer 中切换视频会在更改时创建 Flash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7238200/