MediaPlayer framework for video playback on iOS

Source: Internet
Author: User

To play a movie file:

You can use MPMoviePlayerController to play movie files in the IOS SDK. However, there are strict formatting requirements for playing movie files on iOS devices, and only movie files in the following two formats can be played.

? H. 3.0 (Baseline profile)

? MPEG-4 Part 2 Video
MPMoviePlayerController can play local video files or play video files on the Internet.

First, you need to introduce the media Player framework in your project

Item-->targets-->build Phases-->link Binary with Libraries

Click the + sign in the lower left corner

Locate Mediaplayer.framework and add


Add the following code where you want to use the library:

[CPP]View Plaincopy
    1. #import <MediaPlayer/MediaPlayer.h>   



This project is named Mediaplayertest, the source code is as follows:

VideoPlayerViewController.h Header File Code:

[CPP]View Plaincopy
  1. #import <UIKit/UIKit.h>   
  2. #import <MediaPlayer/MediaPlayer.h>   
  3. @interface Viewcontroller:uiviewcontroller
  4. {
  5. UIButton *playbtn;
  6. MPMoviePlayerController *movieplayer;
  7. }
  8. @property (Retain, nonatomic) Iboutlet UIButton *playbtn;
  9. @property (Retain, nonatomic) MPMoviePlayerController *movieplayer;
  10. -(Ibaction) PlayVideo: (ID) sender;
  11. @end



V IDEOPLAYERVIEWCONTROLLER.M Implementation file code:

[CPP]View Plaincopy
  1. #import "ViewController.h"   
  2. #import <MediaPlayer/MediaPlayer.h>   
  3. @interface Viewcontroller ()
  4. @end
  5. @implementation Viewcontroller
  6. @synthesize playbtn;
  7. @synthesize MoviePlayer;
  8. -(void) viewdidload
  9. {
  10. [Super Viewdidload];
  11. Self.movieplayer = [[MPMoviePlayerController alloc] Initwithcontenturl:
  12. [Nsurl urlwithstring:@"http://v.youku.com/player/getRealM3U8/vid/XNTY2MTAxOTUy/type/video.m3u8"];
  13. Self.moviePlayer.movieControlMode = Mpmoviecontrolmodedefault;
  14. [Self.moviePlayer.view setframe:cgrectmake (0, 0, 320, 380)];
  15. additional setup after loading the view, typically from a nib.   
  16. }
  17. -(void) didreceivememorywarning
  18. {
  19. [Super didreceivememorywarning];
  20. //Dispose of any resources, can be recreated.   
  21. }
  22. -(Ibaction) PlayVideo: (ID) sender
  23. {
  24. NSLog (@"into PlayVideo." )  );
  25. [Self.view Insertsubview:[self.movieplayer view] atindex:10];
  26. [Self.movieplayer play];
  27. }
  28. @end



Mainstoryboard.storyboard:

Associate the Viewcontroller playbtn with the IB button


Associate the touch up inside event of the button with the PlayVideo action of the View controller


Operation Result:



Reference:

IOS 6 Programming-using the MPMoviePlayerController class to implement a video player

Multimedia playback of iOS development

Simple iphone video player

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.