IOS9.0 之前的视频播放 MPMoviePlayerController 视频播放控制器

//网络

//  ViewController.matom

//  IOS9.0之前的视频播放url

//spa

//  Created by dc008 on 15/12/28..net

//  Copyright © 2015 lin. All rights reserved.3d

//orm


#import "ViewController.h"视频

//引入头文件server

#import <MediaPlayer/MediaPlayer.h>rem



@interface ViewController ()


@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;// 视频播放控制器


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self.moviePlayer play];

    //添加通知

    [self addNotification];

}


- (void)addNotification{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    //播放状态改变的通知

    [notificationCenter addObserver:self selector:@selector(stateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_moviePlayer];

    //播放完成的通知

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

}


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

    

    NSLog(@"播放结束");

}


- (void)stateChange : (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;

    }

}

#pragma mark 建立视频播放控制器

- (MPMoviePlayerController *)moviePlayer{

    

    if (!_moviePlayer) {

        //1.获取视频地址(能够本地,也能够网络)

        NSString *urlStr =  [[NSBundle mainBundle]pathForResource:@"0" ofType:@"mp4"];

        NSURL *url = [NSURL fileURLWithPath:urlStr];

        //2.初始化播放控制器

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

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

        

        //播放器视图->自适应屏幕宽高

        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        [self.view addSubview:_moviePlayer.view];

    }

    return _moviePlayer;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark 移除

- (void)dealloc{

    //移除全部self里的通知监控

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}


@end