IOS:视频
来源:互联网 发布:有关红酒的网络广告语 编辑:程序博客网 时间:2024/05/01 19:08
iOS sdk中可以使用MPMoviePlayerController来播放电影文件。但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件。
• H.264 (Baseline Profile Level 3.0)
• MPEG-4 Part 2 video (Simple Profile)
幸运的是你可以先使用iTunes将文件转换成上面两个格式。
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController :UIViewController
@property(nonatomic,retain)NSString *selectedName;
@property(nonatomic,retain)MPMoviePlayerController *moviePlayer;
@end
#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:choiseanimated:YEScompletion:nil];
[choiserelease];
}
@interface ZKChoiseMoveViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSArray *arr;
#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{
staticNSString *str =@"identifier";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];
//加视频标题
cell.textLabel.text = [self.arrobjectAtIndex:[indexPathrow]];
}
return cell;
}
#pragma mark tableviewdelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[NSNotificationCenterdefaultCenter]postNotificationName:@"moviename"object:[self.arrobjectAtIndex:[indexPathrow]]];
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
还有一个MPMoviePlayerViewController类,用于全屏播放视频文件,用法和MPMoviePlayerController一样。
- IOS:视频
- ios视频
- iOS-视频
- ios 视频聊天 视频通讯
- [iOS 项目视频] iOS视频大全-转
- iOS 5上手视频
- ios截取视频缩略图
- iOS视频播放代码
- IOS 视频播放实例
- ios 进阶 -- 开机视频
- IOS播放视频代码
- IOS 视频播放--
- IOS获取视频缩略图
- ios播放网络视频
- iOS视频播放代码
- iOS视频播放-MPMoviePlayerController
- iOS:视频播放
- ios 播放视频
- 重定向输出流实现程序日志
- 14款优秀的MySQL客户端
- LoadRunner 学习笔记 一
- 小马哥2013年口述精华:移动互联网是一场巨大的颠覆
- dddd
- IOS:视频
- Encoding.GetEncoding 编码列表 .
- FreeBSD使用英汉辞典
- [ACM]hdu Herding(枚举+三角形面积)
- 经典排序算法之——快速排序
- mySQL中删除unique key的语法 (删除某个字段的唯一性)
- linux术语解析(持续更新)
- js 函数的执行环境和作用域链的深入解析
- 自己动手写RTP服务器——用RTP协议传输TS流