How do I include http headers with MediaPlayer setDataSource?

被刻印的时光 ゝ 提交于 2019-12-02 18:43:12

Background:

The method setDataSource(Context context, Uri uri, Map<String, String> headers) has been included in the SDK (marked as @hide) for quite a long time (at least since Froyo 2.2.x, API Level 8), check out the change history:

API Extension: Support for optionally specifying a map of extra request headers when specifying the uri of media data to be played

And has been unhidden and open to public since Ice Cream Sandwich 4.0.x, API Level 14:

Unhide MediaPlayer's setDataSource method that takes optional http headers to be passed to the server

Workaround:

Prior to Ice Cream Sandwich 4.0.x, API Level 14, we can use reflection call this hide API:

Uri uri = Uri.parse(path);
Map<String, String> headers = new HashMap<String, String>();
headers.put("key1", "value1");
headers.put("key2", "value2");

mMediaPlayer = new MediaPlayer();
// Use java reflection call the hide API:
Method method = mMediaPlayer.getClass().getMethod("setDataSource", new Class[] { Context.class, Uri.class, Map.class });
method.invoke(mMediaPlayer, new Object[] {this, uri, headers});
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepareAsync();

... ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!