gpt4 book ai didi

iphone - MPMoviePlayerController切换电影导致白闪

转载 作者:可可西里 更新时间:2023-11-01 03:10:40 34 4
gpt4 key购买 nike

我有一个显示重复电影的小型 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; }

最佳答案

我按照之前的建议使用 AVFoundation 设法让我的电影在没有白色闪光的情况下发生变化。下面的sudo代码,希望它能帮助别人:)

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

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

播放器 View .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 中,我有以下内容:

显示电影.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];
}

关于iphone - MPMoviePlayerController切换电影导致白闪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4560065/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com