【发布时间】:2021-09-27 23:42:07
【问题描述】:
我有一个音乐应用程序,它有一个带有自定义单元格的表格视图,其中的标签和信息已经在我的 JSON 文件中:
[
{
"loop_name" : "Come Back",
"Instrument" :"Guitar",
"loop_link" : "local link to wav file", (need to figure out)
"producer" : "Bernulli"
}
]
主要问题是,我不知道如何将本地音频文件实现为 JSON 文件,然后将此数据加载到我的应用程序中。这是我的带有标签的自定义单元格(已经在 JSON 文件和按钮中,应该在表格视图的每一行中播放 JSON 文件中的歌曲):
import UIKit
import AVFoundation
protocol CustomSongDelegate: AnyObject {
func btnUseTap(cell: CustomLoopsCell)
}
class CustomSongCell: UITableViewCell {
weak var delegate: CustomSongDelegate?
var songs: [String] = []
var audioPlayer = AVAudioPlayer()
@IBOutlet weak var loopNameLabel: UILabel!
@IBOutlet weak var producerLabel: UILabel!
@IBOutlet weak var instrumentLabel: UILabel!
@IBOutlet weak var playButtonOutlet: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func btnUse(_ sender: Any) {
delegate?.btnUseTap(cell: self)
}
}
【问题讨论】:
-
其实JSON只需要文件名,你可以用
Bundle.main.url(forResource:”fileName”, withExtension:”mp3”)获取URL或者用FileManager从文档目录中获取。 -
@vadian 所以在 JSON 文件中我应该写歌名还是什么?
-
磁盘上对应文件的名称。
-
@vadian 例如:songname.wav 并使其看起来像 json:"song_name" : "songname.wav" ???
标签: ios json swift audio avfoundation