zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    WordPress Uploader插件‘blog’参数跨站脚本漏洞
    WordPress Counter Per Day插件拒绝服务漏洞
    OpenSSH 远程拒绝服务漏洞
    WordPress Count Per Day插件 ‘daytoshow’参数跨站脚本漏洞
    昨日关注-每日编译(DailyBuild) 参考
    产品这么卖很好玩
    知己知彼
    让一切有调理
    昨日关注SqlServer中区分大小写
    软件公司到底有几种
  • 原文地址:https://www.cnblogs.com/soulDn/p/5683907.html
Copyright © 2011-2022 走看看