示例名:MoviePlayer(电影播放器)
功能:播放本地或网络视频
框架:MediaPlayer.framework
源码解释:
1 -initAndPlayMovie创建一个电影播放控制器,指定播放内容的URL,并开始播放
2 -applicationDidFinishLaunching从主程序束中得到Movie.m4v的路径,并调用-initAndPlayMovie开始播放
核心源码:
1 -(void)initAndPlayMovie:(NSURL *)movieURL
2 {
3 // Initialize a movie player object with the specified URL
4 MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
5
6 // save the movie player object
7 self.moviePlayer = mp;
8 [mp release];
9
10 // Play the movie!
11 [self.moviePlayer play];
12 }
1 - (void)applicationDidFinishLaunching:(UIApplication *)application {
2 // Override point for customization after application launch
3 NSURL *movieURL;
4
5 NSBundle *bundle = [NSBundle mainBundle];
6 NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
7 movieURL = [NSURL fileURLWithPath:moviePath];
8 [self initAndPlayMovie:movieURL];
9 [window makeKeyAndVisible];
10 }
网上有,拿来改改。挺好用的。比较简单。分享。
- (void) Playback : (id) sender
{
//指定播放的名字和地址
NSString *path = [[NSBundle mainBundle] pathForResource:@"ss" ofType:@"mov"];
// m4a格式也是允许的。
MPMoviePlayerController* theMovie=[[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie play];
}
- (void) setUpForPlay: (NSNotification *) notification
{
UIButton *button = (UIButton *)[self.view viewWithTag:BUTTON_TAG];
// Prepare button for re-starting
[button setTitle:@"Start" forState:UIControlStateNormal];
[button removeTarget:self action:@selector(stopPlayback:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(startPlayback:) forControlEvents: UIControlEventTouchUpInside];
}