3

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)
}
shavedape
  • 71
  • 1
  • 5

3 Answers3

1

I had the same issue and realised my iPhone was on silent mode, taking it off silent played the sounds.

Unfortunately AudioToolbox sounds respond to the 'Ringer' volume of the device, and not the 'Volume' setting that audio through AVFoundation uses.

It makes sense, as AudioToolbox is supposed to be used for system sounds.

danfordham
  • 980
  • 9
  • 15
0

From related question: AudioServicesPlaySystemSound not working on iPad device

You can't play MP3 on iDevice, need to use a different framework.

Community
  • 1
  • 1
sschale
  • 5,168
  • 3
  • 29
  • 36
0

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

Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61