加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift3 MPMoviePlayerViewController的使用,iOS播放视频

发布时间:2020-12-14 06:30:06 所属栏目:百科 来源:网络整理
导读:iOS 播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。MPMoviePlayerViewController里面包含了一个MPMoviePlayerController,如果要在页面中内嵌播放器的话,使用 MPMoviePlayerController,如果只需要播放一个视频,那么用 M

iOS播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController。MPMoviePlayerViewController里面包含了一个MPMoviePlayerController,如果要在页面中内嵌播放器的话,使用MPMoviePlayerController,如果只需要播放一个视频,那么用MPMoviePlayerViewController也是很方便的。

下面是我自己封装的一个调用播放器的方法,用Swift3.0写的

import UIKit
import MediaPlayer

final class YSDMediaPlayer: NSObject {
    static let shared = YSDMediaPlayer()
    private override init() {}
    
    func showPlayerWithFile(path: String?,presentedViewController: UIViewController) {
        guard let pa = path else {
            return
        }
        autoreleasepool {
            let movieURL = URL(fileURLWithPath: pa)
            let movieController = MPMoviePlayerViewController(contentURL: movieURL)!
            movieController.moviePlayer.prepareToPlay()
            movieController.moviePlayer.controlStyle = .fullscreen
            movieController.view.backgroundColor = UIColor.black
            movieController.view.frame = CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height)
            presentedViewController.present(movieController,animated: true) {
                NotificationCenter.default.addObserver(forName: NSNotification.Name.MPMoviePlayerPlaybackDidFinish,object: movieController.moviePlayer,queue: OperationQueue.main,using: { (noti) in
                    let theMovie = noti.object as! MPMoviePlayerController
                    NotificationCenter.default.removeObserver(self,name: NSNotification.Name.MPMoviePlayerPlaybackDidFinish,object: theMovie)
                    movieController.dismiss(animated: true,completion: nil)
                })
            }
        }
    }
}

moviePlayer.moviewControlMode = MPMovieControlModeDefault

MPMovieControlModeDefault 显示播放/暂停、音量和时间控制

MPMovieControlModeVolumeOnly 只显示音量控制

MPMovieControlModeHidden 没有控制器

moviePlayer.scallingMode = MPMovieScallingModeAspectFit;

你可以使用下列宽高比值:

MPMovieScallingModeNone 不做任何缩放

MPMovieScallingModeAspectFit 适应屏幕大小,保持宽高比

MPMovieScallingModeAspectFill 适应屏幕大小,保持宽高比,可裁剪

MPMovieScallingModeFill 充满屏幕,不保持宽高比

你会观察到以下通知:

MPMoviePlayerContentPreloadDidFinishNotification

当电影播放器结束对内容的预加载后发出。因为内容可以在仅加载了一部分的情况下播放,所以这个通知可能在已经播放后才发出。

MPMoviePlayerScallingModeDidChangedNotification

当用户改变了电影的缩放模式后发出。用户可以点触缩放图标,在全屏播放和窗口播放之间切换。

MPMoviePlayerPlaybackDidFinishNotification

当电影播放完毕或者用户按下了Done按钮后发出。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读