博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - MPMoviePlayer 视频播放
阅读量:6693 次
发布时间:2019-06-25

本文共 16621 字,大约阅读时间需要 55 分钟。

前言

MP_EXTERN_CLASS_AVAILABLE(3_2) NS_DEPRECATED_IOS(3_2, 9_0, "Use AVPlayerViewController in AVKit.")    @interface MPMoviePlayerViewController : UIViewController         @available(iOS, introduced=3.2, deprecated=9.0, message="Use AVPlayerViewController in AVKit.")    public class MPMoviePlayerViewController : UIViewController            MP_EXTERN_CLASS_AVAILABLE(2_0) NS_DEPRECATED_IOS(2_0, 9_0, "Use AVPlayerViewController in AVKit.")    @interface MPMoviePlayerController : NSObject 
@available(iOS, introduced=2.0, deprecated=9.0, message="Use AVPlayerViewController in AVKit.") public class MPMoviePlayerController : NSObject, MPMediaPlayback
  • 视频播放:
    • 添加库文件:MediaPlayer.framework
    • 包含头文件:#import <MediaPlayer/MediaPlayer.h>

1、本地/网络视频播放

1.1 使用 MPMoviePlayerViewController 播放

  • Objective-C

    // 添加库文件:MediaPlayer.framework    // 包含头文件:#import 
    // 声明媒体播放控件 @property(nonatomic, retain)MPMoviePlayerViewController *mpMoviePlayerVC; // 加载文件路径,加载本地视频 NSURL *mediaUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"步步高手机" ofType:@"mp4"]]; // 加载文件路径,加载网络视频 NSURL *mediaUrl = [NSURL URLWithString:@"http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4"]; // 实例化媒体播放控件 mpMoviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaUrl]; // 弹出播放页面,开始播放 [self presentMoviePlayerViewControllerAnimated: mpMoviePlayerVC];
  • Swift

    // 添加库文件:MediaPlayer.framework    // 包含头文件:import MediaPlayer    // 声明媒体播放控件    var mpMoviePlayerVC:MPMoviePlayerViewController!    // 加载文件路径,加载本地视频    let mediaUrl:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("步步高手机", ofType: "mp4")!)    // 加载文件路径,加载网络视频    let mediaUrl:NSURL = NSURL(string: "http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4")!    // 实例化媒体播放控件    mpMoviePlayerVC = MPMoviePlayerViewController(contentURL: mediaUrl)    // 弹出播放页面,开始播放    self.presentMoviePlayerViewControllerAnimated(mpMoviePlayerVC)

1.2 使用 MPMoviePlayerController 播放

  • Objective-C

    // 添加库文件:MediaPlayer.framework    // 包含头文件:#import 
    // 声明媒体播放控件 @property(nonatomic, retain)MPMoviePlayerController *mpMoviePlayer; // 加载文件路径,加载本地视频 NSURL *mediaUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"步步高手机" ofType:@"mp4"]]; // 加载文件路径,加载网络视频 NSURL *mediaUrl = [NSURL URLWithString:@"http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4"]; // 实例化媒体播放控件 mpMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl]; // 添加播放页面,开始播放 [self.view addSubview:mpMoviePlayer.view]; // 设置是否全屏播放,必须设置,必须放在添加页面之后设置,为 NO 时,必须设置 mpMoviePlayer.view.frame mpMoviePlayer.fullscreen = YES;
  • Swift

    // 添加库文件:MediaPlayer.framework    // 包含头文件:import MediaPlayer    // 声明媒体播放控件    var mpMoviePlayer:MPMoviePlayerController!    // 加载文件路径,加载本地视频    let mediaUrl:NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("步步高手机", ofType: "mp4")!)    // 加载文件路径,加载网络视频    let mediaUrl:NSURL = NSURL(string: "http://w2.dwstatic.com/1/5/1525/127352-100-1434554639.mp4")!    // 实例化媒体播放控件    mpMoviePlayer = MPMoviePlayerController(contentURL: mediaUrl)    // 添加播放页面,开始播放    self.view.addSubview(mpMoviePlayer.view)    // 设置是否全屏播放,必须设置,必须放在添加页面之后设置,为 NO 时,必须设置 mpMoviePlayer.view.frame    mpMoviePlayer.fullscreen = true

