I have some songs and showing in 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.
Change your playSong function to call audioPlayer.stop() before creating the new AVAudioPlayer. That should stop the previous sound.
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
}