10

Audio is not playing using this approach. The play() function is executing without any error. Please help

var audioPlayer = AVAudioPlayer()
let path = Bundle.main.path(forResource: "a", ofType: "mp3")
@State var isPlaying : Bool = false

var body: some View {
    Button(action: {
         self.isPlaying.toggle()    
         let url = URL(fileURLWithPath: self.path!)
         do {
             self.audioPlayer = try AVAudioPlayer(contentsOf: url)
             self.audioPlayer.prepareToPlay()
             self.audioPlayer.play()
         }catch {
             print("Eror")
         }        
      }, label: {
           if isPlaying {
                    Image(systemName: "pause")
                    .font(Font.system(.largeTitle).bold())
           }else {
                Image(systemName: "play.fill")
                 .font(Font.system(.largeTitle).bold())
           }
    })
}
deepak joshi
  • 125
  • 1
  • 9

1 Answers1

21

Is the audiofile there? Please select the project, go to Build Phases tab and under 'Copy Bundle Resources' you must see the audio file. If it is there then the problem is this.

I tried your code, it played the sound and then crashed. i changed it like this to make it work

 @State var audioPlayer:AVAudioPlayer?

 @State var isPlaying : Bool = false

 var body: some View {

     Button(action: {

         if let path = Bundle.main.path(forResource: "a", ofType: ".mp3") {

             self.audioPlayer = AVAudioPlayer()

             self.isPlaying.toggle()

             let url = URL(fileURLWithPath: path)

             do {
                 self.audioPlayer = try AVAudioPlayer(contentsOf: url)
                 self.audioPlayer?.prepareToPlay()
                 self.audioPlayer?.play()
             }catch {
                 print("Error")
             }
         }

     }, label: {

----

Have you considered separating your Audio model from your UI? It would make your code much clearer if you put it into separate Swift file

import AVFoundation

 class Sounds {

   static var audioPlayer:AVAudioPlayer?

   static func playSounds(soundfile: String) {

       if let path = Bundle.main.path(forResource: soundfile, ofType: nil){

           do{

               audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
               audioPlayer?.prepareToPlay()
               audioPlayer?.play()

           }catch {
               print("Error")
           }
       }
    }
 }

And just one line to use it in UI

var body: some View {
    Button(action: {
        self.isPlaying.toggle()
        Sounds.playSounds(soundfile: "0.wav")

    }, label: {
Nalov
  • 578
  • 5
  • 9
  • I cannot get your class Sounds version to work in my SwiftUI app. I'm trying to add the Sounds.playSounds(soundfile: "GetReady.m4a") in an .onAppear() but it won't play my sound. Is there a trick to adding sound files to SwiftUI projects maybe? I created a new folder in my Project navigator then dragged/dropped the .m4a files into it. – jammyman34 Jan 27 '21 at 22:28
  • can you put the project in githube? – Nalov Feb 06 '21 at 08:32
  • I created a public test project that is essentially set up the same way as my real project: https://gitlab.com/jammyman34/sounds-test – jammyman34 Feb 08 '21 at 21:27
  • Some files are missing in github project, can you reload? the ones in red color are the missing files https://ibb.co/FYWFX13 – Nalov Feb 10 '21 at 13:28
  • ok, try this project please https://gitlab.com/jammyman34/test-sounds-project – jammyman34 Feb 11 '21 at 23:40
  • It seems your sounds files have not been added to the project, please select each the sound file and on the right side check the box before SoundsTest like this https://ibb.co/PFbRNr8 – Nalov Feb 19 '21 at 18:53
  • 1
    Wow!! That was exactly it. THANK YOU!!! – jammyman34 Feb 21 '21 at 16:38