2、本地/网络视频播放设置

2.1 使用 MPMoviePlayerViewController 播放

  • Objective-C

    // 显示播放页面    /*        弹出播放页面,开始播放    */    [self presentMoviePlayerViewControllerAnimated: mpMoviePlayerVC];    // 开始播放    [mpMoviePlayerVC.moviePlayer play];    // 暂停播放    [mpMoviePlayerVC.moviePlayer pause];    // 停止播放    /*        停止播放会自动退出播放界面    */    [mpMoviePlayerVC.moviePlayer stop];    // 退出播放    /*        退出播放界面,停止播放    */    [self dismissMoviePlayerViewControllerAnimated];
  • Swift

    // 显示播放页面    /*        弹出播放页面,开始播放    */    self.presentMoviePlayerViewControllerAnimated(mpMoviePlayerVC)    // 开始播放    mpMoviePlayerVC.moviePlayer.play()    // 暂停播放    mpMoviePlayerVC.moviePlayer.pause()    // 停止播放    /*        停止播放会自动退出播放界面    */    mpMoviePlayerVC.moviePlayer.stop()    // 退出播放    /*        退出播放界面,停止播放    */    self.dismissMoviePlayerViewControllerAnimated()

2.2 使用 MPMoviePlayerController 播放

  • Objective-C

    // 判断媒体是否正在通过 AirPlay 播放    BOOL airPlayVideoActive = mpMoviePlayer.isAirPlayVideoActive;    // 判断是否准备好显示    BOOL readyForDisplay = mpMoviePlayer.readyForDisplay;    // 获取播放控制器所在的视图    /*        The view in which the media and playback controls are displayed    */    UIView *playbackView = mpMoviePlayer.view;    // 获取媒体播放背景视图    /*        A view for customization which is always displayed behind movie content    */    UIView *backgroundView = mpMoviePlayer.backgroundView;    // 获取播放状态    /*        MPMoviePlaybackStateStopped,                停止        MPMoviePlaybackStatePlaying,                播放        MPMoviePlaybackStatePaused,                 暂停        MPMoviePlaybackStateInterrupted,            中断        MPMoviePlaybackStateSeekingForward,         快进        MPMoviePlaybackStateSeekingBackward         快退        Returns the current playback state of the movie player    */    MPMoviePlaybackState playbackState = mpMoviePlayer.playbackState;    // 获取网络加载状态    /*        MPMovieLoadStateUnknown        = 0,       状态未知        MPMovieLoadStatePlayable       = 1 << 0,  可播放        MPMovieLoadStatePlaythroughOK  = 1 << 1,  Playback will be automatically started in this                                                  state when shouldAutoplay is YES        MPMovieLoadStateStalled        = 1 << 2,  Playback will be automatically paused in this                                                   state, if started        Returns the network load state of the movie player    */    MPMovieLoadState loadState = mpMoviePlayer.loadState;    // 获取媒体类型    /*        MPMovieMediaTypeMaskNone  = 0,        MPMovieMediaTypeMaskVideo = 1 << 0,        MPMovieMediaTypeMaskAudio = 1 << 1        The types of media in the movie, or MPMovieMediaTypeNone if not known    */    MPMovieMediaTypeMask movieMediaType = mpMoviePlayer.movieMediaTypes;    // 获取媒体原始尺寸    /*        The natural size of the movie, or CGSizeZero if not known/applicable    */    CGSize naturalSize = mpMoviePlayer.naturalSize;    // 获取媒体播放时长    /*        The duration of the movie, or 0.0 if not known    */    NSTimeInterval duration = mpMoviePlayer.duration;    // 获取当前可播放的时长    /*        for progressively downloaded network content    */    NSTimeInterval playableDuration = mpMoviePlayer.playableDuration;    // 设置播放页面大小    mpMoviePlayer.view.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 200);    // 缓冲视频    /*        即使不写,系统也会自动调用该方法    */    [mpMoviePlayer prepareToPlay];    // 显示播放页面    /*        添加播放页面,开始播放    */    [self.view addSubview:mpMoviePlayer.view];    // 开始播放    [mpMoviePlayer play];    // 暂停播放    [mpMoviePlayer pause];    // 停止播放    [mpMoviePlayer stop];    // 设置全屏    [mpMoviePlayer setFullscreen:YES animated:YES];    // 快进    [mpMoviePlayer beginSeekingForward];    // 快退    [mpMoviePlayer beginSeekingBackward];    // 停止快进或快退    [mpMoviePlayer endSeeking];    // 设置是否全屏播放    /*        必须设置,必须放在添加页面之后设置,设置为 NO 时,必须设置 mpMoviePlayer.view.frame 的大小    */    mpMoviePlayer.fullscreen = NO;    // 设置是否自动开始播放    /*        默认为 YES    */    mpMoviePlayer.shouldAutoplay = NO;    // 设置是否允许通过 AirPlay 播放    /*        Defaults to YES on iOS 5.0 and later    */    mpMoviePlayer.allowsAirPlay = NO;    // 设置当前播放时间    mpMoviePlayer.currentPlaybackTime = 10;    // 设置当前播放速度    /*        默认为 1.0 (normal speed),设为 0.0 时暂停播放    */    mpMoviePlayer.currentPlaybackRate = 1.0;    // 设置开始播放时间    /*        The start time of movie playback. Defaults to NaN, indicating the natural start time of         the movie    */    mpMoviePlayer.initialPlaybackTime = 10;    // 设置停止播放时间    /*        The end time of movie playback. Defaults to NaN, which indicates natural end time of         the movie    */    mpMoviePlayer.endPlaybackTime = 20;    // 设置播放控制器类型    /*        MPMovieControlStyleNone,        没有控制器,  No controls        MPMovieControlStyleEmbedded,    嵌入式,默认,Controls for an embedded view   可将播放窗口全屏化        MPMovieControlStyleFullscreen,  全屏式,     Controls for fullscreen playback        MPMovieControlStyleDefault = MPMovieControlStyleEmbedded    */    mpMoviePlayer.controlStyle = MPMovieControlStyleEmbedded;    // 设置重复播放模式    /*        MPMovieRepeatModeNone,   不重复,默认        MPMovieRepeatModeOne     重复播放    */    mpMoviePlayer.repeatMode = MPMovieRepeatModeNone;    // 设置画面缩放模式    /*        MPMovieScalingModeNone,         不做任何缩放        MPMovieScalingModeAspectFit,    适应屏幕大小,保持宽高比,默认        MPMovieScalingModeAspectFill,   充满屏幕,保持宽高比        MPMovieScalingModeFill          充满屏幕,不保持宽高比    */    mpMoviePlayer.scalingMode = MPMovieScalingModeAspectFit;    // 设置媒体资源类型    /*        The playback type of the movie. Defaults to MPMovieSourceTypeUnknown.        Specifying a playback type before playing the movie can result in faster load times.        MPMovieSourceTypeUnknown,        MPMovieSourceTypeFile,         Local or progressively downloaded network content        MPMovieSourceTypeStreaming     Live or on-demand streaming content    */    mpMoviePlayer.movieSourceType = MPMovieSourceTypeFile;
  • Swift

    // 判断媒体是否正在通过 AirPlay 播放    let airPlayVideoActive:Bool = mpMoviePlayer.airPlayVideoActive    // 判断是否准备好显示    let readyForDisplay:Bool = mpMoviePlayer.readyForDisplay    // 获取播放控制器所在的视图    /*        The view in which the media and playback controls are displayed    */    let playbackView:UIView = mpMoviePlayer.view    // 获取媒体播放背景视图    /*        A view for customization which is always displayed behind movie content    */    let backgroundView:UIView = mpMoviePlayer.backgroundView    // 获取播放状态    /*        Stopped,           停止        Playing,           播放        Paused,            暂停        Interrupted,       中断        SeekingForward,    快进        SeekingBackward    快退        Returns the current playback state of the movie player    */    let playbackState:MPMoviePlaybackState = mpMoviePlayer.playbackState    // 获取网络加载状态    /*        Unknown        = 0,        状态未知        Playable       = 1 << 0,   可播放        PlaythroughOK  = 1 << 1,   Playback will be automatically started in this state when                                    shouldAutoplay is YES        Stalled        = 1 << 2,   Playback will be automatically paused in this state, if started        Returns the network load state of the movie player    */    let loadState:MPMovieLoadState = mpMoviePlayer.loadState    // 获取媒体类型    /*        MaskNone  = 0,        MaskVideo = 1 << 0,        MaskAudio = 1 << 1        The types of media in the movie, or MPMovieMediaTypeNone if not known    */    let movieMediaType:MPMovieMediaTypeMask = mpMoviePlayer.movieMediaTypes    // 获取媒体原始尺寸    /*        The natural size of the movie, or CGSizeZero if not known/applicable    */    let naturalSize:CGSize = mpMoviePlayer.naturalSize    // 获取媒体播放时长    /*        The duration of the movie, or 0.0 if not known    */    let duration:NSTimeInterval = mpMoviePlayer.duration    // 获取当前可播放的时长    /*        for progressively downloaded network content    */    let playableDuration:NSTimeInterval = mpMoviePlayer.playableDuration    // 设置播放页面大小    mpMoviePlayer.view.frame = CGRectMake(10, 30, self.view.bounds.size.width - 20, 200)    // 缓冲视频    /*        即使不写,系统也会自动调用该方法    */    mpMoviePlayer.prepareToPlay()    // 显示播放页面    /*        添加播放页面,开始播放    */    self.view.addSubview(mpMoviePlayer.view)    // 开始播放    mpMoviePlayer.play()    // 暂停播放    mpMoviePlayer.pause()    // 停止播放    mpMoviePlayer.stop()    // 设置全屏    mpMoviePlayer.setFullscreen(true, animated: true)    // 快进    mpMoviePlayer.beginSeekingForward()    // 快退    mpMoviePlayer.beginSeekingBackward()    // 停止快进或快退    mpMoviePlayer.endSeeking()    // 设置是否全屏播放    /*        必须设置,必须放在添加页面之后设置,设置为 NO 时,必须设置 mpMoviePlayer.view.frame 的大小    */    mpMoviePlayer.fullscreen = false    // 设置是否自动开始播放    /*        默认为 YES    */    mpMoviePlayer.shouldAutoplay = false    // 设置是否允许通过 AirPlay 播放    /*        Defaults to YES on iOS 5.0 and later    */    mpMoviePlayer.allowsAirPlay = false    // 设置当前播放时间    mpMoviePlayer.currentPlaybackTime = 10    // 设置当前播放速度    /*        默认为 1.0 (normal speed),设为 0.0 时暂停播放    */    mpMoviePlayer.currentPlaybackRate = 1.0    // 设置开始播放时间    /*        The start time of movie playback. Defaults to NaN, indicating the natural start time of         the movie    */    mpMoviePlayer.initialPlaybackTime = 10    // 设置停止播放时间    /*        The end time of movie playback. Defaults to NaN, which indicates natural end time of         the movie    */    mpMoviePlayer.endPlaybackTime = 20    // 设置播放控制器类型    /*        None,        没有控制器,  No controls        Embedded,    嵌入式,默认,Controls for an embedded view     可将播放窗口全屏化        Fullscreen,  全屏式,     Controls for fullscreen playback    */    mpMoviePlayer.controlStyle = MPMovieControlStyle.Embedded    // 设置重复播放模式    /*        None,   不重复,默认        One     重复播放    */    mpMoviePlayer.repeatMode = MPMovieRepeatMode.None    // 设置画面缩放模式    /*        None,          不做任何缩放        AspectFit,     适应屏幕大小,保持宽高比,默认        AspectFill,    充满屏幕,保持宽高比        Fill           充满屏幕,不保持宽高比    */    mpMoviePlayer.scalingMode = MPMovieScalingMode.AspectFit    // 设置媒体资源类型    /*        The playback type of the movie. Defaults to MPMovieSourceTypeUnknown.        Specifying a playback type before playing the movie can result in faster load times.        Unknown,        File,          Local or progressively downloaded network content        Streaming      Live or on-demand streaming content    */    mpMoviePlayer.movieSourceType = MPMovieSourceType.File

