如何知道 AVPlayer 何时准备好播放并将信息发送到控制器
How to know when AVPlayer is ready to play and sent info to the controller
我正在 swift 中构建一个广播流媒体应用程序。目前一切正常,但我想稍微改善一下用户体验。
我有一个 RadioPlayer.swift class 来处理我的无线电操作。
import Foundation
import AVFoundation
class RadioPlayer {
static let sharedInstance = RadioPlayer()
private var player = AVPlayer(URL: NSURL(string:"http://rfcmedia.streamguys1.com/classicrock.mp3")!)
private var isPlaying = false
func play() {
player = AVPlayer(URL: NSURL(string: "http://rfcmedia.streamguys1.com/classicrock.mp3")!)
player.play()
isPlaying = true
player.currentItem?.status
}
func pause() {
player.pause()
isPlaying = false
player.replaceCurrentItemWithPlayerItem(nil)
}
func toggle() {
if isPlaying == true {
pause()
} else {
play()
}
}
func currentlyPlaying() -> Bool {
return isPlaying
}
然后我有一个实现 class 的视图控制器。我的 objective 是当播放器正在加载时,发送一条消息说正在准备流式传输,因此用户知道必须等待(也禁用播放按钮)。
所以我的问题是如何实现这一点,在 android 中我使用广播来发送消息,但我没有在 swift 中找到等效项。
您可以为 AVPlayer
属性添加观察者,例如在 Swift 3:
player.addObserver(self, forKeyPath: "reasonForWaitingToPlay", options: .new, context: &observerContext)
或者在Swift2中,使用.New
:
player.addObserver(self, forKeyPath: "reasonForWaitingToPlay", options: .New, context: &observerContext)
注意,这是使用私有 属性 来识别上下文:
private var observerContext = 0
然后就可以添加观察者方法了。在 Swift 3:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observerContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
// look at `change![.newKey]` to see what the status is, e.g.
if keyPath == "reasonForWaitingToPlay" {
NSLog("\(keyPath): \(change![.newKey])")
}
}
或在Swift 2:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard context == &observerContext else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
return
}
// look at `change![NSKeyValueChangeNewKey]` to see what the status is, e.g.
if keyPath == "reasonForWaitingToPlay" {
NSLog("\(keyPath): \(change![NSKeyValueChangeNewKey])")
}
}
我正在 swift 中构建一个广播流媒体应用程序。目前一切正常,但我想稍微改善一下用户体验。
我有一个 RadioPlayer.swift class 来处理我的无线电操作。
import Foundation
import AVFoundation
class RadioPlayer {
static let sharedInstance = RadioPlayer()
private var player = AVPlayer(URL: NSURL(string:"http://rfcmedia.streamguys1.com/classicrock.mp3")!)
private var isPlaying = false
func play() {
player = AVPlayer(URL: NSURL(string: "http://rfcmedia.streamguys1.com/classicrock.mp3")!)
player.play()
isPlaying = true
player.currentItem?.status
}
func pause() {
player.pause()
isPlaying = false
player.replaceCurrentItemWithPlayerItem(nil)
}
func toggle() {
if isPlaying == true {
pause()
} else {
play()
}
}
func currentlyPlaying() -> Bool {
return isPlaying
}
然后我有一个实现 class 的视图控制器。我的 objective 是当播放器正在加载时,发送一条消息说正在准备流式传输,因此用户知道必须等待(也禁用播放按钮)。
所以我的问题是如何实现这一点,在 android 中我使用广播来发送消息,但我没有在 swift 中找到等效项。
您可以为 AVPlayer
属性添加观察者,例如在 Swift 3:
player.addObserver(self, forKeyPath: "reasonForWaitingToPlay", options: .new, context: &observerContext)
或者在Swift2中,使用.New
:
player.addObserver(self, forKeyPath: "reasonForWaitingToPlay", options: .New, context: &observerContext)
注意,这是使用私有 属性 来识别上下文:
private var observerContext = 0
然后就可以添加观察者方法了。在 Swift 3:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observerContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
// look at `change![.newKey]` to see what the status is, e.g.
if keyPath == "reasonForWaitingToPlay" {
NSLog("\(keyPath): \(change![.newKey])")
}
}
或在Swift 2:
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
guard context == &observerContext else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
return
}
// look at `change![NSKeyValueChangeNewKey]` to see what the status is, e.g.
if keyPath == "reasonForWaitingToPlay" {
NSLog("\(keyPath): \(change![NSKeyValueChangeNewKey])")
}
}