新建一个空的工程 创建两个类MediaPlayerViewController和ChoiceMediaViewController
导入框架MediaPlayer.framework
在代理中导入MediaPlayerViewController类 添加下列的代码
MediaPlayerViewController *mediaVC = [[MediaPlayerViewController alloc]init];
self.window.rootViewController = mediaVC;
[mediaVC release];
在MediaPlayerViewController.h文件中的代码
(导入<MediaPlayer/MediaPlayer.h>和ChoiceMediaViewController)
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MediaPlayerViewController : UIViewController
@property (copy,nonatomic) NSString *selectedName;
@property (retain,nonatomic) MPMoviePlayerController *moviePlayer;
@end
在MediaPlayerViewController.m文件中的代码
#import "MediaPlayerViewController.h"
#import "ChoiceMediaViewController.h"
@interface MediaPlayerViewController ()
@end
@implementation MediaPlayerViewController
- (void)dealloc
{
[_moviePlayer release];
[_selectedName release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(choiceMovieName:) name:@"moviename" object:nil];
}
- (void)choiceMovieName:(NSNotification *)not
{
self.selectedName = [not object];
//获取视频的路径
NSString *path = [[NSBundle mainBundle]pathForResource:self.selectedName ofType:@"mp4"];
NSURL *url = [[NSURL alloc]initFileURLWithPath:path];
//NSURL *url = [NSURL URLWithString:path];
//初始化视频播放器
self.moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[url release];
self.moviePlayer.view.frame = CGRectMake(10, 100, 300, 300);
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
//释放观察者
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"moviename" object:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//播放
UIButton *play = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[play setFrame:CGRectMake(40, 60, 60, 30)];
[play setTitle:@"play" forState:UIControlStateNormal];
[play addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:play];
//[play release];
//暂停
UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pause setFrame:CGRectMake(220, 60, 60, 30)];
[pause setTitle:@"pause" forState:UIControlStateNormal];
[pause addTarget:self action:@selector(pauseButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pause];
//[pause release];
UIButton *next = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[next addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
[next setTitle:@"选择视频" forState:UIControlStateNormal];
[next setFrame:CGRectMake(220, 5, 80, 30)];
[self.view addSubview:next];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.moviePlayer stop];
[self.moviePlayer release];
self.moviePlayer = nil;
}
- (void)playButton:(id)sender
{
[self.moviePlayer play];
}
- (void)pauseButton:(id)sender
{
[self.moviePlayer pause];
}
- (void)next:(id)sender
{
[self.moviePlayer stop];
ChoiceMediaViewController *choiceVC = [[ChoiceMediaViewController alloc]init];
[self presentViewController:choiceVC animated:YES completion:nil];
[choiceVC release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在ChoiceMediaViewController.h文件中添加
@interface ChoiceMediaViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (retain,nonatomic) NSArray *MovieNameArr;
@end
在ChoiceMediaViewController.m文件中的代码
#import "ChoiceMediaViewController.h"
@interface ChoiceMediaViewController ()
@end
@implementation ChoiceMediaViewController
- (void)dealloc
{
[_MovieNameArr release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.MovieNameArr = [NSArray arrayWithObjects:@"yanshi",@"xiatianweidao",nil];
UITableView *myTableVC = [[UITableView alloc]initWithFrame:self.view.frame];
myTableVC.delegate = self;
myTableVC.dataSource = self;
[self.view addSubview:myTableVC];
[myTableVC release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.MovieNameArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [self.MovieNameArr objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"moviename" object:[self.MovieNameArr objectAtIndex:[indexPath row]]];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end