[英]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.