Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HLS playback: Passing url parameters as headers in Mediaplayer.setDataSource() does not work properly
    text
    copied!<p>I have an HLS m3u8 playlist hosted on a server:</p> <pre><code>#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 </code></pre> <p>but there are extra url parameters needed to retrieve the playlist, i.e. the calls will look like this: </p> <pre><code>http://myserver/playlist/336385.smil/segment1_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 </code></pre> <p>Setting the headers as key-value pairs on the <a href="http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28android.content.Context,%20android.net.Uri,%20java.util.Map%3Cjava.lang.String,%20java.lang.String%3E%29" rel="nofollow">setDataSource (Context context, Uri uri, Map headers)</a> does not work, at least the way I am trying it. Here is how I create the headers from the original url parameters:</p> <pre><code>String paramString =playlistUrl.substring( playlistUrl.indexOf("?") + 1); String paramsSplit[] = paramString.split("&amp;"); HashMap&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;(); for(int i= 0;i&lt;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"); } </code></pre> <p>Thus I end up with a Map with my parameters in it (i.e. Map'&lt;'param, value'>'), but adding these headers does not work.</p> <p>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:</p> <pre><code>#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&amp;param1=value1&amp;param2=value2" #EXT-X-VERSION:2 #EXT-X-MEDIA-SEQUENCE:1 #EXTINF:10, http://myserver/playlist/336385.smil/segment1_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXTINF:10, http://myserver/playlist/336385.smil/segment2_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXTINF:10, http://myserver/playlist/336385.smil/segment3_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXTINF:10, http://myserver/playlist/336385.smil/segment4_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXTINF:10, http://myserver/playlist/336385.smil/segment5_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXTINF:10, http://myserver/playlist/336385.smil/segment6_3_av.ts?e=6889c9ad1087852b&amp;param1=value1&amp;param2=value2 #EXT-X-ENDLIST </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload