简体   繁体   English

Android Mediaplayer无法使用路径/ documents / audio:1159加载Uri

[英]Android mediaplayer won't load Uri with path /documents/audio:1159

I'm trying to play audio files with the built in mediaplayer in Android Studio. 我正在尝试使用Android Studio中的内置mediaplayer播放音频文件。 I'm using the code below to call an intent and open a third party file manager to get a files Uri and from that the file path whoich I store somewhere. 我正在使用下面的代码调用意图,并​​打开第三方文件管理器来获取文件Uri,并从该文件路径中获取我存储在某处的文件路径。 Anyway, if I use a file manager like ES File Explorer I get a path that looks like "/sdcard/some directory/test.mp3" however, if I use the built in file explorer I get a path like "/documents/audio:1159" for the same file. 无论如何,如果我使用像ES File Explorer这样的文件管理器,我会得到一个看起来像“ / sdcard / some directory / test.mp3”的路径,但是,如果我使用内置文件浏览器,我会得到一个像“ / documents / audio”的路径。 :1159“用于相同的文件。 I understand that the latter is an "asset" but when I try to feed this into mediaplayer I get an exception. 我知道后者是一个“资产”,但是当我尝试将其输入到媒体播放器中时,会出现异常。 What am I doing wrong? 我究竟做错了什么?

The code below shows the intent method I'm using to get the filepath and the code below that shows how I use that file path to get a Uri and feed this into mediaplayer. 下面的代码显示了我用来获取文件路径的intent方法,下面的代码显示了我如何使用该文件路径获取Uri并将其输入到mediaplayer中。 Just to be clear file paths like "/sdcard/some directory/test.mp3" work fine. 只是为了清楚起见,“ / sdcard / some directory / test.mp3”之类的文件路径可以正常工作。 File paths like "/documents/audio:1159" don't. 诸如“ / documents / audio:1159”之类的文件路径不存在。

final View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
public void onClick(final View v) {

    int resID2 = v.getId();

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("audio/*");
    try {
        startActivityForResult(intent,resID2); }
    catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Please install a file manager",Toast.LENGTH_LONG).show();
    }
}

}; };

public void onActivityResult(int requestCode, int resultCode, Intent result) { public void onActivityResult(int requestCode,int resultCode,Intent result){

if (resultCode == RESULT_OK)
{
    Uri data = result.getData();
    String thePath = data.getPath();
    // Do something with the file path
}

} }

Code used to start mediaplayer based on the file path retrieved from above 用于根据从上面检索的文件路径启动mediaplayer的代码

Uri myUri = Uri.parse(filePath);

mediaPlayer = new MediaPlayer();    
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
    mediaPlayer.setDataSource(getApplicationContext(), myUri);
    mediaPlayer.prepare();  
    mediaPlayer.start();
} catch (IOException e) {} 

I guess you cannot use Uri like 我想你不能像这样使用Uri

/documents/audio:1159 / documents / audio:1159

when uses Intent MediaPlayer 使用Intent MediaPlayer时

EDITTED: Try this code for getting filepath from assets folder 编辑:尝试使用此代码从资产文件夹获取文件路径

AssetManager am = getAssets();
InputStream inputStream = am.open(file:///android_asset/myfoldername/myfilename);
File file = createFileFromInputStream(inputStream);

private File createFileFromInputStream(InputStream inputStream) {

   try{
      File f = new File(my_file_name);
      OutputStream outputStream = new FileOutputStream(f);
      byte buffer[] = new byte[1024];
      int length = 0;

      while((length=inputStream.read(buffer)) > 0) {
        outputStream.write(buffer,0,length);
      }

      outputStream.close();
      inputStream.close();

      return f;
   }catch (IOException e) {
         //Logging exception
   }

return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM