简体   繁体   English

再次单击表格视图时如何停止歌曲,以及单击另一个单元格时播放另一首歌曲

[英]How to stop song when click again on table view and play another song when click another cell

I have some songs and showing in tableview. 我有一些歌曲,并在tableview中显示。 When I press cell it's play the song. 当我按单元格时,会播放歌曲。 I want to stop when I touch the same cell but if I touch another cell I want to make it still playing with new song. 当我触摸相同的单元格时我想停下来,但是如果我触摸另一个单元格,我想让它仍在播放新歌曲。

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

presumably your audioPlayer is an instance variable of your view controller. 大概您的audioPlayer是视图控制器的实例变量。

Change your playSong function to call audioPlayer.stop() before creating the new AVAudioPlayer. 在创建新的AVAudioPlayer之前,将playSong函数更改为调用audioPlayer.stop()。 That should stop the previous sound. 那应该停止以前的声音。

EDIT: 编辑:

Add a function like this to your view controller: 将这样的函数添加到视图控制器中:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM