我已经使用https://github.com/jorgenhenrichsen/SwiftAudio这个库在UITableView中添加了音频播放器。播放器工作正常,但在更改UITableView中的播放暂停按钮图像时遇到问题。
这个库有一个提供音频状态的方法(播放,就绪,缓冲区,失败):
func setPlayButtonState(forAudioPlayerState state: AudioPlayerState) ()
此方法效果很好,并且在将音频添加到View Controller中时在播放音频时更改“按钮图像”。但是我必须管理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")
}
它适用于一个索引。但是我需要做的是单击其他行后再次将其更改为停止状态。
and my configure cell method:
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()
}
}
}
You can just save in a property the index of the current playing cell (let's call it X). Than, on tap of that button from another cell (let's call it Y) you can:
That will cause the state change again, this time for cell Y, so you will change the icon of cell Y.
As a side note I want to warn you that probably you should save the state of all cells in some sort of data structure (like an array) and then use that to dequeue cells in cellForRowAtIndexPath, otherwise you could get into some issue when cells start to be more than what you can see in the screen. I don't know if you did that already, but wanted to avoid you from ending up in some other trouble later.
EDIT:
假设controller.player.currentIndex是正在播放的单元格的(可选)索引,则看起来像这样:
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 let index = controller.player.currentIndex, let cell = tableView.cellForRow(at: IndexPath(row: index, section: YOUR_SECTION_FOR_LANGUAGE_CELL)) as! AppSettingViewCell {
cell.playButton.image = UIImage(named: "IMAGE NAME")
}
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()
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句