ios - UITableView 播放音频文件不正确

标签 ios uitableview swift audio avfoundation

我正在制作一个具有在 Swift 中播放音频功能的应用程序。所有音频文件都显示在 UITableView 中,当我单击该行时,必须播放相应的音频文件。问题是,例如,当我单击第一行时它不播放音频,但如果我单击第二行,则播放第一行的音频。
Image of tableView

代码如下:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
    var mp3file: AnyObject = mp3sPaths[indexPath.row]
    cell.textLabel?.text = mp3file.lastPathComponent
    return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var mp3file = mp3sPaths[indexPath.row] as NSString
    var mp3URL: NSURL = NSURL.fileURLWithPath(mp3file)!
    var error: NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: mp3URL, error: &error)
    audioPlayer?.stop()
    audioPlayer?.prepareToPlay()
    audioPlayer?.play()
}

最佳答案

当您
应该使用SelectRowAtIndexPath时,您却使用了DeselectRowAtIndexPath

关于ios - UITableView 播放音频文件不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253412/

相关文章:

ios - 如何在 OpenGL ES 2.0 中使用可分离滤镜着色器?

ios - Facebook SDK for iOS 使用开放图谱 API 贴在墙上

ios - 我们怎样才能停止线程

ios - 在uitableview中识别许多单元格

ios - 如何以编程方式处理 ios 中 Google 登录的重定向 uri?

swift - 在 Swift 中处理 cmd + q

ios - 使用 iOS 8 应用程序组在两个应用程序之间共享数据(使用 NSUserDefaults)

android - 从 Apple Store 安装的应用程序会自动创建主屏幕快捷方式图标吗?

ios - 从模式中选择数据后如何在 UITableViewCell 中刷新 UITextField

ios - 类数组中的过滤结果(Swift 3)