java – 如何使用MediaPlayer setDataSource包含http标头?

栏目: 后端 · 前端 · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/8959300/how-do-i-include-http-headers-with-mediaplayer-setdatasource
我将URI传递给MediaPlayer对象的 setDataSource method

.我的目标api版本小于14,所以相信我不能使用允许头文件的新方法.如何在MediaPlayer请求中包含标题(特别是认证标头),并且仍然支持较旧的Android设备?

我的代码看起来像:

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

背景:

方法 setDataSource(Context context, Uri uri, Map<String, String> headers) 已经被包括在SDK(标记为@hide)很长一段时间(至少从Froyo 2.2.x,API 8级),请查看更改历史记录:

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

自从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

解决方法:

在Ice Cream Sandwich 4.0.x,API Level 14之前,我们可以使用反射来调用这个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://stackoverflow.com/questions/8959300/how-do-i-include-http-headers-with-mediaplayer-setdatasource


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Cracking the Coding Interview

Cracking the Coding Interview

Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95

Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具