MPMoviePlayerController切换电影会导致白色闪烁 [英] MPMoviePlayerController switching movies causes white flash

查看:125
本文介绍了MPMoviePlayerController切换电影会导致白色闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

我设法让我的电影在没有白色的情况下改变如前所述,使用AVFoundation进行闪光。下面的sudo代码,希望它可以帮助某人:)

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

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

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

我做的第一件事就是在我的项目中添加以下类,称为PlayerView(不记得我对不起)。它是UIView的子类及其将显示电影的视图。一旦将其添加到项目中,打开UI Builder,将新视图添加到现有xib并将其类更改为PlayerView。使用IBOutlet连接它。再次记住,这是将显示电影的视图。

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

PlayerView.h


#import 
#import 
#import 

@interface PlayerView : UIView {
    AVPlayer *player;
}

@property (nonatomic,retain) AVPlayer *player;

@end

PlayerView.m

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中显示电影我有以下内容:

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

DisplayMovies.h

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

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

这篇关于MPMoviePlayerController切换电影会导致白色闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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