IPhone video playback Project Development

Source: Internet
Author: User

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:

 
 
  1. -(Void) playMovieWithSDK: (NSURL *) movieUrl
  2. {
  3. If ([[[UIDevice currentDevice] systemVersion] doubleValue]> 3.2)
  4. {
  5. Self. view. backgroundColor = [UIColor redColor];
  6. MSdk4Player = [[MPMoviePlayerViewController alloc] initWithContentURL: movieUrl];
  7. MSdk4Player. navigationController. navigationBar. tag = 1003;
  8. [Self. navigationController. navigationBar setNeedsDisplay];
  9. If (mSdk4Player)
  10. {
  11. MSdk4Player. moviePlayer. shouldAutoplay = YES;
  12. [Self presentMoviePlayerViewControllerAnimated: mSdk4Player];
  13. MSdk4Player. moviePlayer. scalingMode = MPMovieScalingModeAspectFit;
  14. [MSdk4Player. moviePlayer play];
  15. [MSdk4Player release];
  16. BackgroundIamge = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, 80, 80)];
  17. [BackgroundIamge setImage: [UIImage imageNamed: @ "playBg.png"];
  18. [MSdk4Player. view addSubview: backgroundIamge];
  19. BackgroundIamge. hidden = YES;
  20. UIDevice * device = [UIDevice currentDevice];
  21. If (device. orientation = UIInterfaceOrientationPortrait | device. orientation = UIInterfaceOrientationPortraitUpsideDown)
  22. {
  23. BackgroundIamge. center = CGPointMake (160,240 );
  24. }
  25. Else if (device. orientation = UIInterfaceOrientationLandscapeLeft | device. orientation = UIInterfaceOrientationLandscapeRight)
  26. {
  27. BackgroundIamge. center = CGPointMake (240,160 );
  28. }
  29. Else
  30. {
  31. BackgroundIamge. center = CGPointMake (160,240 );
  32. }
  33. [[Nsicationicationcenter defacenter center]
  34. AddObserver: self
  35. Selector: @ selector (movieFinishedCallback :)
  36. Name: MPMoviePlayerPlaybackDidFinishNotification
  37. Object: mSdk4Player. moviePlayer];
  38. [[Nsicationicationcenter defacenter center]
  39. AddObserver: self
  40. Selector: @ selector (movieReadyToPlay :)
  41. Name: MPMovieMediaTypesAvailableNotification
  42. Object: nil];
  43. [[Nsicationicationcenter defacenter center]
  44. AddObserver: self
  45. Selector: @ selector (addMoviePicture :)
  46. Name: MPMoviePlayerNowPlayingMovieDidChangeNotification
  47. Object: nil];
  48. [[Nsicationicationcenter defacenter center]
  49. AddObserver: self
  50. Selector: @ selector (changeAddedPictureSize :)
  51. Name: UIApplicationWillChangeStatusBarOrientationNotification
  52. Object: nil];
  53. }
  54. // BackgroundIamge. center = CGPointMake (240,160 );
  55. }
  56. Else if ([[UIDevice currentDevice] systemVersion] doubleValue] <3.2) // sdk 3.0 player
  57. {
  58. MPMoviePlayerController * mSdk3player = [[MPMoviePlayerController alloc] initWithContentURL: movieUrl];
  59. MSdk3player. scalingMode = MPMovieScalingModeAspectFit;
  60. [[Nsicationicationcenter defacenter center]
  61. AddObserver: self
  62. Selector: @ selector (movieFinishedCallback :)
  63. Name: MPMoviePlayerPlaybackDidFinishNotification
  64. Object: mSdk3player];
  65. [MSdk3player play];
  66. }
  67. }
  68. // Play a movie
  69. -(Void) playMovie
  70. {
  71. NSURL * movieUrl = [NSURL URLWithString: _ movieString];
  72. If ([movieUrl scheme])
  73. {
  74. [Self playMovieWithSDK: movieUrl];
  75. // Here, the latest movie mentioned above is carried out in the database
  76. [Self checkHisState];
  77. [Self setHistory];
  78. }
  79. Else
  80. {
  81. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "message: @" "delegate: nil cancelButtonTitle: @" o" otherButtonTitles: nil];
  82. [Alert show];
  83. [Alert release];
  84. }
  85. }
  86. // Notification to release the player
  87. -(Void) movieFinishedCallback :( NSNotification *) aNotification
  88. {
  89. MPMoviePlayerController * theMovie = [aNotification object];
  90. [[Nsicationcenter center defacenter center] removeObserver: self
  91. Name: MPMoviePlayerPlaybackDidFinishNotification
  92. Object: theMovie];
  93. _ DetailViewController = [[DetailViewController alloc] initWithNibName: nil bundle: nil messageData: _ message index: index];
  94. [_ DetailViewController. navigationController. navigationBar setBackgroundImage: [UIImage imageNamed: @ ""];
  95. [Self. navigationController pushViewController: _ detailViewController animated: NO];
  96. }
  97. -(Void) addMoviePicture :( NSNotification *) aNotification
  98. {
  99. BackgroundIamge. hidden = NO;
  100. }
  101. -(Void) movieReadyToPlay :( NSNotification *) aNotification
  102. {
  103. BackgroundIamge. hidden = YES;
  104. [[Nsicationcenter center defacenter center] removeObserver: self
  105. Name: MPMovieMediaTypesAvailableNotification
  106. Object: nil];
  107. }
  108. -(Void) changeAddedPictureSize :( NSNotification *) aNotification
  109. {
  110. UIDevice * device = [UIDevice currentDevice];
  111. If (device. orientation = UIInterfaceOrientationPortrait | device. orientation = UIInterfaceOrientationPortraitUpsideDown)
  112. {
  113. BackgroundIamge. center = CGPointMake (160,240 );
  114. }
  115. Else if (device. orientation = UIInterfaceOrientationLandscapeLeft | device. orientation = UIInterfaceOrientationLandscapeRight)
  116. {
  117. BackgroundIamge. center = CGPointMake (240,160 );
  118. }
  119. Else
  120. {
  121. BackgroundIamge. center = CGPointMake (160,240 );
  122. }
  123. }

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]

 
 
  1. addObserver: self  
  2. selector: @selector(movieFinishedCallback:)  
  3. name: MPMoviePlayerPlaybackDidFinishNotification  
  4. 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]

 
 
  1. addObserver: self  
  2. selector: @selector(movieReadyToPlay:)  
  3. name: MPMovieMediaTypesAvailableNotification  
  4. object: nil]; 

The above notification is triggered when you prepare to play the video.

3) [[nsicationicationcenter defacenter center]

 
 
  1. addObserver: self  
  2. selector: @selector(addMoviePicture:)  
  3. name: MPMoviePlayerNowPlayingMovieDidChangeNotification  
  4. object: nil]; 

The above notification is loadingVideoNot playingVideoPreviously triggered.

4) [[nsicationicationcenter defacenter center]

 
 
  1. addObserver: self  
  2. selector: @selector(changeAddedPictureSize:)  
  3. name: UIApplicationWillChangeStatusBarOrientationNotification  
  4. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.