iPhone視頻播放項(xiàng)目開(kāi)發(fā)
iPhone視頻播放項(xiàng)目開(kāi)發(fā)是本文要介紹的內(nèi)容,我感覺(jué)iphone視頻的播放一直是一個(gè)軟肋,我在之前因?yàn)樽隽艘粋€(gè)iphone視頻的項(xiàng)目所以其有所研究。 大家應(yīng)該知道!3.2后iphone視頻方面增加了和多通知!這在之前是沒(méi)有的,于是多了很多功能,我感覺(jué)這方面還是不錯(cuò)的!
下面一步一步的講講視頻:
首先展示一下代碼:
- -(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ù)庫(kù)中進(jìn)行的
- [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);
- }
- }
通過(guò)以上代碼我想大家應(yīng)該對(duì)iphone視頻的播放有所了解了!我重點(diǎn)說(shuō)幾點(diǎn)吧:
1、我在播放視頻的時(shí)候加上了對(duì)手機(jī)版本的判斷,這是因?yàn)楹芏嗤ㄖ?.2 之前的版本是不能用的。
2、在代碼中我加入了大量的通知,我一個(gè)一個(gè)說(shuō)來(lái):
(1) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk4Player.moviePlayer];
上面這個(gè)通知是在視頻播放完的時(shí)候觸發(fā)的,就是說(shuō)你想在播放完視頻的時(shí)候想做什么工作可以在方法中寫入。
(2) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(movieReadyToPlay:)
- name: MPMovieMediaTypesAvailableNotification
- object: nil];
上面這個(gè)通知是在準(zhǔn)備播放視頻觸發(fā)的。
(3) [[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(addMoviePicture:)
- name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- object: nil];
上面這個(gè)通知是在加載視頻之前還沒(méi)有播放視頻之前觸發(fā)的。
(4)[[NSNotificationCenter defaultCenter]
- addObserver: self
- selector: @selector(changeAddedPictureSize:)
- name: UIApplicationWillChangeStatusBarOrientationNotification
- object: nil];
上面這個(gè)通知是設(shè)備發(fā)生重力變換的時(shí)候觸發(fā)的。
通過(guò)以上幾個(gè)通知基本上因該滿足一個(gè)不錯(cuò)的,可觀賞性的視頻播放器了。大家可以試試!!
小結(jié):iPhone視頻播放項(xiàng)目開(kāi)發(fā)的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!