2

I'm trying to play an audio file using SoundPool to change the speed of play dynamically. However, when I try to load the file using

soundPool.load(mediaPath, 0);

where mediaPath is the path to the file which I get using an Intent. I get the same error when I'm hard-coding the file path.

I get the following error:

02-20 14:57:55.570  17079-17079/net.stupidiot.pacekeeper E/SoundPool﹕ error loading /document/audio:190688

I've visited this and this post. But none of it addresses my situation.

Please help.

Community
  • 1
  • 1
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • It might be because of relative path. Have you tried using absolute path? – Qandeel Abbassi Feb 20 '16 at 21:41
  • @QandeelAbbasi, Yes. I did. I used hard-coded absolute path. But I get the same error. I think I might be missing some permissions. – Rahul Bobhate Feb 20 '16 at 22:27
  • 1
    Looking at the implementation over [here](https://github.com/android/platform_frameworks_base/blob/master/media/java/android/media/SoundPool.java) i dont think there should be problem even after giving absolute path. Can you try it on some other audio file, i mean try opening some other audio file in different directory – Qandeel Abbassi Feb 20 '16 at 22:44
  • It is possible that you might not have the permission to read the file – Qandeel Abbassi Feb 20 '16 at 22:56
  • @QandeelAbbasi, Checked that. I've added the permission `READ_EXTERNAL_MEDIA` in the Manifest, as well as I'm dynamically granting the access to the returning URI. Still getting the same error. – Rahul Bobhate Feb 20 '16 at 22:58

1 Answers1

2

I got the solution for your problem. Its definitely a problem of path. I implemented the code for using soundpool with audio intent. At first i was getting path from URI returned in onActivityResult like this:

Uri uri = data.getData(); String path = uri.getPath();

And it gave me the exact same error like yours. Then i hardcoded the path of one of my audio files something like this /storage/extSdCard/Extra/predeftones/predefringtones/Ahoy.aac and it worked. I came to know that there is definitely something wrong with the path, so i implemented a method, which returns real path of file, as follows:

 private String _getRealPathFromURI(Context context, Uri contentUri) {
        String[] proj = { MediaStore.Audio.Media.DATA };
        CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

I pass the uri to this method and it returns me the real path then i use that path in load method of soundpool. The whole code is given below:

import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    Button b;
    private boolean loaded;
    private int soundID;
    private SoundPool soundPool;
    private float volume;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        b = (Button) findViewById(R.id.btn);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loaded) {
                    soundPool.play(soundID, volume, volume, 1, 0, 1f);
                    Log.e("Test", "Played sound");
                }
            }
        });

        Intent intent_upload = new Intent();
        intent_upload.setType("audio/*");
        intent_upload.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent_upload, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {

            if (resultCode == RESULT_OK) {
                try {
                    //the selected audio.
                    Uri uri = data.getData();
                    String path = _getRealPathFromURI(MainActivity.this, uri);
                    Log.e("test", "" + path);
                    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
                    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                        @Override
                        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                            loaded = true;
                        }
                    });
                    soundID = soundPool.load(path, 1);
                    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                    float actualVolume = (float) audioManager
                            .getStreamVolume(AudioManager.STREAM_MUSIC);
                    float maxVolume = (float) audioManager
                            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                    volume = actualVolume / maxVolume;
                    if (loaded) {
                        soundPool.play(soundID, volume, volume, 1, 0, 1f);
                        Log.e("Test", "Played sound");
                    }
                } catch (Exception e) {
                    Log.e("Test", "error" + e);
                }

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private String _getRealPathFromURI(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Audio.Media.DATA};
        CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}

Note: According to Android reference guide getPath returns decoded path. Idk what a decoded path is but its not the real path for sure

Qandeel Abbassi
  • 999
  • 7
  • 31