UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Where should audio files be placed in a SwiftUI project?

Forums > SwiftUI

I’m working with sounds for the first time, and for some reason, Swift can’t find my .wav file in my project. The file name is Test_Bell.wav”, and here’s my code, which is failing as it tries to force-unwrap the path variable:

class SoundManager {

var audioPlayer = AVAudioPlayer()
let hintSolvedFileName = “Bell_Test.wav"
@Published private(set) var soundEffectsEnabled = false
@Published private(set) var musicEnabled = false

func playHintSolved() {
//force unwrapped path
let path = Bundle.main.path(forResource: hintSolvedFileName, ofType: nil)!
let url = URL(fileURLWithPath: path)

do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.play()
} catch {
print("SoundManager.playHintSolved couldn't play")
fatalError("Coudln't play file")
}//catch
}//func

}//class

I’ve confirmed that my file name is correct, and even confirmed that I can play the sound, by selecting it in the Navigator, viewing it in the Editor, and playing the sound.

Is there a specific area I need to place my file In my project? Right now, it’s just inside my project, where all of my Swift and JSON files are. I’m stumped as to why it can’t find the path my my file.

2      

I would think that you would want to put them in assets, but I'm not 100% sure of that. I've never worked with sound yet.

3      

@Bnerd  

I have the below setup and it works. My code is the same as yours, only difference is I am using a Struct and not a Class.

My soundplayer struct

import Foundation
import AVFoundation
var audioPlayer: AVAudioPlayer!

struct PlaySound {

    func playSound(soundFile: String) {
        let path = Bundle.main.path(forResource: soundFile, ofType: nil)!
        let url = URL(fileURLWithPath: path)

        do {
            //create your audioPlayer in your parent class as a property
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            audioPlayer.play()
        } catch {
            print("couldn't load the file")
        }
    }
}

2      

OK - you can try this code , i had some similar issue with one of my music apps,

Step 1 - i saved my music files along with other files , not in documents directory

Step 2 - i made all my music file names lower cased , removed special characters etc, some like belltest , instead of may be Bell_Test

Step 3- then i called as per my app , where i was using an Observable class - songs.playSongs(fileName: "\(song.name.lowercased()).mp3")

i used lowercased() method and appended my file type , which was .mp3

then it worked ..

hope it helps ..

2      

Thank you all for the suggestions. However, the issue is still with finding the file itself. I've tried dragging it into my project Navigator, and it couldn't be found there. Also, I found my project in Finder, and attempted to paste the file into the project there, and it also didn’t work. Where have you guys put your sound åfiles in your project?

2      

@Bnerd  

As you can see from my screenshot above they are in the Project folder. I placed them by dragging them there into Xcode (I am not sure if simple copy pasting in the finder folder would work..)

2      

@DarkhorseStudios - you can keep the files in the folder where you keep other swift files, have you tried changing their names to small caps and removing any other special characters like dash etc ?

2      

Have you tried looking at the build files themselves to make sure the .wav file is there?

I had a similar problem with loading a machine learning model file only to discover xcode compresses it and changes the file name extension on build.

Edit: Looks like AVAudioPlayer has some specific file format rules. Have you tried converting to an .mp3?

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.