Android Save Music uri SharedPreference and play later

How to use SharedPreferences to save music URI and play later using MediaPlayer

Android simple tutorial to play music from saved path. Save path of music uri SharedPreference its easy to use for later suppose and want play particular background music fro your application. You can use this code snippets.

package attraction.mystore;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class MainActivity extends AppCompatActivity {
Button audioSet,playUri;
MediaPlayer mp,mnp;
Uri uri;
Context c;
SharedPreferences preferences;
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
prepareForm();
playUri = (Button) findViewById(R.id.play);
audioSet = (Button) findViewById(R.id.set_audio);
audioSet.setOnClickListener(new View.OnClickListener() {
@Override
publicvoidonClick(Viewview) {
System.gc();
Intent intent =newIntent(android.content.Intent.ACTION_GET_CONTENT);
Uri data =Uri.parse(“file:///sdcard/media/audio/notifications”);
String type =”audio/mp3″;
intent.setDataAndType(data, type);
startActivityForResult(intent, 1);
}
});
playUri.setOnClickListener(new View.OnClickListener() {
@Override
publicvoidonClick(Viewview) {
mnp = new MediaPlayer();
try {
preferences = getSharedPreferences(“mypref”, 0);
String audiouri = preferences.getString(“useraudio”, null);
if (audiouri !=null) {
Uri mUri =Uri.parse(audiouri);
mnp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mnp.setDataSource( c, mUri);
mnp.prepare();
mnp.start();
}
}
catch (Exception exception) {
exception.printStackTrace();
Toast.makeText(MainActivity.this, “Error”, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protectedvoidonActivityResult(intrequestCode, intresultCode, Intentdata) {
if (requestCode ==1) {
if (data !=null) {
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
uri = data.getData();
String pathm = uri.toString();
SharedPreferences.Editor editor = getSharedPreferences(“mypref”, 0).edit();
editor.putString(“useraudio”, pathm);
editor.commit();
if (uri !=null) {
mp.setDataSource(getApplicationContext(), uri);
mp.prepare();
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
publicvoidonCompletion(MediaPlayermp) {
mp.release();
}
});
}
else {
Toast.makeText(MainActivity.this, “Data is null…”, Toast.LENGTH_SHORT).show();
}
}
catch (Exception exception) {
exception.printStackTrace();
Toast.makeText(MainActivity.this, “Error”, Toast.LENGTH_SHORT).show();
}
}
}
}
}

Music uri sharedpreference android example source code

If a path is saved in sharedpreferences it will saved till you clear it or delete it form sharedPrefernce. Suppose you uninstall application and install again it will be saved it is like saving settings. Only possible through remove path is clear data for particular application. Learn more about sharedPrefernces from Android official page.

Visit Blog NextGenEarn.

Add a Comment

Your email address will not be published. Required fields are marked *