IPhone videoPlaying project development is the content to be introduced in this article, I feelIphone videoPlaying has always been a weakness.Iphone videoProject. Everyone should know! After 3.2Iphone videoAdded and added multiple notifications! This is not available before, so there are many more features, I feel this is good!
The following is a step-by-step description.Video:
The following code is displayed:
- -(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 );
- }
- [[Nsicationicationcenter defacenter center]
- AddObserver: self
- Selector: @ selector (movieFinishedCallback :)
- Name: MPMoviePlayerPlaybackDidFinishNotification
- Object: mSdk4Player. moviePlayer];
- [[Nsicationicationcenter defacenter center]
- AddObserver: self
- Selector: @ selector (movieReadyToPlay :)
- Name: MPMovieMediaTypesAvailableNotification
- Object: nil];
- [[Nsicationicationcenter defacenter center]
- AddObserver: self
- Selector: @ selector (addMoviePicture :)
- Name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- Object: nil];
- [[Nsicationicationcenter defacenter center]
- 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 player
- {
- MPMoviePlayerController * mSdk3player = [[MPMoviePlayerController alloc] initWithContentURL: movieUrl];
- MSdk3player. scalingMode = MPMovieScalingModeAspectFit;
- [[Nsicationicationcenter defacenter center]
- AddObserver: self
- Selector: @ selector (movieFinishedCallback :)
- Name: MPMoviePlayerPlaybackDidFinishNotification
- Object: mSdk3player];
- [MSdk3player play];
- }
- }
- // Play a movie
- -(Void) playMovie
- {
- NSURL * movieUrl = [NSURL URLWithString: _ movieString];
- If ([movieUrl scheme])
- {
- [Self playMovieWithSDK: movieUrl];
- // Here, the latest movie mentioned above is carried out in the database
- [Self checkHisState];
- [Self setHistory];
- }
- Else
- {
- UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "message: @" "delegate: nil cancelButtonTitle: @" o" otherButtonTitles: nil];
- [Alert show];
- [Alert release];
- }
- }
- // Notification to release the player
- -(Void) movieFinishedCallback :( NSNotification *) aNotification
- {
- MPMoviePlayerController * theMovie = [aNotification object];
- [[Nsicationcenter center defacenter center] 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;
- [[Nsicationcenter center defacenter center] 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 );
- }
- }
Through the above code, I think you shouldIphone videoHave some knowledge about playing! Let me focus on the following points:
1. I added a judgment on the mobile phone version when playing a video. This is because many notifications are unavailable in versions earlier than 3.2.
2. I added a large number of notifications to the code. one by one:
1) [[nsicationicationcenter defacenter center]
- addObserver: self
- selector: @selector(movieFinishedCallback:)
- name: MPMoviePlayerPlaybackDidFinishNotification
- object: mSdk4Player.moviePlayer];
The above notification is triggered when the video is played, that is, what you want to do when playing the video can be written in the method.
2) [[nsicationicationcenter defacenter center]
- addObserver: self
- selector: @selector(movieReadyToPlay:)
- name: MPMovieMediaTypesAvailableNotification
- object: nil];
The above notification is triggered when you prepare to play the video.
3) [[nsicationicationcenter defacenter center]
- addObserver: self
- selector: @selector(addMoviePicture:)
- name: MPMoviePlayerNowPlayingMovieDidChangeNotification
- object: nil];
The above notification is loadingVideoNot playingVideoPreviously triggered.
4) [[nsicationicationcenter defacenter center]
- addObserver: self
- selector: @selector(changeAddedPictureSize:)
- name: UIApplicationWillChangeStatusBarOrientationNotification
- object: nil];
The above notification is triggered when the device undergoes a gravity conversion.
The above notifications basically satisfy a good and entertaining situation.VideoThe player is ready. You can try it !!
Summary:IPhone videoThe development of the playback project is complete. I hope this article will help you!