• iOS 播放本地,网络视频


    /**

     *  创建媒体播放控制器MPMoviePlayerControlle 可以控制尺寸

     *

     *  @return 媒体播放控制器

     */

    -(MPMoviePlayerController *)moviePlayer{

        if (!_moviePlayer) {

            NSURL *url=[self getFileUrl];

            _moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:url];

            _moviePlayer.view.frame=self.view.bounds;

            _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

            [self.view addSubview:_moviePlayer.view];

        }

        return _moviePlayer;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

     

            //播放

            [self.moviePlayer play];

       }

     

    #pragma mark - 私有方法

    //获取本地路径

    -(NSURL *)getFileUrl{

        NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"xxx.mp4" ofType:nil];

        NSURL *url=[NSURL fileURLWithPath:urlStr];

        return url;

    }

    /**

     *  取得网络文件路径

     *

     *  @return 文件路径

     */

    -(NSURL *)getNetworkUrl{

        NSString *urlStr=@"http://192.168.1.161/xxxx.mp4";

        urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL *url=[NSURL URLWithString:urlStr];

        return url;

    }

    /**

     *  添加通知监控媒体播放控制器状态

     */

    -(void)addNotification{

        NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];

        [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];

        [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];

        

    }

     

    /**

     *  播放状态改变,注意播放完成时的状态是暂停

     *

     *  @param notification 通知对象

     */

    -(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{

        switch (self.moviePlayer.playbackState) {

            case MPMoviePlaybackStatePlaying:

                NSLog(@"正在播放...");

                break;

            case MPMoviePlaybackStatePaused:

                NSLog(@"暂停播放.");

                break;

            case MPMoviePlaybackStateStopped:

                NSLog(@"停止播放.");

                break;

            default:

                NSLog(@"播放状态:%li",self.moviePlayer.playbackState);

                break;

        }

    }

     

    /**

     *  播放完成

     *

     *  @param notification 通知对象

     */

    -(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{

        NSLog(@"播放完成.%li",self.moviePlayer.playbackState);

    }

     

     

    //使用 MPMoviePlayerViewController,只能全屏

    /**

     *  视频播放控制器全屏

     */

    @property (nonatomic,strong) MPMoviePlayerViewController *moviePlayerViewController;

     

    -(MPMoviePlayerViewController *)moviePlayerViewController{

        if (!_moviePlayerViewController) {

            NSURL *url=self.videoUrl;

            _moviePlayerViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];

            [self addNotification1];

        }

        return _moviePlayerViewController;

    }

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        //播放

          [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];

    }

     

    -(void)addNotification1{

        NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];

        [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange1:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayerViewController.moviePlayer];

        [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished1:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerViewController.moviePlayer];

        

    }

     

    /**

     *  播放状态改变,注意播放完成时的状态是暂停

     *

     *  @param notification 通知对象

     */

    -(void)mediaPlayerPlaybackStateChange1:(NSNotification *)notification{

        switch (self.moviePlayerViewController.moviePlayer.playbackState) {

            case MPMoviePlaybackStatePlaying:

                NSLog(@"正在播放...");

                break;

            case MPMoviePlaybackStatePaused:

                NSLog(@"暂停播放.");

                break;

            case MPMoviePlaybackStateStopped:

                NSLog(@"停止播放.");

                 self.moviePlayerViewController =nil;

                break;

            default:

                NSLog(@"播放状态:%li",self.moviePlayerViewController.moviePlayer.playbackState);

                break;

        }

    }

     

    /**

     *  播放完成

     *

     *  @param notification 通知对象

     */

    -(void)mediaPlayerPlaybackFinished1:(NSNotification *)notification{

        NSLog(@"播放完成.%li",self.moviePlayerViewController.moviePlayer.playbackState);

        self.moviePlayerViewController =nil;

    }

  • 相关阅读:
    mysql锁 实战测试代码
    Memcache教程 Memcache零基础教程
    Moneybookers API支付方式开发 步骤
    dede判断当前文章
    Windows下的Memcache安装 linux下的Memcache安装
    Ecshop:后台添加新功能栏目以及管理权限设置
    解决echsop兼容jquery(transport.js的冲突)的问题
    PHP AJAX JSONP实现跨域请求使用实例
    shell基础 -- 基本语法
    神器之strace
  • 原文地址:https://www.cnblogs.com/soulDn/p/5683907.html
Copyright © 2020-2023  润新知