ios视频播放

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];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容