从 Struct 创建的数组中存储的选定表格单元格中播放音频

Posted

技术标签:

【中文标题】从 Struct 创建的数组中存储的选定表格单元格中播放音频【英文标题】:Playing audio from a selected table cell stored in an array created from a Struct 【发布时间】:2017-05-10 11:43:45 【问题描述】:

我有一个显示在表格中的环境声音列表,当您单击一个时,它会将您带到第二个视图控制器,该控制器显示声音标题,显示图像并给出环境声音的描述并显示播放按钮,我从 Struct 获取了数组中的所有数据,但在播放音频时遇到问题,这是我的代码示例:

我的结构:

import UIKit
import AVFoundation
import Foundation

struct Audio 
    var title: String
    var imageName: UIImage
    var descr: String
    var audioURL: String

我的视图控制器:

import UIKit
import AVFoundation

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

    let audios = [Audio(title: "Bubbling Pools", imageName: UIImage(named: ("1.png"))!, descr: "The stench of life fills the air. Viscous fluid bubbles to the surface in great exhortations of gas and moisture. Something about these pools is familiar..", audioURL: "2_Bubbling_Pools.mp3"),
                  Audio(title: "March Of Faith", imageName: UIImage(named: ("2.png"))!, descr: "The devoted stream into the distance. The dust from their sandaled feet blocks out the sun for miles around. There's no stopping them. They believe.", audioURL: "3_The_March_of_the_Faithful.mp3"),
                  Audio(title: "Solemn Vow", imageName: UIImage(named: ("3.png"))!, descr: "When death is near, and retreat is not an option. Only the bravest have any purchase on the word \"honor\".", audioURL: "4_Solemn_Vow-a.mp3")

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

        return audios.count
    

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        let cell = UITableViewCell()
        let audio = audios[indexPath.row]
        cell.textLabel?.text = audio.title

        return cell
    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        let audio = audios[indexPath.row]

        performSegue(withIdentifier: "segue", sender: audio)

    

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
        if segue.identifier == "segue" 

            let audio = sender as! Audio
            let destinationVC = segue.destination as! SecondViewController
            destinationVC.selectedAudio = audio
        
    

    @IBOutlet weak var tableView: UITableView!

    override var preferredStatusBarStyle: UIStatusBarStyle 
        return .lightContent
    

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    


我的第二个视图控制器:

import UIKit
import AVFoundation

class SecondViewController: UIViewController 

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var artworkImage: UIImageView!
    @IBOutlet weak var descriptionLabel: UILabel!
    @IBOutlet weak var playButton: UIButton!

    var selectedAudio: Audio!

    override var preferredStatusBarStyle: UIStatusBarStyle 
        return .lightContent
    

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        titleLabel.text = selectedAudio.title
        artworkImage.image = selectedAudio.imageName
        descriptionLabel.text = selectedAudio.descr

    

    @IBAction func playButtonPressed(_ sender: Any) 

        playAudio()

    

    var player: AVAudioPlayer?

    func playAudio() 
        let url = Bundle.main.url(forResource: selectedAudio.audioURL, withExtension: "mp3")!

        do 
            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else  return 

            player.prepareToPlay()
            player.play()
         catch let error 
            print(error.localizedDescription)
        
    


【问题讨论】:

你是否到达调用 player.play() 的地方(确保通过输出或断点)? 连接到上述内容:您是否确定 url 是您喜欢的(仅使用文件名似乎有问题)并且文件确实存在? 【参考方案1】:

尝试从audios 中的URL 中删除.mp3URLForResource:withExtension: 单独获取扩展名。

【讨论】:

以上是关于从 Struct 创建的数组中存储的选定表格单元格中播放音频的主要内容,如果未能解决你的问题,请参考以下文章

从表格小部件中的选定单元格中检索单元格数据

保存、保留和检索数组中的选定单元格

如何在 iOS Swift 中将选定的 tableView 单元格保存到数组中?

将 UIButton 标题设置为选定的表格单元格

从表格中隐藏选定的单元格 - Swift4

如何从具有多个数组的字典中获取特定键并存储到放置在表格视图单元格中的字符串中