AndroidNoob
AndroidNoob

Reputation: 2821

Android HLS playback: Passing url parameters as headers in Mediaplayer.setDataSource() does not work properly

I have an HLS m3u8 playlist hosted on a server:

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-ALLOW-CACHE:YES
#EXT-X-KEY:METHOD=AES-128,URI="https://myserver/playlist/336385.smil/crypt.key?e=6889c9ad1087852"
#EXT-X-VERSION:2
#EXT-X-MEDIA-SEQUENCE:1
#EXTINF:10,
http://myserver/playlist/336385.smil/segment1_3_av.ts?e=6889c9ad1087852b
#EXTINF:10,
http://myserver/playlist/336385.smil/segment2_3_av.ts?e=6889c9ad1087852b
#EXTINF:10,
http://myserver/playlist/336385.smil/segment3_3_av.ts?e=6889c9ad1087852b
#EXTINF:10,
http://myserver/playlist/336385.smil/segment4_3_av.ts?e=6889c9ad1087852b
#EXTINF:10,
http://myserver/playlist/336385.smil/segment5_3_av.ts?e=6889c9ad1087852b
#EXTINF:10,
http://myserver/playlist/336385.smil/segment6_3_av.ts?e=6889c9ad1087852b
#EXT-X-ENDLIST

but there are extra url parameters needed to retrieve the playlist, i.e. the calls will look like this:

http://myserver/playlist/336385.smil/segment1_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2

Setting the headers as key-value pairs on the setDataSource (Context context, Uri uri, Map headers) does not work, at least the way I am trying it. Here is how I create the headers from the original url parameters:

String paramString =playlistUrl.substring( playlistUrl.indexOf("?") + 1);

String paramsSplit[] = paramString.split("&");
HashMap<String, String> headers = new HashMap<String, String>();

for(int i= 0;i<paramsSplit.length;i++)
{
     String key = paramsSplit[i].substring(0, playlistUrl.indexOf("="));
     String value = paramsSplit[i].substring(playlistUrl.indexOf("=")+1, playlistUrl.length() );
     headers.put(key, value);//e.g headers.put("param1", "value1");
 }

Thus I end up with a Map with my parameters in it (i.e. Map'<'param, value'>'), but adding these headers does not work.

If i go through the playlist m3u8 itself and amend every single url call with the extra parameters on the end myself, so the playlist is as follows:

#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-ALLOW-CACHE:YES
#EXT-X-KEY:METHOD=AES-128,URI="https://myserver/playlist/336385.smil/crypt.key?e=6889c9ad1087852b&param1=value1&param2=value2"
#EXT-X-VERSION:2
#EXT-X-MEDIA-SEQUENCE:1
#EXTINF:10,
http://myserver/playlist/336385.smil/segment1_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXTINF:10,
http://myserver/playlist/336385.smil/segment2_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXTINF:10,
http://myserver/playlist/336385.smil/segment3_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXTINF:10,
http://myserver/playlist/336385.smil/segment4_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXTINF:10,
http://myserver/playlist/336385.smil/segment5_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXTINF:10,
http://myserver/playlist/336385.smil/segment6_3_av.ts?e=6889c9ad1087852b&param1=value1&param2=value2
#EXT-X-ENDLIST

the playlist works fine, and plays no problem! Obviously it is not ideal to amend the m3u8 with these parameters manually, is there a way to set these parameters for the url calls automatically?

Upvotes: 3

Views: 2664

Answers (2)

dasar
dasar

Reputation: 5361

Everything after "?" mark is NOT headers, but query params.

If you wanna send this query params not only for m3u8 playlist, but for each media segment there is only one way to do so - you have to hardcode this params in playlist (segment urls).

To send them only with playlist request you have to set Url with query params in itself as data source. Like that: setDataSource("http://myserver/playlist/336385.smil/segment1_3_av.m3u8?e=6889c9ad1087852b&param1=value1&param2=value2")

  • you can try to use overloaded version of API setDataSource(Uri)

Upvotes: 1

Moses
Moses

Reputation: 433

it is too late to answer. But might be helpful to you. I didn't test but problem is in your way of coding. especially in following lines.

for(int i= 0;i<paramsSplit.length;i++)
{
     String key = paramsSplit[i].substring(0, playlistUrl.indexOf("="));
     String value = paramsSplit[i].substring(playlistUrl.indexOf("=")+1, playlistUrl.length() );
     headers.put(key, value);//e.g headers.put("param1", "value1");
 }

It might be like this

for(int i= 0;i<paramsSplit.length;i++)
    {
         String key = paramsSplit[i].substring(0, paramsSplit[i].indexOf("="));
         String value = paramsSplit[i].substring(paramsSplit[i].indexOf("=")+1, paramsSplit[i].length() );
         headers.put(key, value);//e.g headers.put("param1", "value1");
     }

Please try and verify with the help of Log.

Upvotes: 2

Related Questions