使用Swift开发一个贝乐虎启蒙App – 视频播放(一)

前言

上一篇咱们现已把视频页面的屏幕旋转搞好了,现在咱们开端做一下视频播映

视频播映

视频播映的话咱们运用Swift版的Player库,下面是效果swift系统swifter

运用Swift开发一个贝乐虎启蒙App - 视频播映(一)

1、依据效果图把所需的icon导入到工程里边

2、在PlayerViewContrapproveoller里边增加urlid特点,url便是视频的urlid则是恳求右侧数据的id

private var url: String
private var id: String
init(url: String, id: String) {
    self.url = url
    self.id = id
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.orientationMask = .landscapeRight
    }
    super.init(nibName: nil, bundle: nil)
}

3、Player的运用

  • Player一些特点
/// Player delegate.
open weak var playerDelegate: PlayerDelegate?
/// Playback delegate.
open weak var playbackDelegate: PlayerPlaybackDelegate?
/// - Parameter url: URL of the asset.
open var url: URL?
/// Volume for the player, ranging from 0.0 to 1.0 on a linear scale.
open var volume: Float
/// Pauses playback automatically when resigning active.
open var playbackPausesWhenResigningActive: Bool = true
/// Pauses playback automatically when backgrounded.
open var playbackPausesWhenBackgrounded: Bool = true
/// Resumes playback when became active.
open var playbackResumesWhenBecameActive: Bool = true
/// Resumes playback when entering foreground.
open var playbackResumesWhenEnteringForeground: Bool = true
/// Playback freezes on last frame frame when true and does not reset seek position timestamp..
open var playbackFreezesAtEnd: Bool = false
/// Current playback state of the Player.
open var playbackState: PlaybackState = .stopped
/// Current buffering state of the Player.
open var bufferingState: BufferingState = .unknown
/// Maximum duration of playback.
open var maximumDuration: TimeInterval
/// Media playback's current time interval in seconds.
open var currentTimeInterval: TimeInterval
/// Media playback's current time.
open var currentTime: CMTime
  • Playappearer一方法
/// 从头开端播映 Begins playback of the media from the beginning.
open func playFromBeginning()
/// 从当时时刻开端播映 Begins playback of the media from the current time.
open func playFromCurrentTime()
/// 暂停播映 Pauses playback of the media.
open func pause()
/// 中止播映 Stops playback of the media.
open func stop()
/// 快进/快退
/// Updates playback to the specified time.
///
/// - Parameters:
///  - time: The time to switch to move the playback.
///  - completionHandler: Call block handler after seeking/
open func seek(to time: CMTime, completionHandler: ((Bool) -> Swift.Void)? = nil)
  • PlayerPlayerDelegatePlayerPapprovelaybackDappearelegate
/// Player delegate protocol
public protocol PlayerDelegate: AnyObject {
  func playerReady(_ player: Player)
  func playerPlaybackStateDidChange(_ player: Player)
  func playerBufferingStateDidChange(_ player: Player)
  // This is the time in seconds that the video has been buffered.
  // If implementing a UIProgressView, user this value / player.maximumDuration to set progress.
  func playerBufferTimeDidChange(_ bufferTime: Double)
  func player(_ player: Player, didFailWithError error: Error?)
}
/// Player playback protocol
public protocol PlayerPlaybackDelegate: AnyObject {
  func playerCurrentTimeDidChange(_ player: Player)
  func playerPlaybackWillStartFromBeginning(_ player: Player)
  func playerPlaybackDidEnd(_ player: Player)
  func playerPlaybackWillLoop(_ player: Player)
  func playerPlaybackDidLoop(_ player: Player)
}

3.1、懒加载player

/// 懒加载
private lazy var player: Player = {
    let player = Player()
    player.playerDelegate = self
    player.playbackDelegate = self
    player.playbackFreezesAtEnd = true /// 播映在最终一帧中止
    player.playerView.playerBackgroundColor = .black
    player.view.backgroundColor = UIColor.black
    player.playbackResumesWhenBecameActive = false
    return player
}()

3.2、布局playappstoreer

/// 布局player
private func addSubviews() {
    view.backgroundColor = UIColor.black
    addChild(player)
    view.addSubview(player.view)
    player.didMove(toParent: self)
    player.view.snp.makeConstraints { (make) in
        make.edges.equalToSuperview()
    }
}

3.3、增加PlayerDelegate

extension PlayerViewController: PlayerDelegate {
    /// 开端播映
    func playerReady(_ player: Player) {
    }
    /// 播映状况
    func playerPlaybackStateDidChange(_ player: Player) {
    }
    /// 缓冲状况
    func playerBufferingStateDidChange(_ player: Player) {
    }
    /// 缓冲进展
    func playerBufferTimeDidChange(_ bufferTime: Double) {
    }
    /// 播映过错
    func player(_ player: Player, didFailWithError error: Error?) {
    }
}

播映状况和反冲状况都是枚举,咱们来看看其定义

PlaybackState

/// Asset playback states.
public enum PlaybackState: Int, CustomStringConvertible {
    case stopped = 0 /// 中止播映
    case playing /// 播映
    case paused /// 暂停
    case failed /// 播映过错
    public var description: String {
        get {
            switch self {
            case .stopped:
                return "Stopped"
            case .playing:
                return "Playing"
            case .failed:
                return "Failed"
            case .paused:
                return "Paused"
            }
        }
    }
}

BufferingState

/// Asset buffering states.
public enum BufferingState: Int, CustomStringConvertible {
    case unknown = 0
    case ready
    case delayed
    public var description: String {
        get {
            switch self {
            case .unknown:
                return "Unknown"
            case .ready:
                return "Ready"
            case .delayed:
                return "Delayed"
            }
        }
    }
}

3.4、增加PlayerPlaybackDelegate

extension PlayerViewController: PlayerDelegate {
    /// 播映进展
    func playerCurrentTimeDidChange(_ player: Player) {
    }
    func playerPlaybackWillStartFromBeginning(_ player: Player) {
    }
    /// 播映完毕
    func playerPlaybackDidEnd(_ player: Player) {
    }
    func playerPlaybackWillLoop(_ player: Player) {}
    func playerPlaybackDidLoop(_ player: Player) {}
}

3.5、播映

player.url = URL(string: url)
player.playFromBeginning()

运用Swift开发一个贝乐虎启蒙App - 视频播映(一)

视频播映就做完了,接下来便是增加控制层来swift是什么组织缩写控制播映

发表评论

提供最优质的资源集合

立即查看 了解详情