I have a dropdown list (spinner
) which shows all the MP3 music files that I have in my external storage. I want that MP3 music to play every time the notification pops up, which will be selected by the user.
The code for reading all the MP3 files in the external storage:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
};
EnableRuntimePermission();
Cursor cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
spin.setOnItemSelectedListener(this);
List<String> songs = new ArrayList<String>();
while (cursor.moveToNext()) {
songs.add(cursor.getString(0));
}
public void EnableRuntimePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(
Pop_up_2.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Toast.makeText(Pop_up_2.this, "Now you can read music files", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(Pop_up_2.this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE}, RequestPermissionCode);
}
}
The code for creating the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_today_black_24dp)
.setContentTitle(s3)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notification=NotificationManagerCompat.from(context);
notification.notify(NOTIFICATION_ID,builder.build());
Edit: I was wondering if there'd be any way to parse String item1 = parent.getItemAtPosition(position).toString();
into Uri and then set that Uri parameter into the RingtoneManager
Uri par=Uri.parse(item1);
Uri alarmSound = RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_NOTIFICATION,par);
.
.
.
.
.
builder.setSound(alarmSound);
But I get an error as Incompatible types.