didSelectRowAt indexPath播放音频

问题描述:

我需要你的帮助!我正在使用Jukebox API开发音频播放器应用程序。我正在使用didSelectRowAt indexPath播放当前流。我的代码工作,但它不会停止流之前播放另一个单元格被点击。我会很感激任何帮助!谢谢!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() 

} 

试试这个,看看

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() 
    } 

} 
+0

完美地工作!非常感谢! – theappledev