Saving audio content Uri in sharedpreference to play later

0

I am trying to design an Alarm app where Audio is played when the device is shaken.

I am selecting the Audio from file system using "ACTION_GET_CONTENT" in Intent with ActivityForResult. I am able to fetch the content uri, saving it in SharedPreference and playing it with MediaPlayer on a different Activity passing the URI in Intent.

But, once I close my app and reopen it and try to play the Audio from the URI in SharedPrederence, the Audio is not played.

What can be done so that I can play Audio from saved content uri in SharedPreference?

Code to fetch uri from Storage:

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


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

  if(requestCode == 1){

    if(resultCode == RESULT_OK){

        //the selected audio.
        String aud_uri = data.getData();

        String quri = aud_uri.toString();



        //Write to shared preferences

    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("quake_uri",quri);
    editor.commit();
        //

    }
  }

Code to fetch from sharedpreference

sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String quri = sharedPref.getString("quake_uri", "defaultString");

Code to play audio in different activity after passing uri through intent

String audio = getIntent().getStringExtra("audio");
Uri uri = Uri.parse(audio);
Mediaplayer player = new MediaPlayer();

...

Logcat when audio is not played

05-26 12:53:44.103: E/MediaPlayer(17183): Unable to create media player
05-26 12:53:44.103: V/Error(17183): setDataSource failed.: status=0x80000000
05-26 12:53:44.113: W/System.err(17183): java.io.IOException: setDataSource failed.: status=0x80000000
05-26 12:53:44.113: W/System.err(17183):    at android.media.MediaPlayer.nativeSetDataSource(Native Method)
05-26 12:53:44.113: W/System.err(17183):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1061)

I am passing the uri to a service which in turn opens the activity to play audio with uri passed from the service with intent

android
android-intent
sharedpreferences
android-mediaplayer
android-contentprovider
asked on Stack Overflow May 26, 2015 by Roy • edited May 26, 2015 by Roy

1 Answer

0

I was struggling with this for a while, I managed to get it working by replacing 'Intent.ACTION_GET_CONTENT' with 'Intent.ACTION_OPEN_DOCUMENT'.

It seems that if you want the reference to this file to be valid for multiple sessions you need to use the open document action. I found this out in the following Android documentation: https://developer.android.com/guide/topics/providers/document-provider

I realize that you asked this question a few years ago, but hopefully this information is still of some use.

answered on Stack Overflow Jan 2, 2019 by Trev Cavill

User contributions licensed under CC BY-SA 3.0