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

我正在尝试用 MPMoviePlayerController 替换 AVPlayer,因为我希望能够将具有透明背景的动画添加到视图中。问题是 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];

【问题讨论】:

    标签: ios video mpmovieplayercontroller avplayer avplayerlayer


    【解决方案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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-16
      • 2011-12-30
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多