Search code examples
androidandroid-studiosoundpool

SoundPool API not playing when loaded with string path


I'm getting my feet wet on SoundPool API, I've made just a small app to learn and see how dynamic SoundPool is. But I don't know why when I load a String path (URI in String format) It simple doesn't plays!

In debug mode I realized that when I go to chose a sound using my Intent and them I load the path of the sound to the SoundPool it gives all the time a SoundID of 0. not sure if it is a problem..

Any help would be appreciated!

Code

import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements     View.OnClickListener {

private Button mKey1;
private Button mKey2;
private Button mChange;
private Button mUpload;

int mSound1;
int mSound2;
String path;



private SoundPool mSoundPool;
private boolean mSelectKey = false;

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

    mKey1 = (Button) findViewById(R.id.key1);
    mKey2 = (Button) findViewById(R.id.key2);
    mChange = (Button) findViewById(R.id.Change);
    mUpload = (Button) findViewById(R.id.Upload);

    mKey1.setOnClickListener(this);
    mKey2.setOnClickListener(this);
    mChange.setOnClickListener(this);
    mUpload.setOnClickListener(this);

    if ( Build.VERSION.SDK_INT >= 21 ) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        mSoundPool = new SoundPool.Builder().setMaxStreams(2).setAudioAttributes(audioAttributes).build();
        }else {

        mSoundPool = new SoundPool(50, AudioManager.STREAM_MUSIC,0);
    }

    mSound1 = mSoundPool.load(this, R.raw.sound1, 1);
    mSound2 = mSoundPool.load(this, R.raw.sound2, 1);

    mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            Toast.makeText(MainActivity.this,"ready!",Toast.LENGTH_SHORT).show();
        }
    });

}


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

    if (resultCode == RESULT_OK){
        path = data.getData().toString();
    }


}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.key1:
            if (mSelectKey){
                mSound1 = mSoundPool.load(path,1);
                mSelectKey = false;
                break;
            }
            mSoundPool.play(mSound1,1,1,1,0,1);
            break;
        case R.id.key2:
            if (mSelectKey){
                mSound2 = mSoundPool.load(path,1);
                mSelectKey = false;
                break;
            }
            mSoundPool.play(mSound2,1,1,1,0,1);
            break;
        case R.id.Change:
            mSound1 = mSoundPool.load(this,R.raw.sound6,1);
            mSound2= mSoundPool.load(this,R.raw.sound3,1);
            break;
        case R.id.Upload:
            mSelectKey = true;
            Intent uploadFile = new Intent(Intent.ACTION_GET_CONTENT);
            uploadFile.setType("audio/mp3");
            startActivityForResult(uploadFile,1);
            break;

    }



}

}


Solution

  • path = data.getData().toString();
    

    First, this will return the string representation of a Uri. That will contain a scheme, such as content:. AFAIK, SoundPool load() takes a filesystem path.

    Second, the Uri that you get back may not be a file, at least at a path that you can access.

    Given the available flavors of load(), I would recommend that you try the one that takes an AssetFileDescriptor as input, using openAssetFileDescriptor() on a ContentResolver to get that AssetFileDescriptor, and see if you have better luck.