1

I am trying to play audio from firebase database. My code is successful at grabbing the correct audio url as a URL from Firebase Database but I am having trouble playing it. When the view controller is opened, no audio is playing. I have tried my AVAudioPlayer with sample music from my library it works fine but it is not playing the selected audio url at all. This is my code for my AVAudioPlayer where I am trying to pass in the URL for it to play.

do {

            let songURL = url

            print(songURL!)
            try player = AVAudioPlayer(contentsOf: songURL!)
            progressBar.maximumValue = Float(player.duration)
            timer = Timer.scheduledTimer(timeInterval: 0.00001, target: self, selector: #selector(changeSliderValueFollowPlayerCurtime), userInfo: nil, repeats: true)
            player.delegate = self

            player.play()

        }

the "url" in the line of code let songURL = url comes from data passed from another tableview controller when the user selects the song it passes through information including the audio url from firebase database.

Allen Hinson
  • 11
  • 1
  • 2
  • Are you able to play that URL in browser? – Dharmesh Kheni Mar 23 '20 at 05:35
  • 1
    @DharmeshKheni yes I am able to play the url in my browser so I'm nut sure why it's not playing correctly on my app. – Allen Hinson Mar 23 '20 at 17:28
  • can you share the url you are trying? – Dharmesh Kheni Mar 23 '20 at 17:34
  • 1
    @DharmeshKheni the url changes depending on what song is selected by the user, but for example if a user selects this song then this url is suppose to be played from firebase `https://firebasestorage.googleapis.com/v0/b/mixed-with-music-28704.appspot.com/o/audio.%2F0FB6D524-F618-41B4-809C-6E4DDA1D1C6B?alt=media&token=41e4cd23-ee98-4211-867d-af4405189787` – Allen Hinson Mar 23 '20 at 17:48
  • @DharmeshKheni the answers in the link you provided does not help. I have already tried changing from AVAudioPlayer to AVPlayer but there are things that are formatted for AVAudioPlayer that do not work for AVPlayer – Allen Hinson Mar 23 '20 at 18:07
  • `AVAudioPlayer` does't support that but you can solve it buy downloading the audio first and then play it with `AVAudioPlayer` – Dharmesh Kheni Mar 23 '20 at 18:14

0 Answers0