First, create a view-based project named PlayVideo, and add MediaPlayer. framework to Frameworks in the project ,:
After adding the video file, add a video file to the file. m4v. Pay attention to the extension. The extension of the video file that I first added is mp4. When the video is played all the time, it is not played. So I can convert the video to the m4v extension!
Next, add the attributes in PlayViewContrller. h file:
MPMoviePlayerController * player; // controls video playback.
Then add the code implementation in the PlayViewController. m file:
[Cpp]-(void) viewDidLoad
{
// Obtain the video file name
NSString * url = [[NSBundle mainBundle] pathForResource: @ "video" ofType: @ "m4v"];
// Initialize the player. The file is video. m4v.
Player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: url];
// Then, you use the NSNotificationCenter class to register the notification. In this way, movieFinishedCallback is called when the video is played (that is, the video ends;
[[Nsicationicationcenter defacenter center]
AddObserver: self selector: @ selector (movieFinishedCallback :)
Name: MPMoviePlayerPlaybackDidFinishNotification object: player];
// Set the Video View Size
Player. view. frame = CGRectMake (10, 10,300,300 );
// Add controls
[Self. view addSubview: player. view];
// Play the video
[Player play];
[Super viewDidLoad];
}
// When a movie stops playing, you should cancel the notification, delete the video, and then release the Playing Object:
-(Void) movieFinishedCallback :( NSNotification *) aNotification {
MPMoviePlayerController * moviePlayer = [aNotification object];
// Logout notification
[[Nsnotifcenter center defacenter center] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer];
// Delete a video
[MoviePlayer. view removeFromSuperview];
// Release
[Player release];
}
-(Void) viewDidLoad
{
// Obtain the video file name
NSString * url = [[NSBundle mainBundle] pathForResource: @ "video" ofType: @ "m4v"];
// Initialize the player. The file is video. m4v.
Player = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: url];
// Then, you use the NSNotificationCenter class to register the notification. In this way, movieFinishedCallback is called when the video is played (that is, the video ends;
[[Nsicationicationcenter defacenter center]
AddObserver: self selector: @ selector (movieFinishedCallback :)
Name: MPMoviePlayerPlaybackDidFinishNotification object: player];
// Set the Video View Size
Player. view. frame = CGRectMake (10, 10,300,300 );
// Add controls
[Self. view addSubview: player. view];
// Play the video
[Player play];
[Super viewDidLoad];
}
// When a movie stops playing, you should cancel the notification, delete the video, and then release the Playing Object:
-(Void) movieFinishedCallback :( NSNotification *) aNotification {
MPMoviePlayerController * moviePlayer = [aNotification object];
// Logout notification
[[Nsnotifcenter center defacenter center] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer];
// Delete a video
[MoviePlayer. view removeFromSuperview];
// Release
[Player release];
}
Effect:
:
Click the zoom in icon in the lower right corner.
Double-click the screen to change to full screen:
Click "Done" to restore the status !!!
OK! Implementation! This blog is my own exercise. I have not explained many things clearly. Please forgive me!
From Ren haili (3G/mobile development)