Skip to content
项目

关于

SZAVPlayer is a lightweight audio/video player library, based on AVPlayer and AVAssetResourceLoaderDelegate, pure-Swift. Support cache and video image output.

设计与媒体

这些数字如何统计

可以做什么

  • Encapsulate the state changes of AVPlayer and AVPlayerItem and output them uniformly, greatly reducing the implementation cost of audio/video play.
  • Achieved full control of AVPlayer data loading, based on AVAssetResourceLoaderDelegate. Through the Range request and corresponding cache, it can respond to player's requests ASAP. It also can play the cached audio/video normally in the weak network and no network enviroment.
  • Support video image output, can be drawn to multiple views at the same time.
  • Load AVAsset asynchronously to not blocking the main thread.

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README_cn.md

原文

SZAVPlayer

Version Carthage compatible SPM supported License Platform

SZAVPlayer是一个轻量级的音视频播放库,基于AVPlayer和AVAssetResourceLoaderDelegate来实现播放过程和缓存过程。纯Swift实现,同时支持缓存播放和视频画面同步输出(比如可以拿来实现同时绘制到不同View上)。

基于AVPlayer实现音视频播放过程和问题整理在这里

功能

  • 对AVPlayer和AVPlayerItem进行良好的封装,对外输出使用便捷的几个接口,极大缩减实现音视频播放所要耗费的时间。
  • 基于AVAssetResourceLoaderDelegate实现了对AVPlayer数据加载的完整控制。通过请求过来的Range,有效的使用本地已缓存部分数据,拼接出本地请求和远程请求在最快的时间内进行回应,因此在弱网或者无网情况下也能正常播放已缓存部分数据。
  • 支持视频画面的同步输出,可直接绘制在各个不同的视图上。
  • 异步加载AVAsset,保证加载和切换音视频不会发生卡顿。
  • 提供手动清理缓存,同时内部也会根据设定的容量自动进行清理。

主流程图

Main Flow

提示

如果你发现在模拟器上一直播放不正常,可以尝试完全退出模拟器,然后重新开始测试,这个是模拟器自身的BUG。

使用

  1. 创建播放器并设置代理

    let player = SZAVPlayer()
    player.delegate = self
    
    audioPlayer = player
    
  2. 使用url进行配置

    // uniqueID is to identify wether they are the same audio. If set to nil will use urlStr to create one.
    let config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)
    audioPlayer.setupPlayer(config: config)
    

    或

    // If you want play video, pass an additional parameter `isVideo`.
    let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: true/false)
    videoPlayer.setupPlayer(config: config)
    
  3. 实现 SZAVPlayerDelegate。

    extension AudioViewController: SZAVPlayerDelegate {
    
        func avplayer(_ avplayer: SZAVPlayer, refreshed currentTime: Float64, loadedTime: Float64, totalTime: Float64) {
            progressView.update(currentTime: currentTime, totalTime: totalTime)
        }
    
        func avplayer(_ avplayer: SZAVPlayer, didChanged status: SZAVPlayerStatus) {
            switch status {
            case .readyToPlay:
                SZLogInfo("ready to play")
                if playerStatus == .playing {
                    audioPlayer.play()
                }
            case .playEnd:
                SZLogInfo("play end")
                handlePlayEnd()
            case .loading:
                SZLogInfo("loading")
            case .loadingFailed:
                SZLogInfo("loading failed")
            case .bufferBegin:
                SZLogInfo("buffer begin")
            case .bufferEnd:
                SZLogInfo("buffer end")
                if playerStatus == .stalled {
                    audioPlayer.play()
                }
            case .playbackStalled:
                SZLogInfo("playback stalled")
                playerStatus = .stalled
            }
        }
    
        func avplayer(_ avplayer: SZAVPlayer, didReceived remoteCommand: SZAVPlayerRemoteCommand) -> Bool {
            return false
        }
    
    }
    
  4. 切换音视频。

    // The setupPlayer function will automatically determine if it has been setup before. 
    // If it is, it will directly call the replacePalyerItem function to replace the new audio.
    audioPlayer.setupPlayer(config: config)
    

    或

    // or just use this function.
    audioPlayer.replace(urlStr: audio.url, uniqueID: nil)
    

    these two functions have the same effect.

  5. 开启视频画面输出。

    • Set isVideoOutputEnabled to true.
    let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: true)
    videoPlayer.setupPlayer(config: config)
    
    • Implement avplayer delegate function.
    func avplayer(_ avplayer: SZAVPlayer, didOutput videoImage: CGImage) {
        videoOutputView1.layer.contents = videoImage
    }
    
    • Call removeVideoOutput function when ready to release the player.
    videoPlayer.removeVideoOutput()
    
  6. 跳跃到某个特定时间。

    audioPlayer.seekPlayerToTime(time: currentTime, completion: nil)
    
  7. 设定最大缓存容量。

    // Unit: MB, if reached the max size, it will automatically trim the cache.
    SZAVPlayerCache.shared.setup(maxCacheSize: 100)
    
  8. 清理所有缓存。

    SZAVPlayerCache.shared.cleanCache()
    
  9. 播放纯本地文件。因为纯本地文件的话无需走自定义加载流程,所以直接设置 disableCustomLoading 为 true 即可。

    let config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)
    config.disableCustomLoading = true
    audioPlayer.setupPlayer(config: config)
    

示例

The Example project has implemented a complete play example, including play/pause/previous/next/seekToTime/cleanCache, etc.

To run the example project, clone the repo, and run pod install from the Example directory first.

要求

  • iOS 10.0+
  • Swift 5.0+

安装

CocoaPods

SZAVPlayer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SZAVPlayer'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SZAVPlayer into your Xcode project using Carthage, specify it in your Cartfile:

github "eroscai/SZAVPlayer" ~> 1.1.1

Swift Package Manager

From Xcode 11, you can use Swift Package Manager to add SZAVPlayer to your project.

  • Select File > Swift Packages > Add Package Dependency. Enter https://github.com/eroscai/SZAVPlayer.git in the "Choose Package Repository" dialog.
  • Add CoreServices.framework and AVFoundation.framework to your project if not added before. (If anyone knows how to do this automatically, please tell me).

Author

eroscai, [email protected]

License

SZAVPlayer is available under the MIT license. See the LICENSE file for more info.

探索相关项目

加载中…