关闭

ios学习--MPMoviePlayerController播放视频的方法

537人阅读 评论(0) 收藏 举报
分类:

MPMoviePlayerController是通过MediaPlayer.frame引入的,可用于播放在iOS支持的所有格式的视频,用起来很简单,但是有注意的事项,

 

代码如下:

-(IBAction)click:(id)sender{

//通过点击按钮出发视频播放视图的加载

[self playMyVedio];

}

-(void)playMyVedio{

//路径的设置,这里要注意,不要用[NSURL urlwithstring],还要去确保路径的正确

NSBundle *bundle = [NSBundle mainBundle];

NSString *moviePath = [bundle pathForResource:@"111/Viva" ofType:@"mp4"];

NSURL *movieURL = [NSURL fileURLWithPath:moviePath];

//很重要的一点是在头文件里已经把player变为属性了,@property(nonamaic,strong),如果不写为属性就会黑屏,目前不知道为什么

player =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];

player.controlStyle=MPMovieControlStyleDefault;

[player prepareToPlay];

[player.view setFrame:self.view.bounds];  // player的尺寸

[self.view addSubview: player.view];

player.shouldAutoplay=YES;

}

如果注意了视频的路径,和设置了属性,那么点击按钮就应该可以顺利的以全屏幕是播放视频,下一个问题就是退出,因为是全屏模式,模仿 iphone自己的视频播放,应该是点击左上角的done按钮就退出播放,这里如果不作处理,点击后只是退出全屏,屏幕还是有一块黑,其实是player 的非全屏模式,所以这里就要监听一个通知,点击done按钮后发出的通知,这样就可以退出了,代码如下:

- (void)viewDidLoad

{

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullScreen:) name: MPMoviePlayerDidExitFullscreenNotification object:nil];

}

-(void)exitFullScreen:(NSNotification *)notification{

[player.view removeFromSuperview];

NSLog(@”remove player”);

}

更新一种方法,程序启动自动播放全屏视频,没有控制条,播放完毕接视图呈现,也就是一个过场动画,这里要注意把设置控制条和全屏等语句写在添加播放器视图之后,要不然设置无效

-(void)playMyVedio{

NSString *myFilePath = [[NSBundle mainBundle] pathForResource:@”mnMovnew.mp4″ ofType:nil inDirectory:nil];

NSURL *movieURL = [NSURL fileURLWithPath:myFilePath];

player =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];

[player prepareToPlay];

[self.view addSubview:player.view];//设置写在添加之后

player.shouldAutoplay=YES;

[player setControlStyle:MPMovieControlStyleNone];

[player setFullscreen:YES];

[player.view setFrame:self.view.bounds];

}


MPMoviePlayerController播放视频扬声器没声音,插上耳机能听到声音,郁闷了好一阵子,最终找到了解决办法:

play 设置会话类型:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

 

.h文件引入头文件:

#import <AVFoundation/AVAudioSession.h>



-----------------------------------------------------------------------------

eg:

#import <MediaPlayer/MediaPlayer.h>

MediaPlayer.framework。


MPMoviePlayerViewController:

打开网络视频:

-(void)openmovie

{

    MPMoviePlayerViewController *movie = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLURLWithString:@"视频网络地址"]];

    

    [movie.moviePlayer prepareToPlay];

    [self presentMoviePlayerViewControllerAnimated:movie];

    [movie.moviePlayersetControlStyle:MPMovieControlStyleFullscreen];

    

    [movie.viewsetBackgroundColor:[UIColorclearColor]];

    

    [movie.view setFrame:self.view.bounds];

    [[NSNotificationCenterdefaultCenter]addObserver:self

     

                                             selector:@selector(movieFinishedCallback:)

     

                                                 name:MPMoviePlayerPlaybackDidFinishNotification

     

                                               object:movie.moviePlayer];


}

-(void)movieFinishedCallback:(NSNotification*)notify{

    

    // 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。

    

    MPMoviePlayerController* theMovie = [notifyobject];

    

    [[NSNotificationCenterdefaultCenter]removeObserver:self

     

                                                    name:MPMoviePlayerPlaybackDidFinishNotification

     

                                                  object:theMovie];

    

    [selfdismissMoviePlayerViewControllerAnimated];

    

}

打开本地视频:

-(void)openmovie

{

  

   NSString *url = [[NSBundlemainBundle]pathForResource:@"IMG_0322"ofType:@"mp4"];

    MPMoviePlayerViewController *playerViewController  = [[MPMoviePlayerViewControlleralloc]initWithContentURL:[NSURLfileURLWithPath:url]];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(movieFinishedCallback:)

                                                name:MPMoviePlayerPlaybackDidFinishNotification

                                              object:[playerViewControllermoviePlayer]];


    [self.view addSubview:playerViewController.view];

    


    MPMoviePlayerController *player = [playerViewControllermoviePlayer];

    [playerplay];

    

}

- (void) movieFinishedCallback:(NSNotification*) aNotification {

   MPMoviePlayerController *player = [aNotificationobject];

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:MPMoviePlayerPlaybackDidFinishNotificationobject:player];

    [playerstop];

    [player.viewremoveFromSuperview];

   

}


--------------------------------------------------------------------------------------------------------

转载:http://blog.sina.com.cn/s/blog_a7c44c8801017k40.html

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:177837次
    • 积分:2654
    • 等级:
    • 排名:第13802名
    • 原创:28篇
    • 转载:290篇
    • 译文:21篇
    • 评论:0条