ios视频播放

2017-08-19  本文已影响7人  被风吹乱的思念

1.效果图

.

2.注意点

导入依赖库


屏幕快照 2017-08-19 上午10.33.50.png

3代码展示

#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *movieView;
@property(nonnull,strong)MPMoviePlayerController *moviePlayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1. 创建本地URL(也可创建基于网络的URL)
  //播放本地视频
    NSURL *movieUrl = [[NSBundle mainBundle] URLForResource:@"9999"withExtension:@"mov"];
    
    // 使用指定URL创建MPMoviePlayerController
    
    //2. MPMoviePlayerController将会播放该URL对应的视频
    
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:movieUrl];
    
    
    //3. 设置该播放器的控制条风格。
    
    _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    
    //4. 设置该播放器的缩放模式
    
    _moviePlayer.scalingMode =MPMovieScalingModeAspectFill;
    
    [_moviePlayer.view setFrame:self.movieView.bounds];
//这是为了检测屏幕是否变换(横屏竖屏)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
//在检测中加入视频
- (void)orientChange:(NSNotification *)notification
{
    [_moviePlayer.view setFrame:self.movieView.bounds];
}
- (IBAction)playClick:(id)sender {
    //添加视频显示内容
    
    [self.movieView addSubview:_moviePlayer.view];
    
    //添加视频声音内容
    _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    
    [_moviePlayer prepareToPlay];
}

上一篇下一篇

猜你喜欢

热点阅读