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=MPMovieControlStyleDefau
lt; [player prepareToPlay];
[player.view setFrame:self.view.bounds];
// player的尺寸 [self.view addSubview: player.view];
player.shouldAutoplay=YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitFullScreen:) name: MPMoviePlayerDidExitFull
screenNotification 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: AVAudioSessionCategoryPl
ayback 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