我怎样包括HTTP标头与MediaPlayer的的setDataSource? [英] How do I include http headers with MediaPlayer setDataSource?

查看:647
本文介绍了我怎样包括HTTP标头与MediaPlayer的的setDataSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的URI <一个href="http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28java.lang.String%29">setDataSource在MediaPlayer对象的方法。我针对API版本低于14,所以认为我不能使用新的方法,使被列入头。我怎么能有头(具体而言,认证头)与MediaPlayer的请求,并且仍然支持旧的Andr​​oid设备?

我的code是这样的:

  mediaPlayer.setDataSource(URL);
 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
 。媒体播放器prepareAsync();
 

解决方案

背景:

该方法<一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">setDataSource(Context的背景下,开放的URI,地图&LT;字符串,字符串&GT;头)已包含在SDK(标记为@hide)在相当长的时间(至少从升级Froyo 2.2.x的,API等级8),检查了改变历史:

<一个href="https://github.com/android/platform_frameworks_base/commit/256430093679e1d62b54fb0c852126e54d162f6f">API扩展:支持任意指定额外的请求头的地图,指定要发挥媒体数据的URI时

此外,由于冰淇淋三明治4.0.x的已取消隐藏和向公众开放,API级别14:

<一个href="https://github.com/android/platform_frameworks_base/commit/72e738a428d7bb39a371da6105f7e2084e67e39d">Unhide MediaPlayer的的setDataSource方法,它采用可选的HTTP标头传递给服务器

解决方法:

此前冰淇淋三明治4.0.x的,API级别14,我们可以使用反射调用这个隐藏API:

 开放的URI = Uri.parse(路径);
地图&LT;字符串,字符串&GT;标题=新的HashMap&LT;字符串,字符串&GT;();
headers.put(键1,值1);
headers.put(KEY2,值2);

mMediaPlayer =新的MediaPlayer();
//使用Java反射调用隐藏API:
方法方法= mMediaPlayer.getClass()实现getMethod(的setDataSource,新的等级[] {Context.class,Uri.class,Map.class})。
method.invoke(mMediaPlayer,新的对象[] {此,URI,头});
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
。mMediaPlayer prepareAsync();

......
 

I am passing a URI to the setDataSource method of the MediaPlayer object. I am targeting api version less than 14, so believe that I cannot use the new method that allows headers to be included. How can I include headers (specifically, authentication header) with the MediaPlayer request and still support older Android devices?

My code looks like:

 mediaPlayer.setDataSource(url);
 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
 mediaPlayer.prepareAsync();

解决方案

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();

... ...

这篇关于我怎样包括HTTP标头与MediaPlayer的的setDataSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