播放视频:
需要添加MediaPlayer.framework框架:
需要:#import <MediaPlayer/MediaPlayer.h>
.h文件里面首先声明全局变量:
@property (retain, nonatomic) MPMoviePlayerViewController *movie;
@property (retain, nonatomic) UIView *aview;
.m文件代码如下:
//viewDidLoad里添加如下代码:
//加载视频
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 400, 50, 44);
[button setTitle:@"ok" forState:UIControlStateNormal];
[button addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//播放视频
- (void)playMovie
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"cyborg" ofType:@"m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
self.movie = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
self.movie.view.frame = CGRectMake(60, 60, 200, 200);
//self.movie.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;//嵌入式
self.movie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:self.movie];
self.aview = [[UIView alloc] initWithFrame:CGRectMake(60, 120, 200, 200)];
[self.aview addSubview:self.movie.view];
[self.view addSubview:self.aview];
//添加播放结束时的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)videoDidFinished:(NSNotification *)notice
{
[self.movie.view removeFromSuperview];
[self.aview removeFromSuperview];
self.movie = nil;
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}