MPMoviePlayerController 切换电影导致白闪

发布于 2024-10-09 12:57:11 字数 761 浏览 0 评论 0原文

我有一个小的 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; }

I have a small UIView that displays a repeated movie. When the user taps a button another movie is loaded and displayed in the same UIView.

The problem is that there is a half second "flash" between the removing of the first movie and the displaying of the second. Is there any to remove this?

- (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; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷不放纵 2024-10-16 12:57:11

正如之前所建议的,我成功地使用 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

在显示电影的 ViewContoller 中,我有以下内容:

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];
}

I managed to get my movies changing without the white flash using the AVFoundation as suggested previously. sudo code below, hope it helps someone :)

Apples reference docs can be found here and I used them to get most of my information:
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188-CH1-SW3

The first thing I did was to add the following class, called PlayerView (can't remember where I got it sorry) to my project. Its a subclass of UIView and its the view that the movie will be displayed in. Once you add it to your project open UI Builder, add a new view to an existing xib and change its class to PlayerView. Connect this using an IBOutlet. Again remember this is the view that will display the movie.

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

In the ViewContoller that displays the movies I have the following:

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];
}

埖埖迣鎅 2024-10-16 12:57:11

上面发布的解决方案对我不起作用。我仍然在曲目之间看到短暂的闪烁。我能够通过创建两个视图来解决这个问题,每个视图都有自己的 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;
}

要更改视频,您只需设置隐藏视图以开始播放您想要的内容,然后在闪烁后进行切换。

The solution posted above didn't work for me. I was still getting short flashes between tracks. I was able to solve the problem by creating two views each with its own AVPlayer. I set them both playing and then switch between them by hiding the top view. This got rid of the flash, and I was able to animate a transition between the two tracks by hiding the top view by setting its alpha to 0.0 instead of using setHidden. Here is my code:

    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];

Then to animate between the videos I used:

    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;
}

To change the videos, you just need to set the hidden view to start playing what you want and then do the switch after the flash.

梦巷 2024-10-16 12:57:11

我仍然需要回到这个项目,但我收到了更多建议。我将在两周内回来处理这个项目,所以我会让你们知道哪种解决方案对我有用(祈祷)。与此同时,这是我的选择:

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

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

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

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

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

希望这有帮助。

I still have to come back to this project but I have received some more advice. I will be back working on this project in hopefully two weeks so I will let ye know which solution worked (fingers crossed) for me. In the mean time here's my options:

1.
You won't be able to use MPMoviePlayerController to switch the movie without a flash.

As an alternative, you can use AV Foundation. The AVPlayer -replaceCurrentItemWithPlayerItem: method will seamlessly transition from the old player item to the new one without flashes.

For more information about programming with AV Foundation, see the "AV Foundation Programming Guide" which you can find here:

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

2.
You can merge all your movies into a single (large) movie, then use the MPMediaPlayback currentPlaybackTime / endPlaybackTime properties to "select" and play the desired movie (within the large movie). If you use this technique you'll also need to make sure there's a key frame at the point in time where the new movie starts.

3.
Alternately, create a single MPMoviePlayerController object, use the -setContentURL: to set the new movie URL, and take advantage of the backgroundView property to make a more seamless transition. The backgroundView is automatically sized with the view property, but it will be hidden before the movie is preloaded. You can put a black view behind both movie views, and then make the backgroundView's backgroundColor = black, which should make it appear more seamless.

Hope this helps.

抚笙 2024-10-16 12:57:11

在这里找到解决方案 http://joris.kluivers.nl /blog/2010/01/04/mpmovieplayercontroller-handle-with-care/ 从 iOS 6 你需要使用 [self.movi​​ePlayerprepareToPlay];并捕获 MPMoviePlayerReadyForDisplayDidChangeNotification 以使用 [self.movi​​ePlayer play];

Find solution here http://joris.kluivers.nl/blog/2010/01/04/mpmovieplayercontroller-handle-with-care/ from iOS 6 you need to use [self.moviePlayer prepareToPlay]; and catch MPMoviePlayerReadyForDisplayDidChangeNotification to use [self.moviePlayer play];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文