How to fetch audio from my api with protected urls using just_audio (flutter)

1.2k views Asked by At

I'm trying to play audios using just_audio on my flutter app. My audio url are coming from an API that i've build using django rest framework and the urls to those audios are protected (I'm using token authorization)

1

There are 1 answers

0
Ryan Heise On

Assuming you have a token (e.g. obtained by making a separate HTTP request to another REST API you provided), you would normally pass that token in the Authorization header when requesting the audio file from your server.

Every method in just_audio that loads a URL also takes a headers parameter so that you can do just that.

Approach 1:

await player.setUrl(url,
    headers: {'Authorization': yourHeaderValue});

Approach 2:

await player.setAudioSource(AudioSource.uri(Uri.parse(url),
    headers: {'Authorization': yourHeaderValue});

Where yourHeaderValue is the Authorization header value required by your REST API. E.g.

Token 481594aa8B.....