ios-MPMoviePlayerController和MPMoviePlayerViewController的使用

虽然这两个类都已经废弃了,但是这里还是简单的记录下它们的使用

首先其实这两个类,MPMoviePlayerViewController是自带界面的,我们使用它很简单

    //1、获取URL地址
    NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
    
    //2、创建带view的播放器
    MPMoviePlayerViewController * mpVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    
    //3、模态视图弹出
    [self presentViewController:mpVC animated:YES completion:nil];

如果我们使用的是MPMoviePlayerController使用步骤就如下所示,这里需要注意的是我们需要去把MPMoviewPlayerController定义成属性,这个我们自己去设置的意义其实就是我们可以通过这么做,做出我们可能想要的界面。

        //1、获取URL地址
        NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
    
        //2、创建不带view的控制器
        self.mpController = [[MPMoviePlayerController alloc]initWithContentURL:url];
    
        //3、设置view的frame
        self.mpController.view.frame = CGRectMake(0, 0, 300, 300);
    
        //4、去添加到view上
        [self.view addSubview:self.mpController.view];
    
        //5、准备播放
        [self.mpController prepareToPlay];
    
        //6、开始播放
        [self.mpController play];
    
        //7、控制模式
        self.mpController.controlStyle = MPMovieControlStyleFullscreen;
还有就是我们有的时候可能会需要根据视频点击的Done 按钮啊, 上一曲下一曲按钮来进行相应的操作的话,我们可以监听通知的方法去监听状态,然后进行相应的处理,我们其实点击MPMoviePlayerController这个类里面去看,里面有很多关于通知的名字,如下所示

ios-MPMoviePlayerController和MPMoviePlayerViewController的使用_第1张图片

我们这里用到的就两个,如下图所示

ios-MPMoviePlayerController和MPMoviePlayerViewController的使用_第2张图片

其实我们通过监听MPMoviePlayerPlaybackDidFinishNotification这个通知,调用的方法中的notification中的userInfo中的key其实就是MPMoviePlayerPlaybackDidFinishReasonUserInfoKey,也就是我们上面圈出来的第二个,然后我们可以看到后面有些这个NSNumber MPMovieFinishReason的其实应该就是这个key所对应的值的意思,我们可以进行搜索,然后就可以看到其实是个枚举,下面就是里面的枚举值。

ios-MPMoviePlayerController和MPMoviePlayerViewController的使用_第3张图片

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) 
  name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

-(void)moviePlayerPlaybackDidFinishNotification:(NSNotification *)notification
{
    
    /**
     MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
     MPMovieFinishReason的枚举值
     MPMovieFinishReasonPlaybackEnded,
     MPMovieFinishReasonPlaybackError,
     MPMovieFinishReasonUserExited
     */
    NSLog(@"%@",notification.userInfo);
    
    //1、获取通知结束的状态
    NSInteger finishReasonUserInfoKey = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]integerValue];
    
    //2、去根据不同的状态来进行写代码的逻辑
    switch (finishReasonUserInfoKey)
    {
        case MPMovieFinishReasonPlaybackEnded:
            //点击上一曲,下一曲的时候以及播放完毕的时候
            NSLog(@"播放结束");
            break;
        case MPMovieFinishReasonPlaybackError:
            NSLog(@"播放错误");
            break;
        case MPMovieFinishReasonUserExited:
            //点击done的时候
            NSLog(@"退出播放");
            break;
    }
}


你可能感兴趣的:(ios-开发)