Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio not playing in SwiftUI

Tags:

swift

swiftui

I'm having an issue getting an audio file to play when the function is called.

I am using an AVAudioPlayer to try and play the file following the instructions form here: https://www.hackingwithswift.com/example-code/media/how-to-play-sounds-using-avaudioplayer After the button is pressed in the view, it calls a func to play the sound, but from what I can tell, nothing is played. There are no errors thrown, and the file is found. The app also uses a speech synthesizer when a button is pushed, and that plays fine.

I looked around stack overflow and followed the instructions from here: Where to place code for audio playback in a SwiftUI app

But still the audio is not played when the button is pushed.

Here is the func:

    func playSound() {
        var sound = AVAudioPlayer()

        if let path = Bundle.main.path(forResource: "TryAgain", ofType: "wav") {
            do {
                sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                print("Playing sound")
                sound.play()
            } catch {
                print( "Could not find file")
            }
        }

Here is the class:

class Player: BindableObject {

    let willChange = PassthroughSubject<Player, Never>()

    var isPlaying: Bool = false {
        willSet {
            willChange.send(self)
        }
    }

    func playSound() {
        var sound = AVAudioPlayer()

        if let path = Bundle.main.path(forResource: "TryAgainWav", ofType: "wav") {
            do {
                sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                print("Playing sound")
                sound.play()
            } catch {
                print( "Could not find file")
            }
        }
    }
}

Update: Not sure if this helps, but I built this in with IB instead of SwiftUI and noticed the same message is printed in the console when I click the button to play the audio file:

2019-07-22 11:29:41.075568-0700 PlaySoundPrac[13952:46549155] [plugin] AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46

Any help would be greatly appreciated

like image 933
dcvonb Avatar asked Jul 22 '19 21:07

dcvonb


1 Answers

I'm pretty new myself, but I'll do my best. What happens if you move the declaration of AVAudioPlayer outside the function? For example:

import Combine

class Player: ObservableObject {

    var sound: AVAudioPlayer! 

    let willChange = PassthroughSubject<Player, Never>()

    var isPlaying: Bool = false {
        willSet {
            willChange.send(self)
        }
    }

    func playSound() {

        if let path = Bundle.main.path(forResource: "TryAgainWav", ofType: "wav") {
            do {
                sound = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                print("Playing sound")
                sound.play()
            } catch {
                print( "Could not find file")
            }
        }
    }
}

Try this out and let me know if it works or not! Thanks.

like image 189
Caleb Pomayo Avatar answered Nov 15 '22 05:11

Caleb Pomayo