IOS9.0 以前的视频播放 MPMoviePlayerController 视频播放控制器
2015-12-28 00:00
239 查看
摘要: 播放器视图->自适应屏幕宽高
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
在模拟器上调试 用command+左右键
//
// 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;// 视频播放控制器
@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
_moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
在模拟器上调试 用command+左右键
//
// 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;// 视频播放控制器
@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
相关文章推荐
- yum rpm命令
- 对于产品经理询问新产品有何建议的答复
- rpm包安装mysql的常见问题
- zabbix之rpm快速部署
- RPM
- jbpm5.3学习
- How to Use ipmitool to remote access SuperMicro Server
- deb,rpm格式
- 你是产品经理么?
- RPM软件包管理总结
- 每个产品经理都应该掌握这4个设计技能
- 【推荐系统算法】BPMF(Bayesian Probabilistic Matrix Factorization)
- 程序包管理-rpm,yum及程序编译安装
- IPMI不同
- IOS 多媒体加载 视频加载 MPMoviePlayerController
- MySQL学习笔记--安装配置(RPM)
- HDU5170PM2.5
- rpm包管理功能全解
- RPM常用组合【转载】
- PopupWindow与PopupMenu的用法