bundlePath / URL issue in Match Game

Quick Description: App hangs up trying to play audio, enters loop spitting out data to console

First Fix Attempt: Download project and run that. Same issue.

Next: Added print logging to isolate problem, which looks to be this statement:


audioPlayer = try AVAudioPlayer(contentsOf: url)

When I comment out this statement, the app runs fine (if silent, with SFX print-logging). I suspect bad URL. Pertinent code from SoundManager class with print statements:


// get path to resource
print(soundFileName)

    let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: ".wav")

print(bundlePath!)

    // check that it's not nil
    guard bundlePath != nil else {

print(“nil exception triggered”)
return
}

    let url = URL(fileURLWithPath: bundlePath!)

print(url)

    do {

print (“enter do”)
// create the audio player
audioPlayer = try AVAudioPlayer(contentsOf: url)

print (“SFX:” + soundFileName)

// // play sfx
audioPlayer?.play()

    }
    catch {
        print ("Couldn't create audio player.")
        return

Console returns:


shuffle

/Users/oberon/Library/Developer/CoreSimulator/Devices/44DC320C-DD90-411D-8AE0-974AF684B819/data/Containers/Bundle/Application/E916DF5C-6338-42F8-8A89-6C3BC22F24D6/MatchGame2.app/shuffle.wav

file:///Users/oberon/Library/Developer/CoreSimulator/Devices/44DC320C-DD90-411D-8AE0-974AF684B819/data/Containers/Bundle/Application/E916DF5C-6338-42F8-8A89-6C3BC22F24D6/MatchGame2.app/shuffle.wav

enter do

2020-08-06 17:41:07.852212-0700 MatchGame2[5466:604450] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10000004

2020-08-06 17:41:12.854148-0700 MatchGame2[5466:604450] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10000004

2020-08-06 17:41:12.854554-0700 MatchGame2[5466:604450] HALDefaultDevice::Initialize: couldn’t add the default input device listener, Error: 268435460 (^P)

with last two messages repeating.

The path is what is weird. It is directing through the normally hidden Library folder and through the simulator instead of the direct file path. I think that’s the issue, but how do i solve it?

Thanks!

@bowtie
Hi Eric,

Welcome to the Code Crew community.

Can you post the entire SoundManager code. When you paste the code in use three back-ticks before the first line and three after the last line (the back-tick character on your keyboard is below the tilde ~). That makes sure that the code is formatted correctly.

For example, this is my SoundManager version

import Foundation
import AVFoundation

class SoundManager {
    
    var audioPlayer: AVAudioPlayer?
    
    enum SoundEffect {
        case flip
        case shuffle
        case match
        case noMatch
    }
    
    func playSound(_ effect: SoundEffect) {
        
        var soundFilename = ""
        
        switch effect {
            case .flip:
                soundFilename = "cardflip"
            case .match:
                soundFilename = "dingcorrect"
            case .noMatch:
                soundFilename = "dingwrong"
            case .shuffle:
                soundFilename = "shuffle"
        }
        
        //  Get path to resource
        let filePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
        
        //  Check that it is not nil
        guard filePath != nil else {
            print("Couldn't find sound file \(soundFilename) in the bundle")
            return
        }
        
        //  Create a URL object from the bundlePath string.
        let soundUrl = URL(fileURLWithPath: filePath!)
        
        do {
            //  Create audio player object
            audioPlayer = try AVAudioPlayer(contentsOf: soundUrl)
            
            //  Play the sound effect
            audioPlayer?.play()
            
        } catch {
            print("Cound not create the audio player object for the sound file \(soundFilename)")
            return
        }
    }
}

The next thing I’d like you to check is that for each sound file, ensure that in the File Inspector that the Target Membership is checked as per the attached screenshot:

Sorry for the delay. Based on some things I have read, it sounds like there might be a bug with the newest XCode (I’m on 12.0 b3) and iOS 14 simulator. Not sure. I have tried different approaches, and they all wind up with the " failed to find the AHS sub-server port" error.

Here’s my code with print-logging:

import Foundation
import AVFoundation


class SoundManager {
    
    
    var audioPlayer:AVAudioPlayer?
    
    enum SoundEffect {
        case flip
        case match
        case nomatch
        case shuffle
    }
    
    
    func playSound(effect:SoundEffect) {
        var soundFileName = ""
        switch effect {
        case .flip:
            soundFileName = "cardflip"
        case .match:
            soundFileName = "dingcorrect"
        case .nomatch:
            soundFileName = "dingwrong"
        case .shuffle:
            soundFileName = "shuffle"
            
        }
        
        // get path to resource
        print(soundFileName)
        
        let bundlePath = Bundle.main.path(forResource: soundFileName, ofType: ".wav")
        
        print(bundlePath!)
        
        // check that it's not nil
        guard bundlePath != nil else {
            print("nil exception triggered")
            return
        }
        
        let url = URL(fileURLWithPath: bundlePath!)
        print(url)
        
        do {
            print ("enter do")
            // create the audio player
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            
            print ("SFX:" + soundFileName)
            
            //            // play sfx
            audioPlayer?.play()
            
        }
        catch {
            print ("Couldn't create audio player.")
            return
            
        }
        
    }

Target membership is checked and correct.

Here is the relevant console output:

/Users/oberon/Library/Developer/CoreSimulator/Devices/44DC320C-DD90-411D-8AE0-974AF684B819/data/Containers/Bundle/Application/7E9EABF9-A42B-4608-9527-46A5DA3D1EE5/MatchGame2.app/shuffle.wav

file:///Users/oberon/Library/Developer/CoreSimulator/Devices/44DC320C-DD90-411D-8AE0-974AF684B819/data/Containers/Bundle/Application/7E9EABF9-A42B-4608-9527-46A5DA3D1EE5/MatchGame2.app/shuffle.wav

enter do

2020-08-20 20:02:26.868280-0700 MatchGame2[35793:3800216] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10000004

As a follow up, I commented out the creation of the audio player and the app ran smoothly, printing out the sound effect to the console.

        do {
print ("enter do")
            // create the audio player
//            audioPlayer = try AVAudioPlayer(contentsOf: url)

print ("SFX:" + soundFileName)
            
//            // play sfx
            audioPlayer?.play()
            
        }

enter do

SFX:dingcorrect

That’s one of the reasons that I don’t download Beta software when I need things to work properly. You would be better off running a stable release of the OS and a stable release of Xcode.

Sounds like downgrading is the best option …

As a side question, is there a way to catch this type of problem in code and trigger a catch or something else? It doesn’t cause a crash, it just hangs in a loop.

To be honest I don’t know. If the try AVAudioPlayer(contentsOf: url) is hanging then there’s not a lot you can do but wait for it to time out.

Have you searched in StackOverflow for anyone posting the same issue?

The other question on my mind is did you add the sound files to your project so that they were copied in?