iPhone视频播放项目开发是本文要介绍的内容,我感觉iphone视频的播放一直是一个软肋,我在之前因为做了一个iphone视频的项目所以其有所研究。 大家应该知道!3.2后iphone视频方面增加了和多通知!这在之前是没有的,于是多了很多功能,我感觉这方面还是不错的!
下面一步一步的讲讲视频:
首先展示一下代码:
- -(void) playMovieWithSDK: (NSURL*) movieUrl
- {
- if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 3.2)
- {
- self.view.backgroundColor = [UIColor redColor];
- mSdk4Player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
- mSdk4Player.navigationController.navigationBar.tag = 1003;
- [self.navigationController.navigationBar setNeedsDisplay];
- if (mSdk4Player)
- {
- mSdk4Player.moviePlayer.shouldAutoplay = YES;
- [self presentMoviePlayerViewControllerAnimated:mSdk4Player];
- mSdk4Player.moviePlayer.scalingMode= MPMovieScalingModeAspectFit;
- [mSdk4Player.moviePlayer play];
- [mSdk4Player release];
- backgroundIamge = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
- [backgroundIamge setImage:[UIImage imageNamed:@"playBg.png"]];
- [mSdk4Player.view addSubview:backgroundIamge];
- backgroundIamge.hidden = YES;
- UIDevice *device = [UIDevice currentDevice];
- if (device.orientation == UIInterfaceOrientationPortrait || device.orientation == UIInterfaceOrientationPortraitUpsideDown)
- {
- backgroundIamge.center = CGPointMake(160, 240);
- }
- else if(device.orientation == UIInterfaceOrientationLandscapeLeft||device.orientation == UIInterfaceOrientationLandscapeRight)
- {
- backgroundIamge.center = CGPointMake(240, 160);
- }
- else
- {
- backgroundIamge.center = CGPointMake(160, 240);
- }
- [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk4Player.moviePlayer];
- [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieReadyToPlay:)
- name: MPMovieMediaTypesAvailableNotification
- object: nil];
- [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(addMoviePicture:)
- name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- object: nil];
- [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(changeAddedPictureSize:)
- name: UIApplicationWillChangeStatusBarOrientationNotification
- object: nil];
- }
- // backgroundIamge.center = CGPointMake(240, 160);
- }
- else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) //sdk 3.0 播放器
- {
- MPMoviePlayerController* mSdk3player = [[MPMoviePlayerController alloc] initWithContentURL: movieUrl];
- mSdk3player.scalingMode = MPMovieScalingModeAspectFit;
- [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk3player];
- [mSdk3player play];
- }
- }
- //播放电影
- -(void) playMovie
- {
- NSURL *movieUrl = [NSURL URLWithString:_movieString];
- if([movieUrl scheme])
- {
- [self playMovieWithSDK:movieUrl];
- //此处的将最新的movie提到最上面是在数据库中进行的
- [self checkHisState];
- [self setHistory];
- }
- else
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"" message: @"" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- }
- // 通知释放播放器
- -(void) movieFinishedCallback:(NSNotification*)aNotification
- {
- MPMoviePlayerController* theMovie=[aNotification object];
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:MPMoviePlayerPlaybackDidFinishNotification
- object:theMovie];
- _detailViewController = [[DetailViewController alloc]initWithNibName:nil bundle:nil messageData:_message index:index];
- [_detailViewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""]];
- [self.navigationController pushViewController:_detailViewController animated:NO];
- }
- -(void) addMoviePicture:(NSNotification*)aNotification
- {
- backgroundIamge.hidden = NO;
- }
- -(void) movieReadyToPlay:(NSNotification*)aNotification
- {
- backgroundIamge.hidden = YES;
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:MPMovieMediaTypesAvailableNotification
- object:nil];
- }
- -(void) changeAddedPictureSize:(NSNotification*)aNotification
- {
- UIDevice *device = [UIDevice currentDevice];
- if (device.orientation == UIInterfaceOrientationPortrait || device.orientation == UIInterfaceOrientationPortraitUpsideDown)
- {
- backgroundIamge.center = CGPointMake(160, 240);
- }
- else if(device.orientation == UIInterfaceOrientationLandscapeLeft||device.orientation == UIInterfaceOrientationLandscapeRight)
- {
- backgroundIamge.center = CGPointMake(240, 160);
- }
- else
- {
- backgroundIamge.center = CGPointMake(160, 240);
- }
- }
通过以上代码我想大家应该对iphone视频的播放有所了解了!我重点说几点吧:
1、我在播放视频的时候加上了对手机版本的判断,这是因为很多通知在3.2 之前的版本是不能用的。
2、在代码中我加入了大量的通知,我一个一个说来:
(1) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk4Player.moviePlayer];
上面这个通知是在视频播放完的时候触发的,就是说你想在播放完视频的时候想做什么工作可以在方法中写入。
(2) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieReadyToPlay:)
- name: MPMovieMediaTypesAvailableNotification
- object: nil];
上面这个通知是在准备播放视频触发的。
(3) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(addMoviePicture:)
- name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- object: nil];
上面这个通知是在加载视频之前还没有播放视频之前触发的。
(4)[[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(changeAddedPictureSize:)
- name: UIApplicationWillChangeStatusBarOrientationNotification
- object: nil];
上面这个通知是设备发生重力变换的时候触发的。
通过以上几个通知基本上因该满足一个不错的,可观赏性的视频播放器了。大家可以试试!!
小结:iPhone视频播放项目开发的内容介绍完了,希望本文对你有所帮助!