我如何在另一个 class 中使用此扩展委托更新我的 ViewController UI
How can i update my ViewController UI using this extension delegate in another class
我正在使用一个名为 AudioManager
的 class 你可以找到它作为这个问题的答案 here。
它有委托方法 AVAudioPlayerDelegate
作为 AudioManager
class 的扩展。
有没有办法在我的 viewcontroller 中使用它或覆盖这些委托方法,以便能够更改我的 UI 中的属性。
这是扩展名:
extension AudioManager: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
successfully flag: Bool) {
player.stop()
print("Finish Playing")
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer,
error: Error?) {
print(/error?.localizedDescription)
}
}
这是我的 viewcontroller 播放按钮代码:
@IBAction func playButtonTapped(_ sender: UIButton) {
if AudioManager.shared.player == nil {
if recordFilePath != nil {
AudioManager.shared.setupPlayer(URL(string:recordFilePath)!)
}
}
if playButtonIsChecked {
playButtonIsChecked = false
AudioManager.shared.player?.play()
playRecordButton.setBackgroundImage(#imageLiteral(resourceName: "pauseProfileButton"), for: .normal)
} else {
playRecordButton.setBackgroundImage(#imageLiteral(resourceName: "editProfilePlay"), for: .normal)
playButtonIsChecked = true
AudioManager.shared.player?.stop()
}
上面的代码很好,当我调用 player?.stop()
时,AVAudioPlayerDelegate
方法是从 AudioManager
class 调用的,但是当音频文件结束时没有我打电话就停止了 player?.stop
按钮背景图像当然没有改变。
如何在我的 viewcontroller 中使用此委托方法并知道我的音频文件何时结束并停止。
在您的 AudioManager class 中,您可以实现回调(闭包),例如:
var isAudioStopped: ((Bool) -> Void)?
并且您可以在您的扩展程序中调用它,在委托方法中:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
成功标记:Bool) {
player.stop()
isAudioStopped?(true)
print("Finish Playing")
}
然后在您的 ViewController 方法中:
AudioManager.shared.isAudioStopped = {
[weak self] completion in
// if completion the audio is stopped
}
我正在使用一个名为 AudioManager
的 class 你可以找到它作为这个问题的答案 here。
它有委托方法 AVAudioPlayerDelegate
作为 AudioManager
class 的扩展。
有没有办法在我的 viewcontroller 中使用它或覆盖这些委托方法,以便能够更改我的 UI 中的属性。 这是扩展名:
extension AudioManager: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
successfully flag: Bool) {
player.stop()
print("Finish Playing")
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer,
error: Error?) {
print(/error?.localizedDescription)
}
}
这是我的 viewcontroller 播放按钮代码:
@IBAction func playButtonTapped(_ sender: UIButton) {
if AudioManager.shared.player == nil {
if recordFilePath != nil {
AudioManager.shared.setupPlayer(URL(string:recordFilePath)!)
}
}
if playButtonIsChecked {
playButtonIsChecked = false
AudioManager.shared.player?.play()
playRecordButton.setBackgroundImage(#imageLiteral(resourceName: "pauseProfileButton"), for: .normal)
} else {
playRecordButton.setBackgroundImage(#imageLiteral(resourceName: "editProfilePlay"), for: .normal)
playButtonIsChecked = true
AudioManager.shared.player?.stop()
}
上面的代码很好,当我调用 player?.stop()
时,AVAudioPlayerDelegate
方法是从 AudioManager
class 调用的,但是当音频文件结束时没有我打电话就停止了 player?.stop
按钮背景图像当然没有改变。
如何在我的 viewcontroller 中使用此委托方法并知道我的音频文件何时结束并停止。
在您的 AudioManager class 中,您可以实现回调(闭包),例如:
var isAudioStopped: ((Bool) -> Void)?
并且您可以在您的扩展程序中调用它,在委托方法中:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
成功标记:Bool) {
player.stop()
isAudioStopped?(true)
print("Finish Playing")
}
然后在您的 ViewController 方法中:
AudioManager.shared.isAudioStopped = {
[weak self] completion in
// if completion the audio is stopped
}