这是我的 json 文件:
{
"duration": 198,
"file": "https://grepp-programmers-challenges.s3.ap-northeast-2.amazonaws.com/2020-flo/music.mp3",
"lyrics": "[00:16:200]we wish you a merry christmas [00:18:300]we wish you a merry christmas [00:21:100]we wish you a merry christmas [00:23:600]and a happy new year [00:26:300]we wish you a merry christmas [00:28:700]we wish you a merry christmas [00:31:400]we wish you a merry christmas [00:33:600]and a happy new year [00:36:500]good tidings we bring [00:38:900]to you and your kin [00:41:500]good tidings for christmas [00:44:200]and a happy new year [00:46:600]Oh, bring us some figgy pudding [00:49:300]Oh, bring us some figgy pudding [00:52:200]Oh, bring us some figgy pudding [00:54:500]And bring it right here [00:57:000]Good tidings we bring [00:59:700]to you and your kin [01:02:100]Good tidings for Christmas [01:04:800]and a happy new year [01:07:400]we wish you a merry christmas [01:10:000]we wish you a merry christmas [01:12:500]we wish you a merry christmas [01:15:000]and a happy new year [01:17:700]We won't go until we get some [01:20:200]We won't go until we get some [01:22:800]We won't go until we get some [01:25:300]So bring some out here [01:29:800]연주 [02:11:900]Good tidings we bring [02:14:000]to you and your kin [02:16:500]good tidings for christmas [02:19:400]and a happy new year [02:22:000]we wish you a merry christmas [02:24:400]we wish you a merry christmas [02:27:000]we wish you a merry christmas [02:29:600]and a happy new year [02:32:200]Good tidings we bring [02:34:500]to you and your kin [02:37:200]Good tidings for Christmas [02:40:000]and a happy new year [02:42:400]Oh, bring us some figgy pudding [02:45:000]Oh, bring us some figgy pudding [02:47:600]Oh, bring us some figgy pudding [02:50:200]And bring it right here [02:52:600]we wish you a merry christmas [02:55:300]we wish you a merry christmas [02:57:900]we wish you a merry christmas [03:00:500]and a happy new year"
}
然后我在我的项目中解析了 musicManager 中的数据。 我想在 Main.Storyboard 上显示歌词。
我正在使用 AVPlayer 流式传输音频文件。
我通过musicData解析musicManager中的Json到musicModel 那么我愿意更改 UIViewController 中 textView 中的 textLabel。
这里是我的 musicManager 相关代码。
import Foundation
protocol MusicManagerDelegate {
func didUpdateMusic(music: MusicModel)
}
struct MusicManager {
let urlString = "https://grepp-programmers-challenges.s3.ap-northeast-2.amazonaws.com/2020-flo/song.json"
var delegate: MusicManagerDelegate?
func performRequest(urlString: String) {
if let url = URL(string: urlString) {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
if let music = self.parseJSON(musicData: safeData) {
delegate?.didUpdateMusic(music: music)
}
}
}
task.resume()
}
}
func parseJSON(musicData: Data) -> MusicModel? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(MusicData.self, from: musicData)
let singer = decodedData.singer
let album = decodedData.album
let title = decodedData.title
let imageURL = decodedData.image
let fileURL = decodedData.file
let duration = decodedData.duration
let lyrics = decodedData.lyrics
let music = MusicModel(currentSinger: singer, currentAlbum: album, currentTitle: title, currentImage: imageURL, currentFile: fileURL, currentDuration: duration, currentLyrics: lyrics)
return music
} catch {
print(error)
return nil
}
}
}
音乐数据
struct MusicData: Decodable {
var singer: String
var album: String
var title: String
var image: String
var file: String
var duration: Int
var lyrics: String
}
音乐模型
struct MusicModel {
let currentSinger: String
let currentAlbum: String
let currentTitle: String
let currentImage: String
let currentFile: String
let currentDuration: Int
let currentLyrics: String
}