简体   繁体   English

Swift 中的 AudioToolBox - 声音在模拟器中有效,但在我的 iPad 上无效

[英]AudioToolBox in Swift - Sound works in Simulator but not on my IPad

I'm creating a music app where different chimes will be played at different points in a rhythm cycle.我正在创建一个音乐应用程序,其中将在节奏周期的不同点播放不同的铃声。 So far, my code works fine in the Simulator but I'm not getting any sound on my device.到目前为止,我的代码在模拟器中运行良好,但我的设备上没有任何声音。 I get sounds from other apps.我从其他应用程序中获得声音。 Is there something wrong with my code?我的代码有问题吗? Thanks for you help!谢谢你的帮助!

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)
}

I had the same issue and realised my iPhone was on silent mode, taking it off silent played the sounds.我遇到了同样的问题,并意识到我的 iPhone 处于静音模式,将其关闭静音播放声音。

Unfortunately AudioToolbox sounds respond to the 'Ringer' volume of the device, and not the 'Volume' setting that audio through AVFoundation uses.不幸的是,AudioToolbox 声音响应设备的“铃声”音量,而不是通过 AVFoundation 的音频使用的“音量”设置。

It makes sense, as AudioToolbox is supposed to be used for system sounds.这是有道理的,因为 AudioToolbox 应该用于系统声音。

From related question: AudioServicesPlaySystemSound not working on iPad device来自相关问题: AudioServicesPlaySystemSound not working on iPad device

You can't play MP3 on iDevice, need to use a different framework.你不能在 iDevice 上播放 MP3,需要使用不同的框架。

我的问题是为 AudioQueue 分配的缓冲区数,我分配了 3 个,它在 MacOS 上工作,但是对于 iOS 设备,您至少需要 4 个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM