[swift]当 AVAudioPlayer 完成播放时切换表细胞图像

标签: ios Swift
发布时间: 2017/3/6 23:14:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有表,每个单元格有一个播放按钮。 播放按钮是 UIButton ,播放和暂停的图像之间切换。 当按下播放/暂停按钮时, AVAudioPlayer 播放/暂停与单元格关联的音频文件。 到目前为止,我已经切换非常好,而且与音频播放器同步的图像。 然而,当音频播放完,我想要切换回其默认状态-播放按钮图像的图像。

//View controller containing tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.audioTable.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! AudioCell

    let track = self.sectionTracks[indexPath.row]

    cell.playButton.addTarget(self, action: #selector(HomeController.playPausePressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    var playButtonImage = UIImage(named: "TableViewCellPlayButton84.jpg")
    var pauseButtonImage = UIImage(named: "TableViewCellPauseButton84.jpg")
    playButtonImage = playButtonImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    pauseButtonImage = pauseButtonImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

    cell.playButton.setImage(playButtonImage, forState: UIControlState.Normal)
    cell.playButton.setImage(pauseButtonImage, forState: UIControlState.Selected)
    cell.playButton.selected = false

    return cell
}

func playPausePressed(sender:UIButton) {

    let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.audioTable)
    let cellIndexPath = self.audioTable.indexPathForRowAtPoint(pointInTable)
    let track = self.sectionTracks[cellIndexPath!.row]

     //Handle AVAudioPlayer play/pause functionality
}

//Custom tableview cell file
@IBAction func playPausePressed(sender: UIButton!) {

    if (sender.selected)
    {
        sender.selected = false
    }
    else{
        sender.selected = true
    }
}

解决方法 1:

你可以订阅 AVAudioPlayer 的委托,然后检查时音频完整。你可以在一段时间或多播放多个文件?如果您可以播放多个要跟踪所有播放的音频,然后切换基于委托解雇时,哪一行玩家与相关联的行上的按钮。如果你只可以玩一个然后当用户开始播放音频只,保存该行和音频结束时切换按钮为该行。

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/

private var xoAssociationKey: UInt8 = 0

extension AVAudioPlayer {
    var name: String! {
        get {
            return objc_getAssociatedObject(self, &xoAssociationKey) as? String
        }
        set(newValue) {
            objc_setAssociatedObject(self, &xoAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }
}

它可能有助于使上述保存到 AVAudioPlayer 实例名称的扩展。你可以然后跟踪的播放的音频和可变字典中单元格的索引路径名称。你什么时候回的委托函数调用 optional func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) 你可以查字典为 player.name ,将给你回的索引路径到该按钮的单元格。获取单元格表视图中,然后切换按钮的选定的状态。

编辑

为单个单元格打一段时间,你可以将变量添加到您的类var playingAudioIndexPath:NSIndexPath?

当启动音频播放设置为该按钮被单击的单元格索引路径。

然后在播放完事件只抓单元格和切换按钮

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
                             successfully flag: Bool) {
    guard let indexPath = self.playingAudioIndexPath,
        cell = self.table.cellForRowAtIndexPath(indexPath) as? AudioCell else {
         return;
     }

     cell.playButton.selected = false
     self.playingAudioIndexPath = nil
}

DMCApps

官方微信
官方QQ群
31647020