0

I'm trying to face with the book "Intro to app development" by apple. I'm stuck on exercise AnimalSounds. There is a SimpleSound class provided by the book, and our job is to use a sound object to reproduce a sound. The simulator does not play any sound.

The audio files are in the build bundle and are found by the class.

I checked the simulator with other apps (e.g. youtube on safari) and sound works.

SimpleSound class (provided by example, not written by me)

import Foundation
import AudioToolbox

class SimpleSound {

    private var soundID: SystemSoundID = 0

    public init(named name: String) {
        if let soundURL = soundURL(forName: name) {
            let status = AudioServicesCreateSystemSoundID(soundURL as CFURL, &soundID)
            if status != noErr {
                print("Unable to create sound at URL: '\(name)'")
                soundID = 0
            }
        }
    }

    public func play() {
        if soundID != 0 {
            print("Playing sound \(soundID)")
            AudioServicesPlaySystemSound(soundID)
            //AudioServicesPlaySystemSound(1001)
        }
    }

    private func soundURL(forName name: String) -> URL? {

        let fileExtensions = ["m4a", "wav", "mp3", "aac", "adts", "aif", "aiff", "aifc", "caf", "mp4"]

        for fileExtention in fileExtensions {
            if let soundURL = Bundle.main.url(forResource: name, withExtension: fileExtention) {
                return soundURL
            }
        }
        print("Unable to find sound file with name '\(name)'")
        return nil
    }

    deinit {
        if soundID != 0 {
            AudioServicesDisposeSystemSoundID(soundID)
        }
    }
}

Code used in my viewController (written by me) in a button tapped action:

let meowSound = SimpleSound(named: "meow")
meowSound.play()

When debugging from Xcode line by line, when the function play() is invoked, the sound is reproduced. When app is running in simulator (with no debugging) or in actual iPhone sound does not play. If standard system sound 1001 is uncommented, sound is played for both simulator and iPhone.

Any ideas?

  • You may need to include the SimpleSound class. Also what do you mean by "I'm trying to face with the book"? – Christopher Larsen Aug 19 '19 at 16:19
  • You need to debug it a bit further: 1 - add a print statement inside `if soundID != 0 {` and see if function enters this if condition when not debugging. If not, it means that for some reason soundID is 0 for some reason at that time, which is one direction. If it enters `if`, but doesn't play a sound, try to call `AudioServicesPlaySystemSound(soundID)` with some predefined sound (e.g. `AudioServicesPlaySystemSound(1000)` – timbre timbre Aug 19 '19 at 16:32
  • 1
    @ChristopherLarsen sorry, I mean that I am studying and doing exercises on the book provided by apple in Books (Intro to App Development with Swift). Maybe face is not the correct word? – Diego Talledo Aug 19 '19 at 18:25
  • @KirilS. I did what you suggested: the message is printed (soundID is different than zero) but no sound coming out from simulator. I added also AudioServicesPlaySystemSound(1000) and I hear the beep! – Diego Talledo Aug 19 '19 at 18:30
  • So it seems the problem is not with your code, it's possibly with sound (e.g. takes time to load) or simulator... You are not alone, see for example: https://stackoverflow.com/questions/10351199/sound-does-only-work-on-device-but-not-in-simulator As you see that person decided to use a different player which I guess is not an option for you. As a workaround, maybe try to put a delay between init and play. Or, maybe force it into same synch thread. – timbre timbre Aug 19 '19 at 19:52
  • @KirilS. I tried the following: 1) add a sleep(5.0) before the AudioServicesPlaySystemSound(soundID). Does not change the fact that sound is not produced. Predefined sounds always work. 2) run in actual iPhone. No sound neither. Maybe it's not the simulator the issue? Also in iPhone a default sound (e.g. 1001) is reproduced. I am still not comfortable with Swift and iOS development in other to force it into same sync thread – Diego Talledo Aug 20 '19 at 06:25
  • so you established that the sound itself is at least a major part of the issue. Based on apple docs (https://developer.apple.com/documentation/audiotoolbox/1405248-audioservicesplaysystemsound) you should only use sounds in specific format, and only with "Packaged in a .caf, .aif, or .wav file". Is the file you are playing one of these? Maybe it's time to give up too: this is just an exercise, and your code is fine. If it was a real app, you'd probably use AVAudioPlayer anyway... – timbre timbre Aug 20 '19 at 16:23

0 Answers0