Unable to play sound file when button is tapped

Hi,

My app is a simple one, where someone taps on a button and a sound file is activated. But when that's done, the sound file doesnt activate.


Here's the code:


import SwiftUI
import AVFoundation

struct ContentView: View {
  
  private var autoPlayer: AVAudioPlayer = AVAudioPlayer()
  
    var body: some View {
     
      GeometryReader { _ in
      
      ZStack {
        
        Color.black
        .edgesIgnoringSafeArea(.all)
        
        Circle()
        .frame(width: 400, height: 400)
        .foregroundColor(Color.white)
        
        Circle()
        .frame(width: 340, height: 500)
        .foregroundColor(Color.red)
        
        Button(action: {
          
          
          
          print("Button Tapped!")
          
          let path = Bundle.main.path(forResource: "siren", ofType: "mp3")!
     
          do {
            let playFile = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
            
            playFile.play()
            
          } catch {
              
            print(error)
          }
          
        }) {
          
          
        Text("Tap HERE for Alarm")
          .font(.largeTitle)
          .foregroundColor(Color.white)
          .bold()
          .italic()
      }
        }
            
        
        
      }.navigationBarTitle(Text("Personal Alarm"))
      }
      
      func playSound()
      {
        print("Button Tapped!")
            
            let path = Bundle.main.path(forResource: "siren.mp3", ofType: nil)!
            let url = URL(fileURLWithPath: path)

            do {
                let soundFile = try AVAudioPlayer(contentsOf: url)
              soundFile.play()
            } catch {
                
              print("Cannot Play Sound File!")
            }
        }
      }



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


Any ideas?


Dan Uff

Accepted Reply

I tried with similar approach and could not get it work.

So, I changed in


      func playSound() 
                       var filePath: String?
                       filePath = Bundle.main.path(forResource: "ButtonTap", ofType: "wav")     // For you should be siren  and mp3
                       let fileURL = URL(fileURLWithPath: filePath!)
                       var soundID:SystemSoundID = 0
                       AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID)
                       AudioServicesPlaySystemSound(soundID)
     }

And it worked.


Note: need to add the resource to the bundle:

https://stackoverflow.com/questions/41775563/bundle-main-pathforresourceoftypeindirectory-returns-nil


Hope that helps

Replies

Oh, here's what the terminal says:


Button Tapped!

2020-06-04 14:36:03.397228-0400 PAlarm[7376:89091] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600000998600> F8BB1C28-BAE8-11D6-9C31-00039315CD46

Did you confirm the actual file name of the sound matches the name in your code? siren.mp3 is not the same as Siren.mp3 on iOS, assuming that's the target platform w/this project.

I tried with similar approach and could not get it work.

So, I changed in


      func playSound() 
                       var filePath: String?
                       filePath = Bundle.main.path(forResource: "ButtonTap", ofType: "wav")     // For you should be siren  and mp3
                       let fileURL = URL(fileURLWithPath: filePath!)
                       var soundID:SystemSoundID = 0
                       AudioServicesCreateSystemSoundID(fileURL as CFURL, &soundID)
                       AudioServicesPlaySystemSound(soundID)
     }

And it worked.


Note: need to add the resource to the bundle:

https://stackoverflow.com/questions/41775563/bundle-main-pathforresourceoftypeindirectory-returns-nil


Hope that helps

My sanity and I thank you. :-)