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

Viewed 1563

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

My problem was the number of buffers allocated for AudioQueue, I allocated 3 and it worked on MacOS, however for iOS device you need at least 4

Related