SZAVPlayer is a lightweight audio/video player library, based on AVPlayer
, pure-Swift. Support cache and video image output.
AVPlayer
and AVPlayerItem
and output them uniformly, greatly reducing the implementation cost of audio/video play.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.If you find that always play failed in the simulator, try exit simulator completely and restart again. This is kind of simulator's bug.
Create player and set delegate.
let player = SZAVPlayer()
player.delegate = self
audioPlayer = player
Setup player with 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)
or
// 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)
Implement 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
}
}
Replace new audio.
// 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
// or just use this function.
audioPlayer.replace(urlStr: audio.url, uniqueID: nil)
these two functions have the same effect.
Enable video image output.
isVideoOutputEnabled
to true
.let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: true)
videoPlayer.setupPlayer(config: config)
func avplayer(_ avplayer: SZAVPlayer, didOutput videoImage: CGImage) {
videoOutputView1.layer.contents = videoImage
}
removeVideoOutput
function when ready to release the player.videoPlayer.removeVideoOutput()
Seek player to time.
audioPlayer.seekPlayerToTime(time: currentTime, completion: nil)
Set max cache size.
// Unit: MB, if reached the max size, it will automatically trim the cache.
SZAVPlayerCache.shared.setup(maxCacheSize: 100)
Clean all cache.
SZAVPlayerCache.shared.cleanCache()
Play local files. Because there is no need to go through the custom loading process for local files, directly set disableCustomLoading
to 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.
SZAVPlayer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SZAVPlayer'
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
From Xcode 11, you can use Swift Package Manager to add SZAVPlayer to your project.
CoreServices.framework
and AVFoundation.framework
to your project if not added before. (If anyone knows how to do this automatically, please tell me).eroscai, csz0102@gmail.com
SZAVPlayer is available under the MIT license. See the LICENSE file for more info.