iPhone視頻播放項目開發(fā)
iPhone視頻播放項目開發(fā)是本文要介紹的內(nèi)容,我感覺iphone視頻的播放一直是一個軟肋,我在之前因為做了一個iphone視頻的項目所以其有所研究。 大家應(yīng)該知道!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提到最上面是在數(shù)據(jù)庫中進行的
- [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);
- }
- }
通過以上代碼我想大家應(yīng)該對iphone視頻的播放有所了解了!我重點說幾點吧:
1、我在播放視頻的時候加上了對手機版本的判斷,這是因為很多通知在3.2 之前的版本是不能用的。
2、在代碼中我加入了大量的通知,我一個一個說來:
(1) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk4Player.moviePlayer];
上面這個通知是在視頻播放完的時候觸發(fā)的,就是說你想在播放完視頻的時候想做什么工作可以在方法中寫入。
(2) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieReadyToPlay:)
- name: MPMovieMediaTypesAvailableNotification
- object: nil];
上面這個通知是在準(zhǔn)備播放視頻觸發(fā)的。
(3) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(addMoviePicture:)
- name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- object: nil];
上面這個通知是在加載視頻之前還沒有播放視頻之前觸發(fā)的。
(4)[[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(changeAddedPictureSize:)
- name: UIApplicationWillChangeStatusBarOrientationNotification
- object: nil];
上面這個通知是設(shè)備發(fā)生重力變換的時候觸發(fā)的。
通過以上幾個通知基本上因該滿足一個不錯的,可觀賞性的視頻播放器了。大家可以試試!!
小結(jié):iPhone視頻播放項目開發(fā)的內(nèi)容介紹完了,希望本文對你有所幫助!