2017-08-03 72 views
0

我正在使用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

请参阅[this](https://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers)示例委托实施。 – Akhilrajtr

+0

这不是我正在寻找。在我的例子中,当歌曲当前时间改变时,委托方法每次都在调用。我需要在secondviewcontroler – eonr

+0

@eonr中查看该库中的示例 –

回答

2

如果你想用音乐playerview从任何地方

  1. 你应该写播放器类,用于播放文件的所有代码应该是在这一点,只需要在任何地方通过列表的阵列,它可以played.and使用自定义代表团在playerview适当的响应

  2. 对UI进行视图 - 控制和u可以从任何类表现出来,所以你可以从任何地方在应用调用

+0

我编辑了这个问题。我还需要在secondviewcontroller中访问该委托方法。我怎样才能做到这一点? – eonr

+0

@eonr为什么你需要第二个viewcontroller中的所有委托方法?这又不是音乐Playerview吗? –

+0

因为secondViewController中有一个显示当前时间的滑块。如何在没有委托方法的情况下更新? – eonr

相关问题