3、监听播放进度

// Movie Property Notifications:媒体属性观察者        MPMovieMediaTypesAvailableNotification   // 媒体属性可获取        MPMovieSourceTypeAvailableNotification   // 媒体类型是 MPMovieSourceTypeUnknown        MPMovieDurationAvailableNotification     // 媒体播放时长可获取        MPMovieNaturalSizeAvailableNotification  // 媒体原始尺寸可获取    // Movie Player Notifications:媒体播放观察者        MPMediaPlaybackIsPreparedToPlayDidChangeNotification   // 准备播放        MPMoviePlayerPlaybackDidFinishNotification            // 播放完成或退出播放        MPMoviePlayerPlaybackDidFinishReasonUserInfoKey       // NSNumber (MPMovieFinishReason)        MPMoviePlayerPlaybackStateDidChangeNotification       // 播放状态改变,通过程序或者用户操作改变        MPMoviePlayerScalingModeDidChangeNotification         // 画面缩放模式改变        MPMoviePlayerLoadStateDidChangeNotification           // 网络加载状态改变        MPMoviePlayerNowPlayingMovieDidChangeNotification     // 当前播放媒体改变        MPMoviePlayerWillEnterFullscreenNotification          // 将要进入全屏播放状态        MPMoviePlayerDidEnterFullscreenNotification           // 已经进入全屏播放状态        MPMoviePlayerWillExitFullscreenNotification           // 将要退出全屏播放状态        MPMoviePlayerDidExitFullscreenNotification            // 已经退出全屏播放状态        MPMoviePlayerFullscreenAnimationDurationUserInfoKey   // NSNumber of double (NSTimeInterval)        MPMoviePlayerFullscreenAnimationCurveUserInfoKey      // NSNumber of NSUInteger (UIViewAnimationCurve)        MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification  // 开始或停止通过 AirPlay 播放        MPMoviePlayerReadyForDisplayDidChangeNotification       // 准备好显示
  • Objective-C

    // 添加系统通知观察者    /*        观察的状态发生改变时将会调用添加的相应方法进行处理    */    [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(playFinished)                                                  name:MPMoviePlayerPlaybackDidFinishNotification                                                object:mpMoviePlayer];    // 观察者响应事件    - (void)playFinished {    }    // 取消监听    [[NSNotificationCenter defaultCenter] removeObserver:self                                                     name:MPMoviePlayerPlaybackDidFinishNotification                                                   object:mpMoviePlayer];
  • Swift

    // 添加系统通知观察者    /*        观察的状态发生改变时将会调用添加的相应方法进行处理    */    NSNotificationCenter.defaultCenter().addObserver(self,                                             selector: #selector(MpMoviePlayerController.playFinished),                                                 name: MPMoviePlayerPlaybackDidFinishNotification,                                               object: mpMoviePlayer)    // 观察者响应事件    func playFinished() {    }    // 取消监听    NSNotificationCenter.defaultCenter().removeObserver(self,                                                    name: MPMoviePlayerPlaybackDidFinishNotification,                                                  object: mpMoviePlayer)

