Android: save sound as ringtone and notification

I save the sound from my application for use as a ringtone or notification sound. Here is the part of my code taken from this page :

ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, soundName); values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); values.put(MediaStore.Audio.Media.ARTIST, "artist"); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); values.put(MediaStore.Audio.Media.IS_ALARM, true); values.put(MediaStore.Audio.Media.IS_MUSIC, false); Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()); this.getContentResolver().insert(uri, values); 

My understanding is that the sound will be saved, as well as the ringtone, notification sound and alarm, since the flags are set to true. At least it works on the emulator, but on the device itself, the sound appears only in the list of ringtones, and I have no idea why.

EDIT: I tried to explore in more detail: deleting a line using "IS_RINGTONE" will not change anything (if you can use only one flag at a time), the sound does not appear in the list using notification sounds.

Any help is appreciated.

Regards, Select0r

+4
source share
3 answers

I played with the path to solve this problem, and here is the solution that I have come up with so far:

In the emulator, the path does not matter, the sound appears in the ringtone, as well as in the list of notifications.

On the phone, sound will appear in the ringtone list if the file is saved in /media/audio/ringtones OR /media/audio/ , but NOT as a notification.
If I use the path /media/audio/notifications , the sound finally appears in the notification list (!), But NOT in the ringtones.

So it seems that I need to save the sound twice to get on both lists? (But why save it on an emulator?)

If there is another way to get sounds in both lists (or even in three lists, there are also sound signals, I just didn’t bother to play with them), only saving them once, please let me know. (Right now, my workaround is a dialog that allows the user to choose whether to save the sound as a ringtone or notification.)

+4
source

Does this help if you try to switch from MediaStore.Audio.Media to AudioColumns? Like this:

 values.put(AudioColumns.ARTIST, "artist"); values.put(AudioColumns.IS_RINGTONE, true); values.put(AudioColumns.IS_NOTIFICATION, true); values.put(AudioColumns.IS_ALARM, true); values.put(AudioColumns.IS_MUSIC, false); 

The link does not say that MediaStore.Audio.Media is deprecated, but I think AudioColumns is now used. I use it in my application and it seems to work.

+1
source

You need to look at the API of the media player in Android ieandroid.media.MediaPlayer; android.media.MediaPlayer.OnCompletionListener. Here is: http://developer.android.com/reference/android/media/package-summary .... Here is how you play the sound when you start the click listener:

  private OnClickListener btnDontBeStupidListener = new OnClickListener() { public void onClick(View v) { //Toast.makeText(getBaseContext(), "Don't Be Stupid audio file is being played", Toast.LENGTH_SHORT).show(); final MediaPlayer mp = MediaPlayer.create(iMEvil.this, R.raw.stupid); //mp.start(); try { mp.start(); //mp.release(); }catch(NullPointerException e){ Log.v("MP error", e.toString()); } mp.setOnCompletionListener(new OnCompletionListener(){ // @Override public void onCompletion(MediaPlayer arg0) { mp.release(); } }); } }; 

Also, did you try to record sound during playback? Try it and see if it works.

-2
source

All Articles