Android 无法将URI加载到MediaPlayer以播放SD卡中的声音

Android 无法将URI加载到MediaPlayer以播放SD卡中的声音,android,button,uri,android-mediaplayer,android-sdcard,Android,Button,Uri,Android Mediaplayer,Android Sdcard,我选择一个声音,从中使用意图选择器从SD卡加载URI Intent loadIntent = new Intent(); loadIntent.setAction(Intent.ACTION_GET_CONTENT); loadIntent.setType("audio/*"); startActivityForResult(loadIntent.createChooser(loadIntent, "Select Audio"), SOUND_

我选择一个声音,从中使用意图选择器从SD卡加载URI

Intent loadIntent = new Intent();
loadIntent.setAction(Intent.ACTION_GET_CONTENT); 
loadIntent.setType("audio/*");
startActivityForResult(loadIntent.createChooser(loadIntent, "Select Audio"),
                         SOUND_LOAD_REQUEST);}
我能够使用几种不同的方法将数据和URI返回到onActivityResult中

if (requestCode == SOUND_LOAD_REQUEST)
  if (resultCode == RESULT_OK) { //make sure the request was successful
        Uri soundUri1 = data.getData();
        String soundPath = data.getDataString(); 
        Uri soundUri2 = Uri.parse(soundPath);
        pathTwo = soundUri.getEncodedPath(); 
声程产额content://media/external/audio/media/1005

路径2/外部/音频/媒体/1005

我已经尝试从上面获取两个URI和从上面获取两个路径,并将它们加载到MediaPlayer和soundPool中,但我似乎都无法成功加载声音

以下是我如何加载:

      mloadedSound = mSoundPool.load(pathTwo, 1);

      mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
            @Override
            public void onLoadComplete(SoundPool mSoundPool, int sampleId, int status) {
                loaded = true;
                  }
          });

这两种方法都会导致加载错误。对于soundpool,它说没有为媒体播放器加载声音,它不会运行代码。 我不确定mediaPlayer或soundPool是否更适合该应用程序。我只是用一个按钮从SD卡加载一个声音,然后用一秒钟播放这个声音。我更喜欢soundPool,因为它简单,但我不知道它是否能处理更大的mp3文件

关于如何加载URI有什么建议吗?当我尝试将URI加载到MediaPlayer时,我得到一个失败的IOException。
我是否必须使用ContentResolver或MediaStore来解析URI?感谢您的帮助和建议。

我已经解决了。似乎错误在于,在KitKat中,您必须声明权限android.permission.READ\u EXTERNAL\u存储。 我最初没有收到这个错误,因为我的MediaPlayer的onClickListener在onCreate方法中,而不是OnActivityResultMethod。 我让一切工作,并张贴了我的加载按钮代码和我的onActivityResultCode下面的其他人谁可能有这个问题

    //Load Sound Button
    final View loader = findViewById(R.id.load_button);
    loader.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent loadIntent = new Intent();
            loadIntent.setAction(ACTION_GET_CONTENT);
            loadIntent.setType("audio/*");
            startActivityForResult(createChooser(loadIntent, "Select Audio"), SOUND_LOAD_REQUEST);}
    });

    //End of Load Sound Button
&

快乐编码

    //Load Sound Button
    final View loader = findViewById(R.id.load_button);
    loader.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent loadIntent = new Intent();
            loadIntent.setAction(ACTION_GET_CONTENT);
            loadIntent.setType("audio/*");
            startActivityForResult(createChooser(loadIntent, "Select Audio"), SOUND_LOAD_REQUEST);}
    });

    //End of Load Sound Button
//Start of onActivityResult
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SOUND_LOAD_REQUEST)
            if (resultCode == RESULT_OK) {
                mediaPlayer.reset();
                Uri soundUri = data.getData();  Log.i(TAG, "Intent data" + data.getData().toString());
                try {
                    mediaPlayer.setDataSource(this, soundUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    mediaPlayer.prepareAsync();

                //Play Sound Button
                final View mrButton = findViewById(R.id.push_button);
                mrButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    if(mediaPlayer.isPlaying()){
                        mediaPlayer.pause();
                    }else{
                        mediaPlayer.start();
                    }
                }
            });
            //End of Play Sound Button
        }
}

//End of onActivityResult