CachingPlayerItem 1.1.1

CachingPlayerItem 1.1.1

Shukov 维护。



  • sukov

CachingPlayerItem

CachingPlayerItem 是 AVPlayerItem 的子类,允许您在 iOS 上流式传输和缓存媒体内容。对这个工作的初始想法是在这里发现的 here

CI Status Version License Language Swift Platform Swift Package Manager

特点

  • 播放和缓存远程媒体
  • 下载的数据被缓冲并存储在文件中,因此您不会有任何 RAM 内存问题
  • 从本地文件/数据播放
  • 通过 CachingPlayerItemDelegate 代理方便的通知
  • CachingPlayerItemAVPlayerItem 的子类,所以您可以像使用 AVPlayerItem 一样使用它,并充分利用 AVFoundation 框架
  • 通过 CachingPlayerItemConfiguration 可配置下载缓冲区限制/读取数据限制
  • 不进行缓存即可播放远程媒体,并且仍然可以使用 CachingPlayerItemDelegate
  • 完整文档

要求

  • iOS 10.0+
  • Xcode 12.0+
  • Swift 5.0+

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖项管理器。您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 CachingPlayerItem 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'CachingPlayerItem'
end

Swift 包管理器

Swift 包管理器 是一个自动分发 Swift 代码的工具,并集成到 swift 编译器中。

一旦您的 Swift 包设置完毕,将 CachingPlayerItem 添加为依赖项,只需将其添加到您的 Package.swift 中的 dependencies 值即可。

dependencies: [
    .package(url: "https://github.com/sukov/CachingPlayerItem.git", from: "1.0.5")
]

用法

快速开始

import UIKit
import AVFoundation
import CachingPlayerItem

class ViewController: UIViewController {
    // You need to keep a strong reference to your player.
    var player: AVPlayer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        let url = URL(string: "https://random-url.com/video.mp4")!
        let playerItem = CachingPlayerItem(url: url)
        player = AVPlayer(playerItem: playerItem)
        player.automaticallyWaitsToMinimizeStalling = false
        player.play()
        
    }
}

来自苹果文档:强烈建议将AVPlayer的属性automaticallyWaitsToMinimizeStalling设置为false。不这样做可能会导致播放启动时间慢和从停滞中恢复不佳。

如果您想缓存文件而无需播放,或为了将来播放而预加载,请使用download()方法.

let playerItem = CachingPlayerItem(url: videoURL)
playerItem.download()

在下载的同时开始播放项目是可以的。

CachingPlayerItemDelegate 协议

注意:所有方法都是可选的。

@objc public protocol CachingPlayerItemDelegate {
    // MARK: Downloading delegate methods

    /// Called when the media file is fully downloaded.
    @objc optional func playerItem(_ playerItem: CachingPlayerItem, didFinishDownloadingFileAt filePath: String)

    /// Called every time a new portion of data is received.
    @objc optional func playerItem(_ playerItem: CachingPlayerItem, didDownloadBytesSoFar bytesDownloaded: Int, outOf bytesExpected: Int)

    /// Called on downloading error.
    @objc optional func playerItem(_ playerItem: CachingPlayerItem, downloadingFailedWith error: Error)

    // MARK: Playing delegate methods

    /// Called after initial prebuffering is finished, means we are ready to play.
    @objc optional func playerItemReadyToPlay(_ playerItem: CachingPlayerItem)

    /// Called when the player is unable to play the data/url.
    @objc optional func playerItemDidFailToPlay(_ playerItem: CachingPlayerItem, withError error: Error?)

    /// Called when the data being downloaded did not arrive in time to continue playback.
    @objc optional func playerItemPlaybackStalled(_ playerItem: CachingPlayerItem)
}

重要:您负责管理下载的本地媒体文件。如果您使用了生成随机filePath的初始化器,您可以在didFinishDownloadingFileAt委托方法中检索它。

示例

要运行示例项目,克隆仓库,然后从示例目录中首先运行pod install

作者

sukov, [email protected]

许可证

CachingPlayerItem 依据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。

已知限制

  • CachingPlayerItem 按顺序加载其内容。如果您试图播放尚未下载的部分,它会等待在此位置之前的数据下载完毕,然后才开始播放。
  • URL 必须包含文件扩展名,以便播放器能够正确加载。为了解决这个问题,可以指定自定义文件扩展名,例如 let playerItem = CachingPlayerItem(url: url, customFileExtension: "mp3")