我的应用程序按顺序播放 mp3 文件。 我终于设法按顺序播放 mp3 文件。 此外,我需要根据 mp3 文件在我的应用程序上显示简单的文本。
例如, 当播放 0.mp3 文件时,textlabel.text 显示来自 textarray 的“hello”。 播放 0.mp3 后, 当播放 1.mp3 文件时,textlabel.text 从 textarray 中显示“nice to meet you”。
如何根据音频文件在文本标签上依次显示简单的句子?
textarray = ["你好", "很高兴认识你", "我叫 James", "你多大了?"]
@IBAction func autoplay(_ sender: Any) {
var items : [AVPlayerItem] = []
for number in myIndex..<arr.count {
let url = Bundle.main.url(forResource: String(number), withExtension: "mp3")!
items.append(AVPlayerItem(url: url))
textlabel.text = textarry[number]
}
queue = AVQueuePlayer(items: items)
queue.play()
}
最佳答案
很多答案都提到了here关于如何在视频项目完成播放时获得回调。显示相关视频文本的一种方式如下,
@IBAction func autoplay(_ sender: Any) {
var items : [AVPlayerItem] = []
for number in myIndex..<arr.count {
let url = Bundle.main.url(forResource: String(number), withExtension: "mp3")!
items.append(AVPlayerItem(url: url))
}
queue = AVQueuePlayer(items: items)
queue.play()
textlabel.text = textarry[myIndex]
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(onVideoComplete),
name: .AVPlayerItemDidPlayToEndTime,
object: nil)
}
@objc func onVideoComplete() {
if let currentTitle = textlabel.text, let currentIndex = textarry.firstIndex(of: currentTitle) {
self.textlabel.text = textarry[currentIndex.advanced(by: 1)]
}
}
关于swift - 如何根据 mp3 文件显示简单文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496978/