ios-MPMoviePlayerController和MPMoviePlayerViewController的使用

本文介绍了如何在iOS应用中使用MPMoviePlayerViewController和MPMoviePlayerController来播放视频,并提供了详细的代码示例及如何监听播放状态变化的通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

首先其实这两个类,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这个类里面去看,里面有很多关于通知的名字,如下所示

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


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


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值