Android的MediaPlayer在播放时设置Http请求头
来源:互联网 发布:mac口红专柜多少钱一支 编辑:程序博客网 时间:2024/05/01 12:40
需求
在MediaPlayer进行播放时需要设置特定的User-Agent。
首先看StackvOerflow上的讨论,总共有2篇文章。
讨论1
http://stackoverflow.com/questions/8959300/how-do-i-include-http-headers-with-mediaplayer-setdatasource
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();... ...
讨论2
http://stackoverflow.com/questions/23858472/android-mediaplayer-set-http-agent
I am building an audio streaming android application using MediaPlayer.
I setup the stream to play using:
mMediaPlayer.setDataSource(finalUrl);
Using this syntax the user agent in HTTP header is Dalvik 1.6.0
I tried to update the http user-agent header using:
Map<String, String> headers = new HashMap<String, String>();headers.put("User-Agent", "My custom User Agent name");mMediaPlayer.setDataSource(getApplicationContext(), finalUri, headers);
查看MediaPlayer.java(android 4.2.2)
/** * Sets the data source as a content Uri. * * @param context the Context to use when resolving the Uri * @param uri the Content URI of the data you want to play * @param headers the headers to be sent together with the request for the data * @throws IllegalStateException if it is called in an invalid state */ public void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { if (headers != null && headers.size() > 0){ IWindowManager mWindowManager = IWindowManager.Stub.asInterface( ServiceManager.getService(Context.WINDOW_SERVICE)); try{ Log.d("Hery", "setDataSource sys.html5.local true, run http"); mWindowManager.setSystemProp("sys.html5.local","true"); } catch (RemoteException ex) { ex.printStackTrace(); } }else{ IWindowManager mWindowManager = IWindowManager.Stub.asInterface( ServiceManager.getService(Context.WINDOW_SERVICE)); try{ Log.d("Hery", "setDataSource sys.html5.local false, run local"); mWindowManager.setSystemProp("sys.html5.local","false"); } catch (RemoteException ex) { ex.printStackTrace(); } } String scheme = uri.getScheme(); if(scheme == null || scheme.equals("file")) { setDataSource(uri.getPath()); return; } AssetFileDescriptor fd = null; try { ContentResolver resolver = context.getContentResolver(); fd = resolver.openAssetFileDescriptor(uri, "r"); if (fd == null) { return; } // Note: using getDeclaredLength so that our behavior is the same // as previous versions when the content provider is returning // a full file. if (fd.getDeclaredLength() < 0) { setDataSource(fd.getFileDescriptor()); } else { setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength()); } return; } catch (SecurityException ex) { } catch (IOException ex) { } finally { if (fd != null) { fd.close(); } } Log.d(TAG, "Couldn't open file on client side, trying server side"); setDataSource(uri.toString(), headers); return; }
- Android的MediaPlayer在播放时设置Http请求头
- android 中设置http请求头headers的Cookie
- Android Mediaplayer播放时设置频谱
- http 请求头设置
- android mediaPlayer 设置播放次数
- Android Http请求头与响应头的学习
- Android搭建HTTP服务,作为MediaPlayer的播放源来播放音频
- Android Http请求头与响应头
- HttpClient设置HTTP请求头Header
- HttpClient设置HTTP请求头Header
- HttpClient设置HTTP请求头Header
- angularjs http请求设置头信息
- Android如何在http头信息里设置参数
- Android如何在http头信息里设置参数
- Android如何在http头信息里设置参数
- Android如何在http头信息里设置参数
- http常见的请求头
- Http的请求头和响应头
- Jquery的filter,not,find和JS中的split,join
- HDU 3664 Permutation Counting
- 灵活运用 SQL SERVER FOR XML PATH
- Hibernate学习_014_级联关系中的CRUD操作
- 观众和专业眼中的“优秀”
- Android的MediaPlayer在播放时设置Http请求头
- hbase Invalid HFile block magic on hdfs system
- HDU 3952 Fruit Ninja(直线与线段相交枚举)
- Transform-style和Perspective属性
- 纯CSS箭头,气泡
- ps显示瞬时进程状态
- Linux:C应用程序终端输出字体颜色的改变
- Java知识点
- Struts2中action之间的跳转