gpt4 book ai didi

ios - Swift 3 尝试将音频路径/音频文件传递给音频播放器 VC

转载 作者:行者123 更新时间:2023-11-30 12:29:27 26 4
gpt4 key购买 nike

我对 swift 和一般编程都很陌生。当按下单元格时,我想进入音频播放器 View 并播放选定的音频。目前我只播放一个音频,选择哪个单元格并不重要。任何帮助将不胜感激,提前致谢。

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!
}
}
}

最佳答案

因此,目前您用于支持表格 View 单元格的“模型”仅包含歌曲的名称。执行此操作的正确方法是创建一个新类,该类将作为您的模型对象。

例如:

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 委托(delegate)/数据源方法以适应新模型。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com