Keerthi MCA
Keerthi MCA

Reputation: 1

AVAudioPlayer is not playing audio in swift

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

Upvotes: 0

Views: 388

Answers (1)

Saifullah ilyas
Saifullah ilyas

Reputation: 464

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()

Upvotes: 0

Related Questions