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
- #import <MediaPlayer/MediaPlayer.h>
This project is named Mediaplayertest, the source code is as follows:
VideoPlayerViewController.h Header File Code:
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- #import <MediaPlayer/MediaPlayer.h>
- @interface Viewcontroller:uiviewcontroller
- {
- UIButton *playbtn;
- MPMoviePlayerController *movieplayer;
- }
- @property (Retain, nonatomic) Iboutlet UIButton *playbtn;
- @property (Retain, nonatomic) MPMoviePlayerController *movieplayer;
- -(Ibaction) PlayVideo: (ID) sender;
- @end
V IDEOPLAYERVIEWCONTROLLER.M Implementation file code:
[CPP]View Plaincopy
- #import "ViewController.h"
- #import <MediaPlayer/MediaPlayer.h>
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- @synthesize playbtn;
- @synthesize MoviePlayer;
- -(void) viewdidload
- {
- [Super Viewdidload];
- Self.movieplayer = [[MPMoviePlayerController alloc] Initwithcontenturl:
- [Nsurl urlwithstring:@"http://v.youku.com/player/getRealM3U8/vid/XNTY2MTAxOTUy/type/video.m3u8"];
- Self.moviePlayer.movieControlMode = Mpmoviecontrolmodedefault;
- [Self.moviePlayer.view setframe:cgrectmake (0, 0, 320, 380)];
- additional setup after loading the view, typically from a nib.
- }
- -(void) didreceivememorywarning
- {
- [Super didreceivememorywarning];
- //Dispose of any resources, can be recreated.
- }
- -(Ibaction) PlayVideo: (ID) sender
- {
- NSLog (@"into PlayVideo." ) );
- [Self.view Insertsubview:[self.movieplayer view] atindex:10];
- [Self.movieplayer play];
- }
- @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