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;

}

posted @ 2016-07-19 10:56  SoulDu  阅读(3922)  评论(0编辑  收藏  举报