Use of Mpmovieplayerviewcontroller (do not place the player directly on the main view controller, but in an internal modal views controller)

Source: Internet
Author: User

In fact MPMoviePlayerController if not played as embedded video (such as embedding a video in the news), it is usually played with a full screen, especially on the IPhone, itouch. So since iOS3.2, Apple is also thinking that since MPMoviePlayerController is often used to add its view views to another view controller as a child view, So why not just create a controller view to create a MPMoviePlayerController property and default full-screen playback, developers use this view controller directly during development. This interior has a MPMoviePlayerController view controller that is Mpmovieplayerviewcontroller, which inherits from Uiviewcontroller. Mpmovieplayerviewcontroller has a MoviePlayer property and an initialization method with a URL, and it internally implements some features that are unique to modal view display, such as the default is full-screen mode display, pop-up AutoPlay, When the modal window is displayed, clicking the "Done" button automatically exits the modal window and so on. In the following example, the player is not placed directly into the master view controller, but instead is placed in a modal views controller, which simply demonstrates the use of Mpmovieplayerviewcontroller.

The code is as follows:

Not a copy of the display:

Created by Linxiu on 15/12/18.



#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface Viewcontroller ()

Player View Controller
@property (Nonatomic,strong) Mpmovieplayerviewcontroller *movieplayerviewcontroller;
@property (Nonatomic,strong) UIButton *playbtn;
@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Nsurl *url = [Nsurl urlwithstring:@ "Http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];

SELF.PLAYBTN = [UIButton buttonwithtype:uibuttontypecustom];
Self.playBtn.frame = CGRectMake (30, 100, 100, 30);
[Self.playbtn settitle:@ "Play" forstate:uicontrolstatenormal];
[Self.playbtn addtarget:self Action: @selector (Playclick:) forcontrolevents:uicontroleventtouchupinside];
[Self.view AddSubview:self.playBtn];

}
#pragma mark-ui Events
-(void) Playclick: (UIButton *) sender{

self.movieplayerviewcontroller=nil;//ensures that the video playback controller view is re-created each time you click to avoid issues that don't play when you click again

Note that a drop-down display animation is added to the uiviewcontroller extension of two methods for modal display and closing Mpmovieplayerviewcontroller in MPMoviePlayerViewController.h
[Self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
[Self PresentViewController:self.moviePlayerViewController animated:yes completion:nil];
//
}
-(void) dealloc{
Remove all notification monitoring
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
}


/**
* Get Network file path
*
* @return File path
*/
-(Nsurl *) getnetworkurl{
NSString *urlstr=@ "Http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4";
Urlstr=[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Nsurl *url=[nsurl URLWITHSTRING:URLSTR];
return URL;
}


-(Mpmovieplayerviewcontroller *) movieplayerviewcontroller{
if (!_movieplayerviewcontroller) {
Nsurl *url=[self Getnetworkurl];
_movieplayerviewcontroller=[[mpmovieplayerviewcontroller Alloc]initwithcontenturl:url];
[Self addnotification];
}
return _movieplayerviewcontroller;
}

#pragma mark-Controller notification
/**
* Add notification monitoring media playback controller status
*/
-(void) addnotification{
Nsnotificationcenter *notificationcenter=[nsnotificationcenter Defaultcenter];
[Notificationcenter addobserver:self selector: @selector (mediaplayerplaybackstatechange:) Name: Mpmovieplayerplaybackstatedidchangenotification Object:self.moviePlayerViewController.moviePlayer];
[Notificationcenter addobserver:self selector: @selector (mediaplayerplaybackfinished:) Name: Mpmovieplayerplaybackdidfinishnotification Object:self.moviePlayerViewController.moviePlayer];

}

/**
* Playback status changes, note that when playback is complete, the status is paused
*
* @param notification Notification Object
*/
-(void) Mediaplayerplaybackstatechange: (nsnotification *) notification{
Switch (self.moviePlayerViewController.moviePlayer.playbackState) {
Case mpmovieplaybackstateplaying:
NSLog (@ "playing ...");
Break
Case mpmovieplaybackstatepaused:
NSLog (@ "pause playback.");
Break
Case mpmovieplaybackstatestopped:
NSLog (@ "stop playing.");
Break
Default
NSLog (@ "Playback status:%li", self.moviePlayerViewController.moviePlayer.playbackState);
Break
}
}

/**
* Playback Complete
*
* @param notification Notification Object
*/
-(void) mediaplayerplaybackfinished: (nsnotification *) notification{
NSLog (@ "play complete.%li", self.moviePlayerViewController.moviePlayer.playbackState);
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end

It needs to be emphasized here that because the Mpmovieplayerviewcontroller initialization method does a lot of work (such as setting URLs, AutoPlay, adding monitoring done by click-to-do, etc.), So when you click Play again to eject the new modal window, if you do not destroy the previous Mpmovieplayerviewcontroller, the new object cannot be initialized so that it cannot be played again.

Use of Mpmovieplayerviewcontroller (do not place the player directly on the main view controller, but in an internal modal views controller)

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.