Create a simple video player

Source: Internet
Author: User

I will not talk about the sandbox mechanism of iOS here. This article uses some simple code to show you how to create a video player and whether a video file can be played. I also attached the Yuan Dynasty


I. Working Principle


1. When running the program, you can play the mp4 video that is loaded into the video player. Disable the video player after the video is played.


II. Implementation steps


1. Create a single view application named "video player.


2. first, we need to name it "MediaPlayer. framework is added to our program, and in ViewController. the h header file contains a header file -- # import <MediaPlayer/MediaPlayer. h>.


3. Next, we need to create a video player in the ViewController. h file. After completing steps 1 and 2, the Code is as follows:


# Import <UIKit/UIKit. h> # import <MediaPlayer/MediaPlayer. h> @ interface ViewController: UIViewController @ property (strong, nonatomic) MPMoviePlayerViewController * mpMoviePlayerViewController; // note that in the current version, only MPMoviePlayerViewController can be used, instead of MPMoviePlayerController @ end.


4.next, we can drag an mp4format video to our project, and we will drag a mp4 video named "droplet.mp4.


5. In the ViewController. m file, we need to write a method. After the video player finishes playing the video, the method will be called back through the message center. The function of this method is to disable the video player after the video is played. The detailed code is as follows:


// After the video player finishes playing the video, call back this method-(void) videoPlayingFinish :( id) sender {// disable the video player [self. mpMoviePlayerViewController. view removeFromSuperview];}


6. The last step is to create a video player. This video player runs when the program is loaded successfully. Therefore, you can enter it in the "-(void) viewDidLoad" method. The detailed code is as follows:


-(Void) viewDidLoad {[super viewDidLoad]; // 1. create a path resource and use mainBundle NSString * videoPath = [[NSBundle mainBundle] pathForResource: @ "droplet" ofType: @ "mp4"]; // 2. convert the path to a URL. This is a required NSURL * videoURL = [[NSURL alloc] initFileURLWithPath: videoPath]; // 3. create a video player _ mpMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL: videoURL]; // 4. add the video player to the current page and display [self. view addSubview: self. mpMoviePlayerViewController. view]; // 5. register with the message center and listen to the events that have ended playing the player. // when the message that has ended playing the player is sent, the custom method "-(void) videoPlayingFinish (id) will be called) sender "[[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (videoPlayingFinish :) name: @" MPMoviePlayerPlaybackDidFinishNotification "object: nil]; // nsicationicationcenter is set specifically for message communication between different classes in the program}


7. If the program you created does not use ARC, do not forget to rewrite the dealloc method to release the applied memory.


Iii. Extension supplement


Do you know how quickplay is written? Why don't I try to write my own quickplay software? Wait for your good news.



This article from the "haizhi ruifeng" blog, please be sure to keep this source http://winners.blog.51cto.com/7080842/1298434

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.