[英]Receive delegate method in different class
I'm developing a music player by using Jukebox library. 我正在使用Jukebox库开发音乐播放器。 It has a delegation which warn elements when songs duration/ current time etc. changed. 它具有一个委派,可以在歌曲持续时间/当前时间等发生变化时警告元素。 In Jukebox example, all of codes are in ViewController. 在自动存储塔示例中,所有代码都在ViewController中。 Therefore, for example when current time changes, it also changes sliders value. 因此,例如,当当前时间更改时,它也会更改滑块值。 I want to create a player class which is usable by more than one ViewController. 我想创建一个供多个ViewController使用的播放器类。 When I put delegate methods inside this model class. 当我将委托方法放入此模型类中时。 How can I reach them in ViewController classes? 如何在ViewController类中找到它们?
First Screenshot is first ViewController which has delegates methods and initialize player. 第一个屏幕截图是第一个ViewController,它具有委托方法和初始化播放器。
MainViewController I need to reach that delegate methods in secondViewController to update the UI. 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)")
}
If you want to use music playerview from anywhere 如果您想从任何地方使用音乐播放器视图
you should write player class all code for playing files should be in this and just need to pass array of list from anywhere and it can played.and use custom delegation for appropriate response in playerview 您应该编写播放器类,用于播放文件的所有代码都应该在此文件中,并且只需要从任何地方传递列表数组就可以播放。并使用自定义委托在播放器视图中进行适当的响应
for UI make a viewcontroller and u can show it from any class so you can call from anywhere in the app 为UI创建一个viewcontroller,您可以从任何类中显示它,因此您可以从应用程序中的任何位置调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.