【问题标题】:How to stop song when click again on table view and play another song when click another cell如何在再次单击表格视图时停止歌曲并在单击另一个单元格时播放另一首歌曲
【发布时间】:2018-09-28 09:50:40
【问题描述】:

我有一些歌曲并在 tableview 中显示。当我按下单元格时,它正在播放歌曲。当我触摸同一个单元格时我想停下来,但如果我触摸另一个单元格,我想让它继续播放新歌。

extension SoundsViewController: UITableViewDataSource,UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return songs.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = myTableView.dequeueReusableCell(withIdentifier: "soundsCell") as! SoundsTableViewCell
    cell.soundPicture.image = UIImage(named: "cell1")
    cell.soundLabel.text = songs[indexPath.row]
    cell.playStopImage.image = UIImage(named: "play")

    if selectedItemIndex == indexPath.row {
      cell.playStopImage.image = UIImage(named: "pause")
    }
    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 120
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedItemIndex = indexPath.row
    playSong(song: songs[indexPath.row], selectedItemIndex: selectedItemIndex!)
    self.myTableView.reloadData()
}

}

func playSong(song: String, selectedItemIndex: Int){
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: song, ofType: "mp3")!))
        audioPlayer.numberOfLoops = -1
        audioPlayer.play()
        var audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayback)
        }catch{
            print(error)
        }
    }catch{
        print(error)
    }
}

【问题讨论】:

    标签: ios swift uitableview avaudioplayer


    【解决方案1】:

    大概你的audioPlayer 是你的视图控制器的一个实例变量。

    在创建新的 AVAudioPlayer 之前,将您的 playSong 函数更改为调用 audioPlayer.stop()。这应该会停止之前的声音。

    编辑:

    将这样的函数添加到您的视图控制器:

    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if let selectedRow = tableView.indexPathForSelectedRow {
            print("Deselecting row \(selectedRow.row). Stop playing sound")
            audioPlayer.stopPlaying()
            tableView.deselectRow(at: indexPath, animated: false)
            if selectedRow == indexPath {
                return nil
            }
        }
        print("Selecting row \(indexPath.row). Start playing sound")
        playSong(song: songs[indexPath.row], selectedItemIndex: selectedItemIndex!)
        return indexPath
    }
    

    【讨论】:

    • 你能给我举个例子吗?
    • 它在你的 playSong 函数中添加了一行代码。如果你不能解决这个问题,你需要回到基础。
    • 这不是解决方案。我想在触摸同一个单元格时停下来。但是,如果我在播放时触摸另一个单元格,它应该会不停地改变。
    • 顺便说一下 audioPlayer.isPlaying 总是假的?
    • 跟踪选择了哪个单元格。如果用户点击已选择的单元格,请致电audioPlayer.stop()。如果他们更改选定的单元格,请调用您的 playSong() 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多