2017-08-07 56 views
2

我需要你的幫助!我正在使用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() 

} 

回答

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

} 
+0

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