In cellForRowAt file
cell.blockPausePlay = { () -> Void in
defer {
DispatchQueue.main.async {
self.tblChatMessages?.reloadData()
}
}
self.isAudioPlaying = !self.isAudioPlaying
self.indexPath = indexPath
print(self.indexPath)
cell.rightSlider?.setValue(0.0, animated: true)
if self.audioPlayer != nil && self.timer != nil {
self.audioPlayer?.currentTime = 0.0
self.audioPlayer?.stop()
self.audioPlayer = nil
self.timer?.invalidate()
self.timer = nil
}
if self.isAudioPlaying {
self.preIndexPath = indexPath
guard let mediaUrl = URL(string: obj["VoiceMedia"] as! String) else {
return
}
do {
try AVAudioSession.sharedInstance().setMode(.default)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
let data = try Data(contentsOf: mediaUrl)
self.audioPlayer = try AVAudioPlayer(data: data)
print("audio duration is: \(Float(self.audioPlayer!.duration))")
self.audioPlayer?.currentTime = TimeInterval(0.0)
cell.rightSlider?.minimumValue = 0.0
cell.rightSlider?.maximumValue = Float(self.audioPlayer!.duration)
cell.rightSlider?.isContinuous = true
self.audioPlayer?.prepareToPlay()
self.audioPlayer?.play()
// delay(3) {
// self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.trackRightAudio), userInfo: nil, repeats: true)
// }
self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.trackRightAudio), userInfo: nil, repeats: true)
} catch let error {
print(error.localizedDescription)
self.isAudioPlaying = false
self.indexPath = nil
cell.rightSlider?.setValue(0.0, animated: true)
if self.audioPlayer != nil && self.timer != nil {
self.audioPlayer?.currentTime = 0.0
self.audioPlayer?.stop()
self.audioPlayer = nil
self.timer?.invalidate()
self.timer = nil
}
}
} else {
if self.preIndexPath != self.indexPath {
cell.rightSlider?.setValue(0.0, animated: true)
if self.audioPlayer != nil && self.timer != nil {
self.audioPlayer?.currentTime = 0.0
self.audioPlayer?.stop()
self.audioPlayer = nil
self.timer?.invalidate()
self.timer = nil
}
if obj["Sender_ID"] as! String == "\(UserInfo.savedUser()?.user_id ?? 0)" {
let audioCell = self.tblChatMessages?.cellForRow(at: self.indexPath!) as! RightVoiceCell
if audioCell.blockPausePlay != nil {
audioCell.blockPausePlay!()
}
} else {
let audioCell = self.tblChatMessages?.cellForRow(at: self.indexPath!) as! LeftVoiceCell
if audioCell.blockPausePlay != nil {
audioCell.blockPausePlay!()
}
}
} else {
cell.rightSlider?.setValue(0.0, animated: true)
self.indexPath = indexPath
if self.audioPlayer != nil && self.timer != nil {
self.audioPlayer?.currentTime = 0.0
self.audioPlayer?.stop()
self.audioPlayer = nil
self.timer?.invalidate()
self.timer = nil
}
}
}
}
in cell file
var blockPausePlay: (() -> ())?
@IBAction func onClickBtnPausePlay(_ sender: UIButton) {
if self.blockPausePlay != nil {
self.blockPausePlay!()
}
}
We are using block code pattern to achieve this functionality of playing audio. we need to write the body of block code in cellForRowAt and we are implementing the block code in the cell file
And audio is not playing until we upload another audio in chat. after we upload a new audio in the chat the whole functionality is working properly.