AVAudioPlayer is not playing audio in swift

0
var player: AVAudioPlayer! = nil
    
    func playSound() {
        guard let url = Bundle.main.url(forResource: "playing", withExtension: "mp3") else { return }
        
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
            try AVAudioSession.sharedInstance().setActive(true)
                        
             player = try AVAudioPlayer(contentsOf: url)
            
            guard let player = player else { return }
            
            player.play()
            
        } catch let error {
            print(error.localizedDescription)
        }
    }

when i execute this code, i got the following error

AudioQueueObject.cpp:372:AudioQueueObject: AudioConverterNew from AudioQueueNew returned 1718449215 io: 1 ch, 16000 Hz, Float32 client: 1 ch, 16000 Hz, '.mp1' (0x00000000) 0 bits/channel, 0 bytes/packet, 384 frames/packet, 0 bytes/frame

ios
swift
audio
mp3
avaudioplayer
asked on Stack Overflow Oct 27, 2020 by Keerthi MCA

1 Answer

0

I am using this code to play sound

let sound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "sound", ofType: "mp3")!)
            audioPlayer = try! AVAudioPlayer(contentsOf: sound as URL)
            audioPlayer.delegate = self
            audioPlayer.prepareToPlay()
            audioPlayer.play()

I observer you did not prepare your media player before play. you just have to call this function before playing your audio

          player.prepareToPlay()
answered on Stack Overflow Oct 27, 2020 by Saifullah ilyas

User contributions licensed under CC BY-SA 3.0