MPMoviePlayerDemo

新建一个空的工程 创建两个类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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值