在Swift中使用Audio Player更改UITableView中按钮单元格的播放暂停状态

ios

我已经使用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()
        }
    }
}
Enricoza

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:

  • Change the icon on cell X
  • Play the Y content

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在UITableView Swift中隐藏空单元格

来自分类Dev

在Swift上创建UITableView单元格

来自分类Dev

使用鼠标按钮更改单元格值-Excel

来自分类Dev

Swift:清除UITableView单元格

来自分类Dev

如何在UITableView中检测单元格选择-Swift

来自分类Dev

UITableView中没有足够的单元格使用

来自分类Dev

Swift-UITableView中的单元格褪色

来自分类Dev

如何在Swift iOS中单击更改单元格中的按钮?

来自分类Dev

在Swift中使用AV Audio Player进行条件音频播放

来自分类Dev

Swift:UIView出现在UITableView单元格中

来自分类Dev

如何在Swift中的表格视图单元格中更改默认的删除按钮?

来自分类Dev

Swift-UITableview:如何在一个单元格上点击以更改另一个单元格中的标签?

来自分类Dev

Swift-在UITableView中添加单元格

来自分类Dev

限制UITableView Swift中每个部分的单元格选择

来自分类Dev

通过使用故事板更改UITableView单元格的高度

来自分类Dev

单元格表视图Swift iOS中的按钮

来自分类Dev

在Swift中使用UITableView填充单元格之前,如何在该数组中添加CKRecords?

来自分类Dev

Swift:如何在动态UITableView中使用静态单元格

来自分类Dev

在Swift的cellForRowAtIndexPath中的UITableView中选择检测单元格

来自分类Dev

在 UITableView 中获取单元格中的文本和按钮 - Swift 3

来自分类Dev

iOS Swift:在单元格中维护切换按钮状态

来自分类Dev

从 UITableView Swift 3 中删除所选单元格下方的单元格

来自分类Dev

在 Swift 中使用 Parse 删除表格视图单元格

来自分类Dev

使用 Swift 更改 UITableView 的选定单元格

来自分类Dev

如何在swift4的tableview单元格中使用按钮获取JSON id

来自分类Dev

如何使用 Swift 处理 tableView 单元格音频播放和暂停按钮?

来自分类Dev

更改 UITableView 中的颜色单元格 以前录制的 Swift

来自分类Dev

如何使用删除按钮删除 UITableView 单元格?

来自分类Dev

我们如何在 uitableview swift 中使用不可见的单元格?

Related 相关文章

  1. 1

    无法在UITableView Swift中隐藏空单元格

  2. 2

    在Swift上创建UITableView单元格

  3. 3

    使用鼠标按钮更改单元格值-Excel

  4. 4

    Swift:清除UITableView单元格

  5. 5

    如何在UITableView中检测单元格选择-Swift

  6. 6

    UITableView中没有足够的单元格使用

  7. 7

    Swift-UITableView中的单元格褪色

  8. 8

    如何在Swift iOS中单击更改单元格中的按钮?

  9. 9

    在Swift中使用AV Audio Player进行条件音频播放

  10. 10

    Swift:UIView出现在UITableView单元格中

  11. 11

    如何在Swift中的表格视图单元格中更改默认的删除按钮?

  12. 12

    Swift-UITableview:如何在一个单元格上点击以更改另一个单元格中的标签?

  13. 13

    Swift-在UITableView中添加单元格

  14. 14

    限制UITableView Swift中每个部分的单元格选择

  15. 15

    通过使用故事板更改UITableView单元格的高度

  16. 16

    单元格表视图Swift iOS中的按钮

  17. 17

    在Swift中使用UITableView填充单元格之前,如何在该数组中添加CKRecords?

  18. 18

    Swift:如何在动态UITableView中使用静态单元格

  19. 19

    在Swift的cellForRowAtIndexPath中的UITableView中选择检测单元格

  20. 20

    在 UITableView 中获取单元格中的文本和按钮 - Swift 3

  21. 21

    iOS Swift:在单元格中维护切换按钮状态

  22. 22

    从 UITableView Swift 3 中删除所选单元格下方的单元格

  23. 23

    在 Swift 中使用 Parse 删除表格视图单元格

  24. 24

    使用 Swift 更改 UITableView 的选定单元格

  25. 25

    如何在swift4的tableview单元格中使用按钮获取JSON id

  26. 26

    如何使用 Swift 处理 tableView 单元格音频播放和暂停按钮?

  27. 27

    更改 UITableView 中的颜色单元格 以前录制的 Swift

  28. 28

    如何使用删除按钮删除 UITableView 单元格?

  29. 29

    我们如何在 uitableview swift 中使用不可见的单元格?

热门标签

归档