KoalaTea Asset Player
KoalaTea Asset Player 是 AVPlayer 的包装器,为您的应用程序提供了一种简单的播放视频和音频的方式。
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
需求
iOS 10.3+
安装
KoalaTeaAssetPlayer 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'KoalaTeaAssetPlayer'
用法
始终要首先导入
import KoalaTeaAssetPlayer
首先,您需要从URL创建一个资产。(文件URL或远程URL)
let asset = Asset(url: Bundle.main.url(forResource: "SampleVideo_1280x720_5mb", withExtension: "mp4")!)
现在您有两个选择
选择1:直接使用AssetPlayer
以下是您如何配置AssetPlayer以及您可以使用哪些选项。
let assetPlayer = AssetPlayer()
/*
Player options.
`shouldLoop` will loop asset indefinitely.
`startMuted` will ...... start the asset muted
*/
let options: [AssetPlayerSetupOption] = [.shouldLoop, .startMuted]
// These are some remote commands if you want your media to be accessible on the lock screen
let remoteCommands: [RemoteCommand] = [.playback,
.changePlaybackPosition,
.skipForward(interval: 15),
.skipBackward(interval: 15)]
// You can of course also just set either of the above to []
// Now use the `setup` action.
assetPlayer.perform(action: .setup(with: asset,
options: options,
remoteCommands: remoteCommands))
执行任何操作与设置一样简单。一切只是‘操作’。
assetPlayer.perform(action: .play)
assetPlayer.perform(action: .pause)
assetPlayer.perform(action: .skip(by: 30))
assetPlayer.perform(action: .skip(by: -15))
现在您已经设置好并可以执行操作,您需要实现委托以获取有关播放器的必要信息。
assetPlayer.delegate = self
extension ViewController: AssetPlayerDelegate {
func playerIsSetup(_ properties: AssetPlayerProperties) {
// Here the player is setup and you can set the max value for a time slider or anything else you need to show in your UI.
}
func playerPlaybackStateDidChange(_ properties: AssetPlayerProperties) {
// Can handle state changes here if you need to display the state in a view
}
func playerCurrentTimeDidChange(_ properties: AssetPlayerProperties) {
// This is fired every second while the player is playing.
}
func playerCurrentTimeDidChangeInMilliseconds(_ properties: AssetPlayerProperties) {
/*
This is fired every millisecond while the player is playing.
You should probably update your slider here to have a smooth animated slider
*/
}
func playerPlaybackDidEnd(_ properties: AssetPlayerProperties) {
/*
The playback did end for the player
Dismiss the player, track some progress, whatever you need to do after the asset is done.
*/
}
func playerBufferedTimeDidChange(_ properties: AssetPlayerProperties) {
/*
This is for tracking the buffered time for the player.
This is that little gray bar you see on YouTube or Vimeo that shows how much track time you have left before you see that buffering spinner
*/
}
func playerDidFail(_ error: Error?) {
// 😱 Something has gone wrong and you should really present a nice error message and log this somewhere. Please don't just print the error.
}
}
如果您使用视频,您还必须设置播放视图。我已经尽量让它变得尽可能简单。
// Asset player has a player view for you to use
let playerView = assetPlayer.playerView
// Then you can layout the view however you want.
playerView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(playerView)
NSLayoutConstraint.activate([
playerView.topAnchor.constraint(equalTo: view.topAnchor),
playerView.heightAnchor.constraint(equalTo: playerView.widthAnchor, multiplier: 9/16),
playerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
playerView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
然后您就可以出发了!
选择2:使用预制的UI
我已经创建了一个AssetPlayerView
作为可以制作的UI的示例。我会继续工作在UI上,随着更新的发布发布更多。
// There are a few options you can use to customize this view
let options: [ControlsViewOption] = [.bufferBackgroundColor(.lightGray),
.bufferSliderColor(.darkGray),
.playbackSliderColor(.red),
.sliderCircleColor(.white)]
let assetPlayerView = AssetPlayerView(controlsViewOptions: options)
// With this view it only supports programmatic layout right now.
assetPlayerView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(assetPlayerView)
NSLayoutConstraint.activate([
assetPlayerView.topAnchor.constraint(equalTo: view.topAnchor),
assetPlayerView.heightAnchor.constraint(equalTo: assetPlayerView.widthAnchor, multiplier: 9/16),
assetPlayerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
assetPlayerView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
// and we have a function to setup the asset player much like we do above.
assetPlayerView.setupPlayback(asset: asset, options: [.shouldLoop], remoteCommands: .all(skipInterval: 30))
贡献
提交一个问题。
提交一个PR。
或给我发一封电子邮件,我会回复您。
作者
Craig Holliday
电子邮件:[email protected]
twitter:https://twitter.com/TheMrHolliday
许可
KoalaTeaAssetPlayer在MIT许可下可用。有关更多信息,请参阅LICENSE文件。