I'm saving a sound from my app to be used as a ringtone or a notification sound. Here's 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.MediaCol开发者_JAVA百科umns.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 a ringtone, a notification sound and an alarm, as the flags are all set to "true". At least on the emulator this does work but on the actual device, the sound only shows up in the ringtone list, and I have no idea why.
EDIT: I've tried to investigate further: removing the line with "IS_RINGTONE" won't change anything (in case only one flag can be used at a time), the sound doesn't show up in the list with the notification-sounds.
Any help is appreciated.
Kind regards, Select0r
I played around with the path to get this solved and here's the solution I came up with so far:
On the emulator the path doesn't matter, the sound will appear in the ringtone as well as the notification-list.
On the phone, the sound will show up the the ringtone list if the file is saved to /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 any more.
So it seems I have to save the sound twice to get it into both list? (But why does saving it once work on the emulator?)
If there's another way of getting sounds into both lists (or even three lists, there are alarm tones as well, I just didn't bother playing around with them) with only saving them once, please let me know. (Right now my workaround is a dialog to let the user choose whether to save the sound as a ringtone or a notification.)
Does it help if you try to change 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 reference does not say MediaStore.Audio.Media is deprecated, but I do think AudioColumns is used now. I'm using it in my app and it seems to work.
You have to look into the media player API in Android i.e.android.media.MediaPlayer; android.media.MediaPlayer.OnCompletionListener . Here it is: http://developer.android.com/reference/android/media/package-summary.... And that is how you play a sound on firing a 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, have you tried recording the sound while playing it? Try it and see if it works.
精彩评论