转载

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

我将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

原文  https://codeday.me/bug/20181015/291616.html
正文到此结束
Loading...