【问题标题】:Change Play Pause state of button cell in UITableView with Audio Player in Swift使用 Swift 中的音频播放器更改 UITableView 中按钮单元的播放暂停状态
【发布时间】: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


    【解决方案1】:

    您可以将当前播放单元格的索引保存在一个属性中(我们称之为 X)。然后,在另一个单元格中点击该按钮(我们称之为 Y),您可以:

    • 更改单元格 X 上的图标
    • 播放 Y 内容

    这将导致状态再次更改,这次是单元格 Y,因此您将更改单元格 Y 的图标。

    作为旁注,我想警告您,您可能应该将所有单元格的状态保存在某种数据结构(如数组)中,然后使用它来使 cellForRowAtIndexPath 中的单元格出列,否则,当单元格开始超出您在屏幕上看到的内容时,您可能会遇到一些问题。 我不知道您是否已经这样做了,但希望避免您以后遇到其他麻烦。

    编辑:

    假设 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()
            }
        }
    }
    

    【讨论】:

    • @encricoza - 你能分享一下这个逻辑的代码吗?正如我已经尝试过但可以做到的那样
    • @ios 很难做到没有更多的代码。如果您与我分享 viewController 的代码,我可以尝试为您编辑它
    • 我已经更新了我的问题。请看看@enricoza
    • @enricoza- 此代码不会重置其他按钮状态。例如:- 如果我首先单击索引 0 处的按钮,然后单击索引 1 处的按钮,它也应该更改索引 0 的图像(播放图像)。
    • @ios 我添加的部分应该正是您想要的。假设 controller.player.currentIndex 是前一个索引,一旦你点击一些不同的原始数据,它会重置 currentIndex 上的图像,然后使用下面的 jumpToItem 调用更改当前索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2013-08-22
    • 2015-04-03
    相关资源
    最近更新 更多