我正在使用Jukebox库开发音乐播放器。它有一个代表团,当歌曲持续时间/当前时间等改变时,它会警告元素。在Jukebox示例中,所有代码都在ViewController中。因此,例如当当前时间改变时,它也改变滑块值。我想创建一个可由多个ViewController使用的播放器类。当我将委托方法放在这个模型类中时。如何在ViewController类中访问它们?
第一个截图是第一个ViewController,它具有委托方法和初始化播放器。
MainViewController 我需要在secondViewController中访问该委托方法以更新UI。
class ViewController: UIViewController, JukeboxDelegate {
var jukebox : Jukebox!
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
// begin receiving remote events
UIApplication.shared.beginReceivingRemoteControlEvents()
// configure jukebox
jukebox = Jukebox(delegate: self, items: [
JukeboxItem(URL: URL(string: "http://www.kissfm.ro/listen.pls")!),
JukeboxItem(URL: URL(string: "http://www.noiseaddicts.com/samples_1w72b820/2514.mp3")!),
JukeboxItem(URL: URL(string: "http://www.noiseaddicts.com/samples_1w72b820/2958.mp3")!)
])!
}
// MARK:- JukeboxDelegate -
func jukeboxDidLoadItem(_ jukebox: Jukebox, item: JukeboxItem) {
print("Jukebox did load: \(item.URL.lastPathComponent)")
}
func jukeboxPlaybackProgressDidChange(_ jukebox: Jukebox) {
if let currentTime = jukebox.currentItem?.currentTime, let duration = jukebox.currentItem?.meta.duration {
let value = Float(currentTime / duration)
slider.value = value
populateLabelWithTime(currentTimeLabel, time: currentTime)
populateLabelWithTime(durationLabel, time: duration)
} else {
resetUI()
}
}
func jukeboxStateDidChange(_ jukebox: Jukebox) {
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.indicator.alpha = jukebox.state == .loading ? 1 : 0
self.playPauseButton.alpha = jukebox.state == .loading ? 0 : 1
self.playPauseButton.isEnabled = jukebox.state == .loading ? false : true
})
if jukebox.state == .ready {
playPauseButton.setImage(UIImage(named: "playBtn"), for: UIControlState())
} else if jukebox.state == .loading {
playPauseButton.setImage(UIImage(named: "pauseBtn"), for: UIControlState())
} else {
volumeSlider.value = jukebox.volume
let imageName: String
switch jukebox.state {
case .playing, .loading:
imageName = "pauseBtn"
case .paused, .failed, .ready:
imageName = "playBtn"
}
playPauseButton.setImage(UIImage(named: imageName), for: UIControlState())
}
print("Jukebox state changed to \(jukebox.state)")
}
func jukeboxDidUpdateMetadata(_ jukebox: Jukebox, forItem: JukeboxItem) {
print("Item updated:\n\(forItem)")
}
答案 0 :(得分:2)
如果您想从任何地方使用音乐播放器视图
你应该编写播放器类所有播放文件的代码都应该在这里,只需要从任何地方传递列表数组,它可以播放。并在playerview中使用自定义委托进行适当的响应
用于制作视图控制器,你可以从任何课程中显示它,这样你就可以在应用程序的任何地方进行调用