Oc MPMoviePlayerController(视频播放器)~dome

1.导入系统框架 UIKit.framework
CoreGraphics.framework
Foundation.framework
MediaPlayer.framework

导入 MP4 的文件用于播放视频用的


#import "ViewController.h"
#import //视频播放器

@interface ViewController ()
//播放视图
@property (strong,nonatomic) IBOutletUIView *movieView;
//播放按钮
- (IBAction)play:(id)sender;
//播放电影控制器
@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1. 创建本地URL(也可创建基于网络的URL)
    NSURL* movieUrl = [[NSBundle mainBundle] URLForResource:@"aaa"withExtension:@"mp4"];
    
    // 使用指定URL创建MPMoviePlayerController
    //2. MPMoviePlayerController将会播放该URL对应的视频
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:movieUrl];
    
    //3. 设置该播放器的控制条风格。
    _moviePlayer.controlStyle =MPMovieControlStyleEmbedded;
    
    //4. 设置该播放器的缩放模式
    _moviePlayer.scalingMode =MPMovieScalingModeAspectFit;
    [_moviePlayer.view setFrame: CGRectMake(0 ,0 , 380 ,320)];
}

//点击播放按钮
- (IBAction)play:(id)sender
{
    //添加视频显示内容
    [self.movieView addSubview:_moviePlayer.view];
 
    //添加视频声音内容
    [_moviePlayer prepareToPlay];
    
}

/**重写该方法,控制该视图控制器只支持横屏显示
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
 */
@end

你可能感兴趣的:(Oc MPMoviePlayerController(视频播放器)~dome)