gpt4 book ai didi

ios - 获取 AVAudioPlayer 一次播放多种声音

转载 作者:IT王子 更新时间:2023-10-29 05:22:30 25 4
gpt4 key购买 nike

我试图让多个声音文件在 AVAudioPlayer 实例上播放,但是当一个声音播放时,另一个停止。我一次只能播放一种声音。这是我的代码:

import AVFoundation

class GSAudio{

static var instance: GSAudio!

var soundFileNameURL: NSURL = NSURL()
var soundFileName = ""
var soundPlay = AVAudioPlayer()

func playSound (soundFile: String){

GSAudio.instance = self

soundFileName = soundFile
soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!)
do{
try soundPlay = AVAudioPlayer(contentsOfURL: soundFileNameURL)
} catch {
print("Could not play sound file!")
}

soundPlay.prepareToPlay()
soundPlay.play ()
}
}

任何人都可以告诉我如何同时播放多个声音文件来帮助我吗?任何帮助深表感谢。

非常感谢,凯

最佳答案

音频停止的原因是因为您只设置了一个 AVAudioPlayer,所以当您要求类播放另一种声音时,您当前正在用 AVAudioPlayer 的新实例替换旧实例。你基本上是在覆盖它。

您可以创建 GSAudio 类的两个实例,然后在每个实例上调用 playSound,或者使该类成为使用 audioPlayers 字典的通用音频管理器。

我更喜欢后一种选择,因为它允许更简洁的代码并且也更高效。你可以检查一下你之前是否已经为声音制作了一个播放器,而不是制作一个新的播放器。

无论如何,我为您重新制作了您的类,以便它可以同时播放多种声音。它还可以在自身上播放相同的声音(它不会替换之前的声音实例)希望对您有所帮助!

该类是单例,因此要访问该类,请使用:

GSAudio.sharedInstance

例如,要播放您会调用的声音:

GSAudio.sharedInstance.playSound("AudioFileName")

同时播放多个声音:

GSAudio.sharedInstance.playSounds("AudioFileName1", "AudioFileName2")

或者您可以在某处的数组中加载声音并调用接受数组的 playSounds 函数:

let sounds = ["AudioFileName1", "AudioFileName2"]
GSAudio.sharedInstance.playSounds(sounds)

我还添加了一个 playSounds 函数,允许您延迟以级联格式播放的每个声音。所以:

 let soundFileNames = ["SoundFileName1", "SoundFileName2", "SoundFileName3"]
GSAudio.sharedInstance.playSounds(soundFileNames, withDelay: 1.0)

会在 sound1 之后播放 sound2,然后 sound3 会在 sound2 之后播放一秒钟,依此类推。

这是类:

class GSAudio: NSObject, AVAudioPlayerDelegate {

static let sharedInstance = GSAudio()

private override init() {}

var players = [NSURL:AVAudioPlayer]()
var duplicatePlayers = [AVAudioPlayer]()

func playSound (soundFileName: String){

let soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!)

if let player = players[soundFileNameURL] { //player for sound has been found

if player.playing == false { //player is not in use, so use that one
player.prepareToPlay()
player.play()

} else { // player is in use, create a new, duplicate, player and use that instead

let duplicatePlayer = try! AVAudioPlayer(contentsOfURL: soundFileNameURL)
//use 'try!' because we know the URL worked before.

duplicatePlayer.delegate = self
//assign delegate for duplicatePlayer so delegate can remove the duplicate once it's stopped playing

duplicatePlayers.append(duplicatePlayer)
//add duplicate to array so it doesn't get removed from memory before finishing

duplicatePlayer.prepareToPlay()
duplicatePlayer.play()

}
} else { //player has not been found, create a new player with the URL if possible
do{
let player = try AVAudioPlayer(contentsOfURL: soundFileNameURL)
players[soundFileNameURL] = player
player.prepareToPlay()
player.play()
} catch {
print("Could not play sound file!")
}
}
}


func playSounds(soundFileNames: [String]){

for soundFileName in soundFileNames {
playSound(soundFileName)
}
}

func playSounds(soundFileNames: String...){
for soundFileName in soundFileNames {
playSound(soundFileName)
}
}

func playSounds(soundFileNames: [String], withDelay: Double) { //withDelay is in seconds
for (index, soundFileName) in soundFileNames.enumerate() {
let delay = withDelay*Double(index)
let _ = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: #selector(playSoundNotification(_:)), userInfo: ["fileName":soundFileName], repeats: false)
}
}

func playSoundNotification(notification: NSNotification) {
if let soundFileName = notification.userInfo?["fileName"] as? String {
playSound(soundFileName)
}
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
duplicatePlayers.removeAtIndex(duplicatePlayers.indexOf(player)!)
//Remove the duplicate player once it is done
}

}

关于ios - 获取 AVAudioPlayer 一次播放多种声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865233/

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