[英]AudioToolBox in Swift - Sound works in Simulator but not on my IPad
我正在创建一个音乐应用程序,其中将在节奏周期的不同点播放不同的铃声。 到目前为止,我的代码在模拟器中运行良好,但我的设备上没有任何声音。 我从其他应用程序中获得声音。 我的代码有问题吗? 谢谢你的帮助!
import UIKit
import AudioToolbox
func playChime1() {
var chime1URL: NSURL?
var chime1ID:SystemSoundID = 0
let filePath = NSBundle.mainBundle().pathForResource("Chime", ofType: "mp3")
chime1URL = NSURL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(chime1URL!, &chime1ID)
AudioServicesPlaySystemSound(chime1ID)
}
func playChime2() {
var chime2URL: NSURL?
var chime2ID:SystemSoundID = 0
let filePath = NSBundle.mainBundle().pathForResource("Chime2", ofType: "mp3")
chime2URL = NSURL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(chime2URL!, &chime2ID)
AudioServicesPlaySystemSound(chime2ID)
}
func playChime3() {
var chime3URL: NSURL?
var chime3ID:SystemSoundID = 0
let filePath = NSBundle.mainBundle().pathForResource("Chime3", ofType: "mp3")
chime3URL = NSURL(fileURLWithPath: filePath!)
AudioServicesCreateSystemSoundID(chime3URL!, &chime3ID)
AudioServicesPlaySystemSound(chime3ID)
}
我遇到了同样的问题,并意识到我的 iPhone 处于静音模式,将其关闭静音播放声音。
不幸的是,AudioToolbox 声音响应设备的“铃声”音量,而不是通过 AVFoundation 的音频使用的“音量”设置。
这是有道理的,因为 AudioToolbox 应该用于系统声音。
来自相关问题: AudioServicesPlaySystemSound not working on iPad device
你不能在 iDevice 上播放 MP3,需要使用不同的框架。
我的问题是为 AudioQueue 分配的缓冲区数,我分配了 3 个,它在 MacOS 上工作,但是对于 iOS 设备,您至少需要 4 个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.