我一般都不熟悉Swift和编程。当按下一个单元格时,我想进入音频播放器视图并播放选定的音频。现在我只播放一个音频,选择哪个单元都没关系。任何帮助将不胜感激,在此先感谢。

class TableViewController: UITableViewController {
    var audioPlayer:AVAudioPlayer = AVAudioPlayer()
    var pictures = ["AlbertEinstein.jpg","dreamBig.jpg", "LionFearless.jpg", "MLK.jpg"]
    var names = ["Einstein", "DreamBig", "LionFearless", "MartinLutherKing",]

    let audioPath = Bundle.main.path(forResource: "Music", ofType: "mp3")
    let newAudioPath = Bundle.main.path(forResource: "AchievingAnythingYouWant", ofType: "m4a")

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

        cell.tittleLabel!.text = names[indexPath.row]
        cell.pictureImage.image = UIImage(named: pictures[indexPath.row])

        return cell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            let detailVC = segue.destination as! PlayerViewController
            let myIndexPath = self.tableView.indexPathForSelectedRow!

            //path to audio file
            let row = newAudioPath
            detailVC.sendData1 = row!

            let row2 = audioPath
            //variable in deatilVC to hold audio
            detailVC.sendData2 = row2!
        }
    }
}

最佳答案

因此,当前用于备份表格单元格的“模型”仅包含歌曲的名称。正确的方法是创建一个新类,将其作为模型对象。

例如:

class SongModel {
    let name: String
    let imagePath: String
    let audioPath: String

    init(name: String, imagePath: String, audioPath: String) {
        self.name = name
        self.imagePath = imagePath
        self.audioPath = audioPath
    }
}


然后,您可以使用该类的实例来支持TableViewController:

// Instead of:
// var names = ["Einstein", "DreamBig", "LionFearless", "MartinLutherKing"]

var model = [SongModel]()


然后在viewDidLoad()中,可以使用SongModel实例加载模型数组。这是一个非常简单的模型示例。

现在在prepareFor(Segue:_, Sender:_)中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showDetail" {

    let detailVC = segue.destination as! PlayerViewController

    let myIndexPath = self.tableView.indexPathForSelectedRow!
        // Pass reference to selected model object (youll have to add
        // the property to the detailVC)
        detailVC.selectedSong = model[myIndexPath.row]
        // now your detail vc will have access to the model
    }
}


我也鼓励您使用if-let或guard语句进行解包,而不是用optionalValue!强制解包。像这样:

if let detailVC = segue.destination as? PlayerViewController {
   if let myIndexPath = self.tableView.indexPathForSelectedRow {
      detailVC.selectedSong = model[myIndexPath.row]
   }
}


注意as?这是一个可选的取消包装,当与if-let或guard-else结合使用时,允许取消包装失败而不会崩溃您的应用程序。

您还必须调整其他tableview委托/数据源方法以说明新模型。

关于ios - Swift 3尝试将音频路径/音频文件传递到音频播放器VC,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43765507/

10-11 22:25
查看更多