didSelectRowAt indexPath播放音频

时间:2017-08-07 02:12:00

标签: ios swift uitableview audio-player

我需要你的帮助!我正在使用Jukebox API开发和音频播放器应用程序。我正在使用didSelectRowAt indexPath来播放当前流。我的代码可以正常工作,但是在单击单元格的情况下播放另一个代码之前它不会停止流。我将不胜感激任何帮助!谢谢!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        jukebox = Jukebox(delegate: self as? JukeboxDelegate, items: [
            JukeboxItem(URL: NSURL(string:audio_url[indexPath.row])! as URL)
            ])

      jukebox.play()

 }

1 个答案:

答案 0 :(得分:1)

试试这个,看看

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            stopJukeBox()
            jukebox = Jukebox(delegate: self as? JukeboxDelegate, items: [
                JukeboxItem(URL: NSURL(string:audio_url[indexPath.row])! as URL)
                ])
            playJukeBox()

}

func stopJukeBox(){
  if jukebox != nil {
   jukebox.stop()
  }
}

func playJukeBox(){
  if jukebox != nil {
    jukebox.play()
   }
}

或者您可以直接在didSelect功能中处理播放和停止

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

   // Stop
   if jukebox != nil {
    jukebox.stop()
   }


            jukebox = Jukebox(delegate: self as? JukeboxDelegate, items: [
                JukeboxItem(URL: NSURL(string:audio_url[indexPath.row])! as URL)
                ])

   // play
   if jukebox != nil {
    jukebox.play()
   }

}
相关问题