【发布时间】:2020-02-01 05:34:54
【问题描述】:
我已经使用https://github.com/jorgenhenrichsen/SwiftAudio 这个库在 UITableView 中添加了音频播放器。播放器工作正常,但我在更改 UITableView 中的播放暂停按钮图像时遇到问题。
这个库有一个方法可以给出音频的状态(播放、就绪、缓冲区、失败):
func setPlayButtonState(forAudioPlayerState state: AudioPlayerState) ()
此方法工作正常,并在添加到视图控制器时在播放音频时更改按钮图像。但我必须管理 UITableViewCell 中的状态。
为了更改单元格的按钮图像,我使用了这种方法:
func setPlayButtonState(forAudioPlayerState state: AudioPlayerState) () {
let cell = YOUR_TABLEVIEW_REF.cellForRow(at: IndexPath(row: YOUR_ROW_FOR_LANGUAGE_CELL, section: YOUR_SECTION_FOR_LANGUAGE_CELL)) as! AppSettingViewCell
cell.playButton.image = UIImage(named: "IMAGE NAME")
}
它适用于一个索引。但是我需要做的是在单击其他行时将其更改为再次停止状态。
和我的配置单元方法:
func configurePlaylistCell (tableView: UITableView, cell: PlaylistCell, controller: AudioController, indexPath: IndexPath){
cell.playPauseButton.tag = indexPath.row + 100
cell.playButtonPressed = { [weak self] (sender: UIButton) in
self?.indexPathForCell = IndexPath(row: sender.tag - 100 , section: 0)
if !controller.audioSessionController.audioSessionIsActive {
try? controller.audioSessionController.activateSession()
}
print (controller.player.currentIndex)
if let item = controller.player.currentItem {
if (indexPath.row == 0) {
try? controller.player.load(item: item, playWhenReady: true)
}else {
try? controller.player.jumpToItem(atIndex: sender.tag - 100, playWhenReady: true)
}
}
else {
controller.player.togglePlaying()
}
}
}
【问题讨论】:
标签: ios swift uitableview audio