用 MPMoviePlayerController 替换 AVPlayer

Posted

技术标签:

【中文标题】用 MPMoviePlayerController 替换 AVPlayer【英文标题】:Replacing AVPlayer with MPMoviePlayerController 【发布时间】:2013-09-18 12:35:46 【问题描述】:

我正在尝试,因为我希望能够将具有透明背景的动画添加到视图中。问题是 MPMoviePlayerController 没有显示动画。

请在下面找到我的台词。第一部分是使用 AVPlayer 并且它的工作。第二个是使用 MPMoviePlayerController 而不是。

代码的作用是播放动画,完成后启动动作。

带有 AVPlayer 的代码(有效):

NSString *filepath = [[NSBundle mainBundle] pathForResource:theAnimationFileName ofType:theString;

//NSURL *fileURL = [NSURL fileURLWithPath:filepath];
// First create an AVPlayerItem

AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:fileURL];

// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

// Pass the AVPlayerItem to a new player
controlledPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *animatedLayer = [AVPlayerLayer playerLayerWithPlayer:controlledPlayer];


[animatedLayer setFrame:CGRectMake(0, 0, 1024, 1024)];
[thisReplacementView.layer addSublayer: animatedLayer];


// Begin playback
[controlledPlayer play];

带有 MPMoviePlayerController 的代码(不显示任何内容):

NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:theString];

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];

moviePlayer.controlStyle = MPMovieControlModeDefault;

moviePlayer.view.frame = CGRectMake(0, 0, 1024, 1024);

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];


[thisReplacementView addSubview:moviePlayer.view];

[moviePlayer play];

【问题讨论】:

【参考方案1】:
NSURL *url = [NSURL URLWithString:@"http://ios.mp4"];
self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayer];

-(void)playbackFinishedCallback:(NSNotification *)notification

    self.moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];

- (void)moviePlaybackComplete:(NSNotification *)notification

    self.moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:self.moviePlayer];
    [self.moviePlayer.view removeFromSuperview];

【讨论】:

以上是关于用 MPMoviePlayerController 替换 AVPlayer的主要内容,如果未能解决你的问题,请参考以下文章

MPMoviePlayerController:搜索按钮点击冻结视频 - 黑屏

后台的 MPMoviePlayerController / AVAudioSession 在来电后不会重新开始播放

如何使用属性 Bool shouldautoplay MPMovieplayercontroller ios

如何使用 MPMoviePlayerController 播放均匀的背景模式

MPMoviePlayercontroller 在 iphone SDK 4 中不起作用? - 需要帮助

MPMoviePlayerViewController和MPMoviePlayerController的使用