[英]Android MediaPlayer unable to setDataSource uri
I'm trying to store the uri of a previously played song, using sharedPreferences. 我正在尝试使用sharedPreferences存储以前播放的歌曲的uri。 But when if fetch and reconstruct the uri in order to play the song i get the following error message:
但是,如果获取并重建uri以播放歌曲,则会收到以下错误消息:
05-03 20:43:14.642 8617-8716/com.stopwatch.app W/MediaPlayer: Couldn't open file on client side;
05/03/20:43:14.642 8617-8716 / com.stopwatch.app W / MediaPlayer:无法在客户端打开文件; trying server side: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{2c95b0 8617:com.stopwatch.app/u0a175} (pid=8617, uid=10175) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
尝试服务器端:java.lang.SecurityException:权限拒绝:从ProcessRecord {2c95b0 8617:com.stopwatch.app/u0a175}(pid = 8617,uid = 10175)打开提供程序com.android.providers.media.MediaDocumentsProvider需要android。 Permission.MANAGE_DOCUMENTS或android.permission.MANAGE_DOCUMENTS
05-03 20:43:14.658 8617-8716/com.stopwatch.app I/MediaPlayer: setDataSource(content://com.android.providers.media.documents/document/audio%3A18504)
05-03 20:43:14.658 8617-8716 / com.stopwatch.app I / MediaPlayer:setDataSource(content://com.android.providers.media.documents/document/audio%3A18504)
05-03 20:43:14.661 8617-8716/com.stopwatch.app E/MediaPlayer: Unable to create media player
05-03 20:43:14.661 8617-8716 / com.stopwatch.app E / MediaPlayer:无法创建媒体播放器
05-03 20:43:14.662 8617-8716/com.stopwatch.app W/System.err: java.io.IOException: setDataSource failed.: status=0x80000000
05-03 20:43:14.662 8617-8716 / com.stopwatch.app W / System.err:java.io.IOException:setDataSource失败。:status = 0x80000000
I don't get way I would need android.permission.MANAGE_DOCUMENTS and my IDE tell me that it is a permission only suited for system applications. 我不知道需要android.permission.MANAGE_DOCUMENTS的方式,我的IDE告诉我这是仅适用于系统应用程序的权限。 My code looks like this:
我的代码如下所示:
if(prefs.contains("AudioFile")){
try {
String UriString = prefs.getString("AudioFile", null);
Uri uri = Uri.parse(UriString);
mediaPlayer.setDataSource(this, uri);
Gdx.app.log("Android Media Player", "Successfully got data");
} catch (IOException e) {
e.printStackTrace();
Gdx.app.log("Android Media Player", e.getMessage());
}
try {
mediaPlayer.prepare();
Gdx.app.log("Android Media Player", "Successfully prepared content");
} catch (IOException e) {
e.printStackTrace();
Gdx.app.log("Android Media Player", e.getMessage());
}
}
I have tried running it in the onCreate
method (which seems to be a bad idea) and through another method which is called on a button click, both failed. 我尝试过在
onCreate
方法中运行它(这似乎是个坏主意),并通过另一个在单击按钮时调用的方法都失败了。
Notice that if I start the application and retrieve the uri through an intent at initiate the mediaPlayer through: 请注意,如果我启动应用程序并通过以下意图通过启动mediaPlayer来检索uri:
protected void onActivityResult (int requestCode, int resultCode, Intent data){
if(requestCode == MUSIC_REQUEST){
if(resultCode == RESULT_OK){
Uri myUri = data.getData();
SharedPreferences.Editor editor = prefs.edit();
editor.putString("AudioFile", myUri.toString());
editor.commit();
System.out.println(myUri);
try {
mediaPlayer.setDataSource(this, myUri);
Gdx.app.log("Android Media Player", "Succesfully got data");
} catch (IOException e) {
e.printStackTrace();
Gdx.app.log("Android Media Player", e.getMessage());
}
try {
mediaPlayer.prepare();
Gdx.app.log("Android Media Player", "Succesfully prepared content");
} catch (IOException e) {
e.printStackTrace();
Gdx.app.log("Android Media Player", e.getMessage());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
I have no problems, not even if I substitute with mediaPlayer.setDataSource(this, Uri.parse(myUri.toString)). 我没有问题,即使我用mediaPlayer.setDataSource(this,Uri.parse(myUri.toString))代替也没有问题。
You cannot persist a Uri
value and expect it to work later. 您无法持久保存
Uri
值,并希望它以后能正常工作。 Those that have a content
scheme offer you access to the content for a very short time . 那些具有
content
计划的程序使您可以在很短的时间内访问内容 。
If you are using ACTION_OPEN_DOCUMENT
to get your Uri
, you can request long-term access to the content via takePersistableUriPermission()
on a ContentResolver
. 如果您使用
ACTION_OPEN_DOCUMENT
来获取Uri
,则可以通过ContentResolver
上的takePersistableUriPermission()
请求对内容的长期访问。 You will still have to deal with the possibility that the user might move or delete the content, though. 但是,您仍然必须处理用户可能移动或删除内容的可能性。
If you are using ACTION_GET_CONTENT
to get your Uri
, either switch to ACTION_OPEN_DOCUMENT
(if your minSdkVersion
is 19 or higher) or make a copy of the content, then use your copy. 如果您使用
ACTION_GET_CONTENT
来获取Uri
,请切换到ACTION_OPEN_DOCUMENT
(如果您的minSdkVersion
为19或更高),或制作内容的副本,然后使用副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.