转载于:https://www.cnblogs.com/QianChia/p/5771156.html

你可能感兴趣的文章
JSONObject 自定义过滤配置
查看>>
Hive job,抛错java.io.FileNotFoundException:/.../container_000001(Is a directory)
查看>>
海量数据查询
查看>>
类的this指针有以下特点
查看>>
mysql主主互备架构
查看>>
驱动学习之gpiolib的建立过程
查看>>
利用NSX搭建专有子网
查看>>
网页特殊符号HTML代码大全
查看>>
Zabbix 3.0 监控MySQL
查看>>
Storm 环境部署及简单使用
查看>>
nfs客户端挂载出错 mount.nfs access denied by server while mounting
查看>>
linux集群之LVS入门和企业级实战(续二)
查看>>
DataGuard physical standby创建与维护
查看>>
*8 脚本小练习
查看>>
[原创]windows server 2012 AD架构 试验 系列 – 10 AD域和站点部署(1)
查看>>
nginx StubStatus模块
查看>>
安装ipython报错"ImportError: No module named shutil_get_terminal_size"
查看>>
使用 Dockerfile 构建镜像
查看>>
操作无法完成。键入的打印机名不正确,或者指定的打印机没有连接到服务器上...
查看>>
linux中的nginx的基础知识
查看>>