MPMoviePlayerController切换电影会出现白色闪烁问题。

8

我有一个小的UIView,显示一个循环播放的电影。当用户点击一个按钮时,另一部电影将被加载并显示在同一个UIView中。

问题是,在删除第一个电影和显示第二个电影之间会有半秒钟的“闪烁”。有没有办法消除这个问题?

- (void) setUpMovie:(NSString*)title {
NSString *url = [[NSBundle mainBundle] pathForResource:title ofType:@"mp4"];

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[player view] setFrame:self.movieView.bounds];
[self.movieView addSubview:player.view];
if ([title isEqualToString:@"Bo_idle_02"]) {
    [player setRepeatMode:MPMovieRepeatModeOne];
} else {
    [player setRepeatMode:MPMovieRepeatModeNone];
}
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}

- (void) startDanceAnimation { [self setUpMovie:@"Bo_dance_02"]; return; }
4个回答

4

我成功使用先前建议的AVFoundation,使我的电影切换没有白色闪烁。以下是sudo代码,希望对某些人有所帮助:)

苹果的参考文档可以在此处找到,我使用它们来获取大部分信息: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188-CH1-SW3

我做的第一件事是将名为PlayerView的以下类添加到项目中(抱歉,我忘记了我从哪里得到它)。它是UIView的子类,是显示电影的视图。一旦将其添加到项目中,打开UI Builder,在现有的xib中添加一个新视图,并将其类更改为PlayerView。使用IBOutlet连接它。再次记住,这是将显示电影的视图。

PlayerView.h


#import 
#import 
#import 

@interface PlayerView : UIView {
    AVPlayer *player;
}

@property (nonatomic,retain) AVPlayer *player;

@end

PlayerView.m


#import "PlayerView.h"


@implementation PlayerView
@synthesize player;

+ (Class)layerClass {
    return [AVPlayerLayer class];
}
- (AVPlayer*)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}

- (void) dealloc {
    [super dealloc];
    [player release];
}

@end

在显示电影的ViewController中,我有以下代码:
DisplayMovies.h

#import 
#import 
@class PlayerView;

@interface DisplayMovies : UIViewController {
IBOutlet AVPlayer *player;
AVPlayerItem *movieOneItem;
AVPlayerItem *movieTwoItem;
}
@property (nonatomic, retain) AVPlayer *player;
@property (retain) AVPlayerItem *movieOneItem;
@property (retain) AVPlayerItem *movieTwoItem;

DisplayMovies.m


@implementation DisplayMovies
@synthesize player, movieOneItem, movieTwoItem;

- (void)viewDidLoad {
// load the two movies
NSURL *movieOneItemURL = [[NSBundle mainBundle] URLForResource:@"movieOne" withExtension:@"mp4"];
    AVURLAsset *movieOneItemAsset = [AVURLAsset URLAssetWithURL:movieOneItemURL options:nil];
    self.movieOneItem = [AVPlayerItem playerItemWithAsset:movieOneItemAsset];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieOneItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:self.movieOneItem];

NSURL *movieTwoItemURL = [[NSBundle mainBundle] URLForResource:@"movieTwo" withExtension:@"mp4"];
    AVURLAsset *movieTwoItemAsset = [AVURLAsset URLAssetWithURL:movieTwoItemURL options:nil];
    self.movieTwoItem = [AVPlayerItem playerItemWithAsset:movieTwoItemAsset];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieTwoItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:self.movieTwoItem];
    [self.player play];
}


- (void) movieOneItemDidReachEnd:(NSNotification*)notification {
// play movie two once movie one finishes
    [self.player seekToTime:kCMTimeZero];
    [self.player replaceCurrentItemWithPlayerItem:self.movieTwoItem];
    [self.player play];
}

- (void) movieTwoItemDidReachEnd:(NSNotification*)notification {
// play movie one once movie two finishes
    [self.player seekToTime:kCMTimeZero];
    [self.player replaceCurrentItemWithPlayerItem:self.movieOneItem];
    [self.player play];
}


嗨@SoundBlaster,我不同意。正如我所说的那样,上面是伪代码,但它确实有效,无论是在我的项目中还是其他人的项目中。如果您解释一下您的项目中正在发生/未发生的事情,也许我们可以提供帮助。 - bennythemink

2

上面发布的解决方案对我无效。在音轨之间仍然会出现短暂的闪烁。我通过创建两个视图,每个视图都有自己的AVPlayer来解决了这个问题。我让它们同时播放,然后通过隐藏顶部视图之间进行切换。这消除了闪烁,并且我能够通过将其alpha设置为0.0而不是使用setHidden来隐藏顶部视图,以实现两个音轨之间的过渡动画。以下是我的代码:

    AVPlayerItem *theAsset = [self generatePlaylistAssetWithVideoOne];  //In my app this returns an AVPlayer Item

    layerView1 = [[VideoLayerView alloc] initWithFrame:CGRectMake(-50, 0, 580, 320)];  //VideoLayerView is a subclass of UIView with the video layer stuff added in.

    [layerView1 setPlayer:thePlayer];

    [thePlayer play];

    [self.view addSubview:layerView1];



    AVPlayerItem *theAsset2 = [self generatePlaylistAssetWithVideoTwo];

    [thePlayer2 setActionAtItemEnd:AVPlayerActionAtItemEndNone];

    layerView2 = [[VideoLayerView alloc] initWithFrame:CGRectMake(-50, 0, 580, 320)];

    [layerView2 setPlayer:thePlayer2];

    [thePlayer2 play];

    [self.view addSubview:layerView2];

然后我使用以下方法在视频之间进行动画:

    if (water)  //Water is a BOOL set up earlier in the file
{



    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];

    ///DO STUFF
    [layerView2 setAlpha:0];


    [UIView commitAnimations];


    water = NO;
}
else
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];

    ///DO STUFF
    [layerView2 setAlpha:1.0];

    [UIView commitAnimations];

    water = YES;
}

要更改视频,您只需将隐藏的视图设置为开始播放所需内容,然后在闪存后进行切换即可。


1

我还需要回到这个项目,但我已经收到了更多的建议。希望两周后我能回来继续工作,届时我会告诉你们哪种解决方案对我有效(祈祷)。在此期间,以下是我的选择:

1. 没有闪光灯,您将无法使用MPMoviePlayerController切换电影。

作为替代方案,您可以使用AV Foundation。 AVPlayer-replaceCurrentItemWithPlayerItem:方法将从旧播放器项无缝过渡到新播放器项,而不会出现闪烁。

有关使用AV Foundation进行编程的更多信息,请参见“AV Foundation编程指南”,您可以在此处找到:

http://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/

2. 您可以将所有电影合并为一个(大)电影,然后使用MPMediaPlayback currentPlaybackTime / endPlaybackTime属性来“选择”和播放所需的电影(在大电影中)。如果使用此技术,还需要确保新电影开始的时间点有关键帧。

3. 或者,创建一个单独的MPMoviePlayerController对象,使用-setContentURL:设置新电影的URL,并利用backgroundView属性实现更无缝的过渡。 backgroundView会自动与view属性一起调整大小,但在预加载电影之前它将被隐藏。您可以在两个电影视图后面放置一个黑色视图,然后使backgroundView的backgroundColor = black,这应该使其看起来更无缝。

希望这可以帮助到您。


1
我正在使用 replaceCurrentItemWithPlayerItem,但仍然在设备上看到白色闪烁,虚拟机没有闪烁。(iOS 9) - shim

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接