AVAudioPlayer:waitingforasoundtofinishplaying
创始人
2024-11-12 21:30:13
0

在使用AVAudioPlayer时,如果正在播放音频时尝试播放另一个音频文件,则会出现“AVAudioPlayer : waiting for a sound to finish playing”的错误信息。这是因为AVAudioPlayer每次只能播放一个音频文件,不能同时播放多个音频文件。如果需要同时播放多个音频,则需要使用AVQueuePlayer。

示例代码:

// 首先,导入AVFoundation框架 import AVFoundation

// 创建AVAudioPlayer对象来播放音频文件 var audioPlayer: AVAudioPlayer?

// 加载音频文件并准备播放 func playSound() { // 获取音频文件路径 guard let path = Bundle.main.path(forResource: "sound", ofType: "mp3") else { return }

do {
    // 创建AVAudioPlayer对象
    audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
    
    // 开始播放音频文件
    audioPlayer?.play()
} catch {
    print("播放音频文件失败:\(error.localizedDescription)")
}

}

// 创建AVQueuePlayer对象来同时播放多个音频文件 var queuePlayer: AVQueuePlayer?

// 加载多个音频文件并准备播放 func playSounds() { // 获取音频文件路径数组 let paths = ["sound1.mp3", "sound2.mp3", "sound3.mp3"].compactMap({Bundle.main.path(forResource: $0, ofType: nil)})

// 创建AVPlayerItem数组
let items = paths.map({AVPlayerItem(url: URL(fileURLWithPath: $0))})

// 创建AVQueuePlayer对象
queuePlayer = AVQueuePlayer(items: items)

// 开始播放音频文件
queuePlayer?.play()

}

相关内容

热门资讯

透视好友"wepok... 透视好友"wepoker软件靠谱么"详细辅助线上教程(软件辅助程序神器)准备好在wepoker软件靠...
透视好友房"wepo... 透视好友房"wepoker辅助透视"详细辅助黑科技教程(黑侠辅助器正版下载软件);wepoker辅助...
透视规律"aapok... 透视规律"aapoker脚本怎么用"详细辅助实用技巧(可以增加运气教程)准备好在aapoker脚本怎...
透视私人局"hhpo... 透视私人局"hhpoker底牌透视脚本"详细辅助细节揭秘(辅助软件视频神器)这是由厦门游乐互动科技有...
透视游戏"hhpok... 1、透视游戏"hhpoker外挂靠谱吗"详细辅助攻略方法(辅助脚本辅助)2、进入游戏-大厅左侧-新手...
透视工具"poker... 透视工具"pokermaster辅助器"详细辅助新版2025教程(游戏安装教程技巧)1、不需要AI权...
透视辅助"德州透视插... 透视辅助"德州透视插件"详细辅助2025教程(辅助脚本透明)准备好在德州透视插件 ia的高塔上攀登,...
透视工具"aapok... 透视工具"aapoker怎么设置抽水"详细辅助大神讲解(可以看底牌透视)1、玩家可以在aapoker...
透视好友房"poke... 透视好友房"pokemmo辅助脚本"详细辅助透明挂教程(插件下载攻略);大神普及一款德州ai内幕,确...
透视模拟器"wepo... 透视模拟器"wepoker免费脚本咨询"详细辅助教你教程(免费钻石透明)是一款可以让一直输的玩家,快...