1

Has any one had success storing an audio file in firebase storage, and then streaming that same file in IOS?

So far I've succesfully recorded the audio in swift (.m4a), upload the file to firebase storage, retrieved the download url which looks like this:

https://firebasestorage.googleapis.com/v0/b/ramb-ecce1.appspot.com/o/rambs%2Framb5E735EE0-E089-4B53-9B7D-DB7AD6CBF7DC?alt=media&token=52b02ee7-9f6b-474b-ab3b-92e0ca9182f3

I stored that audio in my firebase DB, and imported it in a var called "stream" and attemped to pass that into the audio player with no luck...

I see some information on firestore supporting streaming for Andriod, but nothing related to IOS.

Anyone able to offer some help here? Do I need to use another service for storage to host streaming urls? Would love to launch my MVP only using firebase if possible.

@ObservedObject var audioPlayer = AudioPlayer()

var stream = ""

var body: some View {

                VStack(alignment: .leading){

                    if audioPlayer.isPlaying == false {

                        Button(action: {
                            self.audioPlayer.startPlayback(audio: URL(string: self.stream)!)
                        }) {
                            Image(systemName: "play.circle")
                                .resizable()
                                .frame(width: 50, height: 50)
                        }
                    } else {

                        Button(action: {
                            self.audioPlayer.stopPlayback()
                        }) {
                            Image(systemName: "stop.fill")
                                .resizable()
                                .frame(width: 50, height: 50)
                        }
                    }


                }
         }
    }
}

}

Full audio player for reference!

class AudioPlayer: NSObject, ObservableObject, AVAudioPlayerDelegate {

let objectWillChange = PassthroughSubject<AudioPlayer, Never>()

var isPlaying = false {
    didSet {
        objectWillChange.send(self)
    }
}

var audioPlayer: AVAudioPlayer!

func startPlayback (audio: URL) {

    let playbackSession = AVAudioSession.sharedInstance()

    do {
        try playbackSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
    } catch {
        print("Playing over the device's speakers failed")
    }

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: audio)
        audioPlayer.delegate = self
        audioPlayer.play()
        isPlaying = true
    } catch {
        print("Playback failed.")
    }
}

func stopPlayback() {
    audioPlayer.stop()
    isPlaying = false
}

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
    if flag {
        isPlaying = false
    }
}

}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I should add some context, I retrieve download URL from Firebase DB in the form of a variable called "stream". – PeteKeating May 28 '20 at 21:58
  • AVAudioPlayer doesn't work with remote url, you can download it and use it as Data. https://stackoverflow.com/a/51733033/12208004 – user12208004 Oct 11 '20 at 15:04

1 Answers1

0

Have you tried StreamingKit?

var audioPlayer: STKAudioPlayer = STKAudioPlayer()

// Use this in your method
audioPlayer.play(audioURL)

With it, you can use the above for local URLs or the Firebase URL with the token.

Brandon
  • 507
  • 5
  • 21