-1

I'm developing an app, and in that app I have one button, named 'choose sound'. When user will click this button, he/she should be asked to choose any audio file from the file manager/memory.

So, I know that for this, I'll have to use Intent.Action_GetData. I'm doing the same:

//code start
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);

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

  if(requestCode == 1){

    if(resultCode == RESULT_OK){

        //the selected audio.
        Uri uri = data.getData(); 
int SoundID=soundPool.Load(uri.toString(), 1);
//SoundPool is already constructed and is working perfectly for the resource files
PlaySound(SoundID);
//PlaySound method is already defined
    }
  }
  super.onActivityResult(requestCode, resultCode, data);
}
//end of code

but it's not working
Now, in OnActivityResult, I'm not getting that how to load the proper URI of the file selected by user, because before Android 4.4, it returns the different URI and after Android 4.4 it returns the different URI on intent.GetData();. Now, what I have to do?

Also, I know that for playing the audio file, I'll have to use SoundPool, and I have the code for that too, in fact it's working fine for the resource/raw/audio files, but how to load/play files in SoundPool from this URI?

3 Answers3

2

In your onActivityResult(), do the following changes:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && data != null)
    {
        String realPath = null;
        Uri uriFromPath = null;

        realPath = getPathForAudio(YourActivity.this, data.getData());
        uriFromPath = Uri.fromFile(new File(realPath)); // use this uriFromPath for further operations
    }
}     

Add this method in your Activity:

public static String getPathForAudio(Context context, Uri uri)
 {
    String result = null;
    Cursor cursor = null;

    try {
        String[] proj = { MediaStore.Audio.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
            result = cursor.getString(column_index);
            cursor.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return result;
}

Hope It will do your job. You can play audio using MediaPlayer class also

  • Hi Dear Friend, thanks a lot, now I'm able to get the proper path to the file, but still unable to load the sound via SoundPool.Load Method. I've tried giving the file name manually to this method but it doesn't work, I think that there's some problem with method itself: SoundPool.Load(string path, int priority) can you please help? –  Feb 08 '17 at 17:58
  • Hi, I've just tried the same with MediaPlayer class and it's working in it. Unable to understand then why is it not working in SoundPool then. any ideas? –  Feb 08 '17 at 18:37
  • I'm also not confident about SoundPool because I never used it. That's the reason I suggested you about MediaPlayer. Have to study about SoundPool. :) I'll let you know if i find something. – Prathamesh Toradmal Feb 08 '17 at 19:11
  • Hi Prathamesh Toradmal, ok bro. please let me know if you find something. actually, I can't use mediaplayer class as I need the very responsive playback of the sounds for some special purpose. :( Now I'm stuck –  Feb 09 '17 at 05:29
  • Hello @AkashKakkar, I think SoundPool can't play audio from sd card. You can play audio if it is in raw folder. For your reference check this link: http://stackoverflow.com/questions/9528238/playing-audio-file-from-sdcard – Prathamesh Toradmal Feb 09 '17 at 06:04
  • ya, I also feel the same, but the method is there which is SoundPool.Load(String path, int Priority) don't know why it doesn't work :( –  Feb 09 '17 at 06:40
0

You can put below codes in your project when you want to select audio.

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

And override onActivityResult in the same Activity, as below

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

  if(requestCode == 1){

    if(resultCode == RESULT_OK){

        //the selected audio.
        Uri uri = data.getData(); 
    }
  }
  super.onActivityResult(requestCode, resultCode, data);
}
  • I've already tried this approach, but this code is not working. first of all, every app is returning the different URI, secondly, SoundPool.Load methoad is not accepting any URI and not loading the sound –  Feb 08 '17 at 11:06
0

Try to get the path :

 //method to get the file path from uri
    public String getPath(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String document_id = cursor.getString(0);
        document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
        cursor.close();

        cursor = getContentResolver().query(
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
        cursor.moveToFirst();
        String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
        cursor.close();

        return path;
    }

then load it :

s2 = soundPool.load(YOU_PATH, PRIORITY);