美文网首页
视频播放

视频播放

作者: 江河_ios | 来源:发表于2022-06-30 21:32 被阅读0次

import <MediaPlayer/MediaPlayer.h>

import "YLPeerConnectionViewController.h"

import <AVFoundation/AVFoundation.h>

@interface YLPeerConnectionViewController ()
//@property(nonatomic,strong)AVPlayer *player;

@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;//视频播放控制器

@end

@implementation YLPeerConnectionViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    //播放
    [self.moviePlayer play];

    //添加通知
    [self addNotification];
    }

-(void)dealloc{
//移除所有通知监控
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

pragma mark - 私有方法

/**

  • 取得本地文件路径

  • @return 文件路径
    */
    -(NSURL *)getFileUrl{
    // NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"The New Look of OS X Yosemite.mp4" ofType:nil];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"视频" ofType:@"mov"];
    // filePath = self.videoPath;
    NSURL *url=[NSURL fileURLWithPath:filePath];
    return url;
    }

/**

  • 创建媒体播放控制器
  • @return 媒体播放控制器
    */
    -(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
    NSURL *url=[self getFileUrl];
    _moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:url];
    _moviePlayer.view.frame=self.view.bounds;
    _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
    }

/**

  • 添加通知监控媒体播放控制器状态
    */
    -(void)addNotification{
    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];

}

/**

  • 播放状态改变,注意播放完成时的状态是暂停
  • @param notification 通知对象
    */
    -(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.moviePlayer.playbackState) {
    case MPMoviePlaybackStatePlaying:
    NSLog(@"正在播放...");
    break;
    case MPMoviePlaybackStatePaused:
    NSLog(@"暂停播放.");
    break;
    case MPMoviePlaybackStateStopped:
    NSLog(@"停止播放.");
    break;
    default:
    NSLog(@"播放状态:%li",self.moviePlayer.playbackState);
    break;
    }
    }

/**

  • 播放完成
  • @param notification 通知对象
    */
    -(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%li",self.moviePlayer.playbackState);
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

@end

相关文章

  • 初级视频播放功能

    打开相册选择视频 使用系统播放器播放视频 使用VideoView播放视频 使用SurfaceView播放视频 vo...

  • 3.4 音频播放.视频播放.相册调用.相机调用

    音频播放.视频播放.相册调用.相机调用 音频播放 视频播放 相册调用 视频音频资源 视频音频资源.png

  • 视频播放器

    系统播放器 打开视频列表 调用系统播放器播放视频 调用系统播放器播放网络视频 VideoView播放器 调用 V...

  • 视频播放

  • 视频播放

    import "ViewController.h" import "ZSPlayerView.h" @interf...

  • 视频播放

    NSString *file = [[NSBundle mainBundle]pathForResource:@"...

  • 视频播放

    视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器 MediaPlayer框架

  • 视频播放

    一. 视频播放介绍 实现方案四种: 1.AVPlayer 2.MPMoviePlayerControlle 3.M...

  • 播放视频

    主要使用 VideoView 类来实现。和 MediaPlayer 比较类似。 VideoView工作流程 1. ...

  • 视频播放

    1.依赖compile 'fm.jiecao:jiecaovideoplayer:2.0' compile'fm....

网友评论

      本文标题:视频播放

      本文链接:https://www.haomeiwen.com/subject/oirqbrtx.html