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

相關文章
相關標籤/搜索