Play video on iphone

Source: Internet
Author: User

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)

 

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.