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


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

查看所有标签

猜你喜欢:

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

Ethnography and Virtual Worlds

Ethnography and Virtual Worlds

Tom Boellstorff、Bonnie Nardi、Celia Pearce、T. L. Taylor / Princeton University Press / 2012-9-16 / GBP 21.00

"Ethnography and Virtual Worlds" is the only book of its kind - a concise, comprehensive, and practical guide for students, teachers, designers, and scholars interested in using ethnographic methods t......一起来看看 《Ethnography and Virtual Worlds》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器