1

I am trying to make the xcode simulated iPhone play a wav audio file when one of 7 buttons from the UI interface are pressed. The code is the following:

import UIKit
import AudioToolbox

class ViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
    }

   @IBAction func notePressed(_ sender: UIButton) { //7 buttons are associated with this IBAction, tagged from 1-7

        if let soundURL =  Bundle.main.url(forResource: "note1", withExtension: "wav") {
            var mySound: SystemSoundID = 0
            AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)

            //play
            AudioServicesPlaySystemSound(mySound);
            print(sender.tag) //to print the button tag and make sure the IBAction is working
        }
        else{
            print("no note")
        }

    }

}

When I run it, I see no errors. Also, when I press one of the 7 buttons, the button tag is indeed printed. The "no note" condition from the else statement is never printed.

There is a picture of the simulated iPhone running here. In the picture, each colored rectangle is one of the 7 buttons. They were pressed in a sequence so the tags can be seen printed in the output. Also, the "note1.wav" file can be seen in the navigation pain, in the Sound Files folder. If I click in note1.wav in the navigation pane, I can hear it just fine.

Here are the mac specs:

  • Dual Core Processor
  • 8GB of RAM
  • macOS High Sierra 10.13.6

Also, I am using this mac through a remote desktop connection; this computer is from Macincloud, a remote mac rental service). I have no idea if this could change anything, but as I said before, I can hear audio files from the mac just fine if they are played out of the xcode device simulation.

Does anyone have any idea how to solve this?

  • your code is okay to play sound(tested). you may need to enable sound in macincloud. Play youtube in safari from your simulator. – Razib Mollick Oct 22 '18 at 21:39
  • Sound is enable in my remote connection. Youtube audio works fine in safari, and if I click the note1.wav file in the navigation pane under "Sound Files" folder, I can also hear it being played. – F Severgnini Oct 22 '18 at 22:12
  • Did you use Simulator safari, not mac safari? – Razib Mollick Oct 23 '18 at 00:51
  • I think the relevant item is - does it play sound in an actual device? If yes, then what's the issue? And yes, if no, then **what's** the issue? (Please, be specific.) –  Oct 23 '18 at 01:50
  • I am remotely connecting to this mac that I am using to run the simulations, so unfortunately I can't test with real iPhones. – F Severgnini Oct 23 '18 at 17:31
  • @Razib Mollick: Audio is played in Mac safari. Simulated iPhone safari plays no audio either. Simulated volume was set to max. – F Severgnini Oct 23 '18 at 17:40
  • After contacting Macincloud, I was told: "Please note that some sound functions in Xcode are not able to be pushed via RDP and may not work". I have no idea how Macincloud limits certain audio functions in their macs, neither why they would do that. Either way, this is most likely the cause of my issues. Thanks for everyone in here for giving me suggestions to debug this issue. – F Severgnini Oct 24 '18 at 21:56

0 Answers0