ios - 如何在音频停止播放后自动更改按钮的背景图像?

标签 ios swift

我正在使用xcode 10.2.1开发语音记录应用程序...录制音频后,音频将显示在表格 View 中。在每个单元格内,我都有一个播放按钮,按下该按钮后应切换到暂停。音频结束后,它应该重新开始播放。我怎样才能快速实现这一目标?

我在堆栈上尝试了很多答案,但没有一个符合我的要求。

func btnPlayPressed(index: Int) {
        let audio = self.audios[index]

        if audio.isPlaying {
            AudioManager.sharedInstance.stopSound()
            self.audios[index].isPlaying = false
        } else {
            AudioManager.sharedInstance.playSound(fileName: audio.fileName)
            self.audios[index].isPlaying = true
        }
    }

最佳答案

您可以扩展 AVAudioPlayerDelegate 并覆盖下面的函数。然后这个函数会让你知道音频已经结束了。

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    print("Finish")
}

关于ios - 如何在音频停止播放后自动更改按钮的背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072072/

相关文章:

ios - Swift 范围数组导致问题

objective-c - Facebook 好友列表图 API

ios - 应用商店连接拒绝构建,因为缺少 NSBluetoothAlwaysUsageDescription key

c# - 将 InputAccessoryView 设置为 UISearchController

ipad - UIPinchGestureRecognizer 将捏合 View 定位在两根手指之间

ios - 删除并恢复AVPlayer以启用后台视频播放

ios - 当与导航 View Controller 一起使用时,popoverPresentationController 为零

Swift Index 在 cellForRowAtIndexPath 处超出范围,两次调用 getDataInBackground

swift - MemoryLayout<T>.size/stride/alignment 是编译时间吗?

android - Flutter 有状态小部件,其子级未更新状态