gpt4 book ai didi

ios - 在 Swift 中自动播放音频文件

转载 作者:行者123 更新时间:2023-11-30 12:07:43 24 4
gpt4 key购买 nike

我正在制作一个非常简单的应用程序,用户可以在其中选择一些他想要播放的音频文件。然后,他按下“播放”按钮,它就会依次播放选中的所有文件。

为此,我创建了一个数组来存储用户想要播放的文件的名称。然后,由“播放”按钮触发的函数将对数组进行迭代,查找名称并播放文件。

事情是这样的:

//declaring the audioplayer
var audioPlayer = AVAudioPlayer()

//declaring the array
var selectedMusic = [String]()

界面有 6 个按钮,每个按钮都链接到一首特定的音乐。用户可以通过点击按钮立即触发音乐,或者通过长按选择顺序播放。假设用户进行了选择,并且数组如下所示:

selectedMusic = ["Song 1", "Song 5", "Song 3", "Song 2"]

我播放歌曲的功能是:

for selection in selectedMusic {

if selection == "Song 1" {

guard let alertSound = Bundle.main.url(forResource: "Song 1", withExtension: "mp3") else {return}

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)


audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()

} catch let error {
print(error.localizedDescription)
}

} else if selection == "Song 2" {

guard let alertSound = Bundle.main.url(forResource: "Song 2", withExtension: "mp3") else {return}

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)


audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()

} catch let error {
print(error.localizedDescription)
}

依此类推,直到第5首歌。

我想要的是按特定顺序(1 到 5)播放所选歌曲,而不是用户选择的顺序(这很重要)。

然而,这个函数没有给出预期的结果,它只是播放所选内容的最后一个项目。

那么,我应该如何完成这项工作呢?阵列不是这里的最佳选择吗?正如我所提到的,歌曲必须按时间顺序播放。

任何帮助将不胜感激:)谢谢!

最佳答案

首先,您正在使用 for 并重复其中的代码..理论上您可以这样做

audioUrls.forEach { (url) in
guard let alertSound = Bundle.main.url(forResource: url, withExtension: "mp3") else {return}

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)


audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()

} catch let error {
print(error.localizedDescription)
}
}

其次,您没有跟踪音频,因此您的程序不知道轨道已完成播放,并且您正在使用新对象覆盖播放器。

例如,在下面的代码中,我跟踪我的音频播放器并使用当前时间更新标签。

func keepTrackOfAudio() {
audioPlayer?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)

//track progress

let interval = CMTime(value: 1, timescale: 2)
audioPlayer?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main , using: { (progressTime) in
let seconds = CMTimeGetSeconds(progressTime)
let secondsString = String(format: "%02d", Int(seconds.truncatingRemainder(dividingBy: 60)))
let minutesString = String(format: "%02d", Int(seconds / 60))

self.audioCurrentTimeLabel.text = "\(minutesString):\(secondsString)"

//lets move the slider thumb
if let duration = self.audioPlayer?.currentItem?.duration {
let durationSeconds = CMTimeGetSeconds(duration)

self.audioSlider.value = Float(seconds / durationSeconds)

}
})

}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {


if let duration = audioPlayer?.currentItem?.duration {
let seconds = CMTimeGetSeconds(duration)

let secondsText = String(format: "%02d", Int(seconds) % 60)
let minutesText = String(format: "%02d", Int(seconds) / 60)
audioLengthLabel.text = "\(minutesText):\(secondsText)"
}

}
}

你的作业是计算音频何时结束,然后播放下一首轨道。

关于ios - 在 Swift 中自动播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488269/

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