您的位置:首页 > 移动开发 > IOS开发

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一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: