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

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

JavaScript高级程序设计:第2版

JavaScript高级程序设计:第2版

Nicholas Zakas / 李松峰、曹力 / 人民邮电出版社 / 2010-7 / 89.00元

《JavaScript高级程序设计(第2版)》在上一版基础上进行了大幅度更新和修订,融入了近几年来JavaScript应用发展的最新成果,几乎涵盖了所有需要理解的重要概念和最新的JavaScript应用成果。从颇具深度的JavaScript语言基础到作用域(链),从引用类型到面向对象编程,从极其灵活的匿名函数到闭包的内部机制,从浏览器对象模型(BOM)、文档对象模型(DOM)到基于事件的Web脚本......一起来看看 《JavaScript高级程序设计:第2版》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具