//
// ViewController.m
// IOS9.0以前的视频播放
//
// Created by dc008 on 15/12/28.
// Copyright © 2015年 lin. All rights reserved.
//
#import "ViewController.h"
//引入头文件
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;// 视频播放控制器
@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