IOS:视频
2014-02-24 14:21
211 查看
iOS sdk中可以使用MPMoviePlayerController来播放电影文件。但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件。
• H.264 (Baseline Profile Level 3.0)
• MPEG-4 Part 2 video (Simple Profile)
幸运的是你可以先使用iTunes将文件转换成上面两个格式。
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。
代码实现:
1、创建单视图工程,导入MediaPlayer.framework,并导入要播放的视频文件
2、在viewController.h文件代码:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController :UIViewController
@property(nonatomic,retain)NSString *selectedName;
@property(nonatomic,retain)MPMoviePlayerController *moviePlayer;
@end
在m文件里,要包含一个头文件
#import "ZKChoiseMoveViewController.h",它是新建的一个类,用来展示视频列表
-(void)viewWillAppear:(BOOL)animated{
//注册通知,接收视频的名称
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getMovieName:)name:@"moviename"object:nil];
}
-(void)getMovieName:(NSNotification *)noti{
self.selectedName = [notiobject];
//获取视频的路径
NSString *moviePath = [[NSBundlemainBundle]pathForResource:self.selectedNameofType:@"mp4"];
NSURL *movieURL = [NSURLfileURLWithPath:moviePath];
//实例化视频播放器
self.moviePlayer = [[MPMoviePlayerControlleralloc]initWithContentURL:movieURL];
[self.moviePlayer.viewsetFrame:CGRectMake(10,150,
300, 280)];
[self.moviePlayerprepareToPlay];
[self.viewaddSubview:self.moviePlayer.view];
//释放观察值
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"moviename"object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.moviePlayerstop];
[self.moviePlayerrelease];
self.moviePlayer =nil;
}
- (void)viewDidLoad
{
[superviewDidLoad];
//播放
UIButton *play = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[playsetFrame:CGRectMake(40,60,
60, 30)];
[play setTitle:@"play"forState:UIControlStateNormal];
[play addTarget:selfaction:@selector(playButton:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:play];
//[play release];
//暂停
UIButton *pause = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[pausesetFrame:CGRectMake(220,60,
60, 30)];
[pause setTitle:@"pause"forState:UIControlStateNormal];
[pause addTarget:selfaction:@selector(pauseButton:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:pause];
//创建选择视频按钮
UIButton *next = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[next addTarget:selfaction:@selector(next:)forControlEvents:UIControlEventTouchUpInside];
[next setTitle:@"选择视频"
forState:UIControlStateNormal];
[nextsetFrame:CGRectMake(220,5,
80, 30)];
[self.viewaddSubview:next];
}
//play按钮关联的方法
-(void)playButton:(id)sender{
[self.moviePlayerplay];
}
//pause按钮关联的方法
-(void)pauseButton:(id)sender{
[self.moviePlayerpause];
}
//选择视频按钮关联的方法
-(void)next:(id)sender{
[self.moviePlayerstop];
ZKChoiseMoveViewController *choise = [[ZKChoiseMoveViewControlleralloc]init];
[selfpresentViewController:choise
animated:YEScompletion:nil];
[choiserelease];
}
3、在ZKChoiseMoveViewController.h里面:
@interface ZKChoiseMoveViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSArray *arr;
在m文件里面:
#import "ZKChoiseMoveViewController.h"
enum{
TABLE_TAG =1
};
@interface
ZKChoiseMoveViewController ()
@end
@implementation ZKChoiseMoveViewController
@synthesize arr;
-(void)dealloc{
[arrrelease];
[superdealloc];
}
- (void)viewDidLoad
{
[superviewDidLoad];
UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(10,10,
300,
200)style:UITableViewStylePlain];
tableView.delegate =self;
tableView.dataSource =self;
tableView.tag =TABLE_TAG;
[self.viewaddSubview:tableView];
self.arr = [NSArrayarrayWithObjects:@"xiatianweidao",@"yanshi",nil];
// Do any additional setup after loading the view.
}
#pragma mark tableviewdatasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrcount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *str =@"identifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];
//加视频标题
cell.textLabel.text = [self.arrobjectAtIndex:[indexPath
row]];
}
return cell;
}
#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"moviename"object:[self.arrobjectAtIndex:[indexPath
row]]];
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
还有一个MPMoviePlayerViewController类,用于全屏播放视频文件,用法和MPMoviePlayerController一样。
• H.264 (Baseline Profile Level 3.0)
• MPEG-4 Part 2 video (Simple Profile)
幸运的是你可以先使用iTunes将文件转换成上面两个格式。
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。
代码实现:
1、创建单视图工程,导入MediaPlayer.framework,并导入要播放的视频文件
2、在viewController.h文件代码:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController :UIViewController
@property(nonatomic,retain)NSString *selectedName;
@property(nonatomic,retain)MPMoviePlayerController *moviePlayer;
@end
在m文件里,要包含一个头文件
#import "ZKChoiseMoveViewController.h",它是新建的一个类,用来展示视频列表
-(void)viewWillAppear:(BOOL)animated{
//注册通知,接收视频的名称
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getMovieName:)name:@"moviename"object:nil];
}
-(void)getMovieName:(NSNotification *)noti{
self.selectedName = [notiobject];
//获取视频的路径
NSString *moviePath = [[NSBundlemainBundle]pathForResource:self.selectedNameofType:@"mp4"];
NSURL *movieURL = [NSURLfileURLWithPath:moviePath];
//实例化视频播放器
self.moviePlayer = [[MPMoviePlayerControlleralloc]initWithContentURL:movieURL];
[self.moviePlayer.viewsetFrame:CGRectMake(10,150,
300, 280)];
[self.moviePlayerprepareToPlay];
[self.viewaddSubview:self.moviePlayer.view];
//释放观察值
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"moviename"object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.moviePlayerstop];
[self.moviePlayerrelease];
self.moviePlayer =nil;
}
- (void)viewDidLoad
{
[superviewDidLoad];
//播放
UIButton *play = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[playsetFrame:CGRectMake(40,60,
60, 30)];
[play setTitle:@"play"forState:UIControlStateNormal];
[play addTarget:selfaction:@selector(playButton:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:play];
//[play release];
//暂停
UIButton *pause = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[pausesetFrame:CGRectMake(220,60,
60, 30)];
[pause setTitle:@"pause"forState:UIControlStateNormal];
[pause addTarget:selfaction:@selector(pauseButton:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:pause];
//创建选择视频按钮
UIButton *next = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[next addTarget:selfaction:@selector(next:)forControlEvents:UIControlEventTouchUpInside];
[next setTitle:@"选择视频"
forState:UIControlStateNormal];
[nextsetFrame:CGRectMake(220,5,
80, 30)];
[self.viewaddSubview:next];
}
//play按钮关联的方法
-(void)playButton:(id)sender{
[self.moviePlayerplay];
}
//pause按钮关联的方法
-(void)pauseButton:(id)sender{
[self.moviePlayerpause];
}
//选择视频按钮关联的方法
-(void)next:(id)sender{
[self.moviePlayerstop];
ZKChoiseMoveViewController *choise = [[ZKChoiseMoveViewControlleralloc]init];
[selfpresentViewController:choise
animated:YEScompletion:nil];
[choiserelease];
}
3、在ZKChoiseMoveViewController.h里面:
@interface ZKChoiseMoveViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSArray *arr;
在m文件里面:
#import "ZKChoiseMoveViewController.h"
enum{
TABLE_TAG =1
};
@interface
ZKChoiseMoveViewController ()
@end
@implementation ZKChoiseMoveViewController
@synthesize arr;
-(void)dealloc{
[arrrelease];
[superdealloc];
}
- (void)viewDidLoad
{
[superviewDidLoad];
UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(10,10,
300,
200)style:UITableViewStylePlain];
tableView.delegate =self;
tableView.dataSource =self;
tableView.tag =TABLE_TAG;
[self.viewaddSubview:tableView];
self.arr = [NSArrayarrayWithObjects:@"xiatianweidao",@"yanshi",nil];
// Do any additional setup after loading the view.
}
#pragma mark tableviewdatasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrcount];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *str =@"identifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];
//加视频标题
cell.textLabel.text = [self.arrobjectAtIndex:[indexPath
row]];
}
return cell;
}
#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"moviename"object:[self.arrobjectAtIndex:[indexPath
row]]];
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
还有一个MPMoviePlayerViewController类,用于全屏播放视频文件,用法和MPMoviePlayerController一样。
相关文章推荐
- iOS 如何通过 Windows 录制视频
- iOS 调用h5页面 视频不自动播放的问题
- iOS 录音和视频录制播放
- Android IOS WebRTC 音视频开发总结(五九)-- webrtc这蛋糕都怎么吃?
- ios学习--获取通过UIImagePackerController获取的系统相册 图片&视频 的名称信息
- 《IOS视频录制,本地视频播放,剪切,合并》系列(1)本地视频播放
- iOS视频播放代码
- iOS10-浅谈iOS视频播放的N种解决方案
- 程序猿之路-----学习MJ视频总结1---UIImageView和UIButton的区别 、iOS懒加载
- iOS开发进阶 - 用AVFoundation自定义视频录制功能
- iOS 视频播放方式整理
- iOS 视频开发学习
- iOS开发之ijkplayer视频直播
- Android IOS WebRTC 音视频开发总结(七二)-- 看到Google Duo,你想到了什么?
- ios之 本地音视频的获取
- js + php 读取、播放视频流 兼容firefox,chrome,ie,mac的safari,ios的safari,ios的微信浏览器(curl读取文件流篇)
- Android IOS WebRTC 音视频开发总结(六)-- iOS开发之含泪经验
- ios 视频,音频播放,录制操作
- app ios开发视频捕捉
- iOS文档、视频等文件导入教程