我是菜鸟。我有一些编程经验,但是对Swift 4和Xcode 9.2还是陌生的。
我一直在学习教程和阅读。我已经实现了许多Music Player示例之一。我将其扩展为显示歌曲元数据。效果很好。
我已经实现了AVAudioPlayerDelegate audioPlayerDidFinishPlaying()函数。在此功能中,我将获取下一首歌曲的元数据,设置视图变量,然后播放歌曲。歌曲已播放,但旧歌曲元数据仍保留。
如何使用新的元数据刷新视图?
对于这样一个简单的问题,我事先表示歉意。我已经在广泛搜索答案,或者不了解或似乎不适用。善良,我年老又脆弱。
import UIKit
import AVFoundation
class SecondViewController: UIViewController, AVAudioPlayerDelegate {
@IBOutlet weak var currentSongArtwork: UIImageView!
@IBOutlet weak var currentSongTitle: UILabel!
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
playSong()
}
func playSong() {
extractSongMetaData()
currentSongTitle.text = extractedSongTitle
currentSongArtwork = extractedSongArtwork
let audioPath = Bundle.main.path(forResource: songs[currentSongIndex], ofType: ".m4a")
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
audioPlayer.delegate = self
audioPlayer.play()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
// Increment currentSongIndex
playSong()
}
}
答案 0 :(得分:0)
您可能需要在UI线程中调用playSong,以使其正确刷新。
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
// Increment currentSongIndex
DispatchQueue.main.async
{
playSong()
}
}