Problem with audio files in Swift

Hi guys, I've been this app for quite a while and I wanted to add audio to it but I've encountered a strange bug. Whenever I try to play the audio from the testing device, it prints out that the audio file cannot be found. I've checked multiple times the names and the code and I get no errors there whatsoever. I have no idea what might be causing this. Here's a part of the code: `import SwiftUI import AVFoundation

struct Card: Identifiable {
    let id = UUID()
    let heading: String
    let description: String
    let imageName: String
    let detailDescription: String
    let sonification: String
}
struct ExplorepageUI: View {
    @State private var selectedCard: Card?
    @State private var showMore = false
    @State private var currentIndex = 0
    @State private var currentCards: [Card] = []
    
    let galaxies = [
        Card(heading: "The Mice Galaxies",
             description: "They’re located about 300 million light-years away in the constellation Coma Berenices.",
             imageName: "TheMiceGalaxiesHubble",
             detailDescription:"""
                    Their name refers to the long tails produced by tidal action, the relative difference between gravitational pulls on the near and far parts of each galaxy, known here as a galactic tide. It is a possibility that both galaxies, which are members of the Coma Cluster, have experienced collision, and will continue colliding until they coalesce. The colors of the galaxies are peculiar. In NGC 4676A a core with some dark markings is surrounded by a bluish white remnant of spiral arms. The tail is unusual, starting out blue and terminating in a more yellowish color, despite the fact that the beginning of each arm in virtually every spiral galaxy starts yellow and terminates in a bluish color. NGC 4676B has a yellowish core and two arcs; arm remnants underneath are bluish as well. 
                    The galaxies were photographed in 2002 by the Hubble Space Telescope. In the background of the Mice Galaxies, there are over 3000 galaxies, at distances up to 13 billion light-years. 
             """,
        sonification: "SonificationoftheMiceGalaxies"),

`class MusicPlayer: ObservableObject { private var audioPlayer: AVPlayer?

func playSound(named sonificationFileName: String){
    if let url = Bundle.main.url(forResource: sonificationFileName, withExtension: "mp3"){
        print("✅ Found audio file at: \(url)")
        audioPlayer = try? AVPlayer(url: url)
        audioPlayer?.play()
        print("🎵 Audio should now be playing!")
    } else {
        print("❌ Audio file not found: \(sonificationFileName).mp3")
   }
    
}
func pause(){
    audioPlayer?.pause()
}

}

The problem with your app not being able to find the audio files most likely has nothing to do with your code.

Are the audio files in the Resources folder in the app bundle? If they are not, your code will not be able to find and load them.

Make sure the audio files are in the app target's Copy Bundle Resources build phase. The files must in this build phase to get copied to the Resources folder in the app bundle when you build the project.

Take the following steps to look at the files in the Copy Bundle Resources build phase.

  1. Open the project editor by selecting the project file from the left side of the project window.
  2. Select the app target from the target list on the left side of the project editor.
  3. Click the Build Phase button at the top of the project editor.
  4. Click the disclosure triangle next to the Copy Bundle Resources build phase.

If your audio files are not in the Copy Bundle Resources build phase, click the Add button below the list of files to open a sheet where you can choose the files to add to the build phase.

Problem with audio files in Swift
 
 
Q