播放电影文件:
iOS sdk中可以使用MPMoviePlayerController来播放电影文件。但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件。
? H.264 (Baseline Profile Level 3.0)
? MPEG-4 Part 2 video (Simple
Profile)
MPMoviePlayerController可以播放本地视频文件,也可以播放互联网上的视频文件。
首先在项目中需要引入Media Player 框架
项目-->Targets-->Build Phases-->Link Binary With Libraries
点击左下角的+号
找到MediaPlayer.framework并添加
在需要使用库的地方添加下面的代码:
- #import <MediaPlayer/MediaPlayer.h>
本项目名为MediaPlayerTest,源码如下:
VideoPlayerViewController.h 头文件代码:
- #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
VideoPlayerViewController.m 实现文件代码:
- #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)];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)playVideo:(id)sender
- {
- NSLog(@"Into playvideo.");
- [self.view insertSubview:[self.moviePlayer view] atIndex:10];
- [self.moviePlayer play];
- }
- @end
MainStoryboard.storyboard:
将ViewController的playBtn和IB的Button关联起来
将Button的Touch Up Inside事件和View Controller的playVideo操作关联起来
运行结果:
参考:
IOS 6编程-使用MPMoviePlayerController类实现视频播放器