Swift 3试图将音频路径/音频文件传递到Audio Player VC



我是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结合使用时,可以允许UNAPRAP失败而不会崩溃您的应用程序。

您还必须调整其他TableView委托/数据源方法来考虑新模型。

最新更新