Android: save sound as ringtone and notification

无人久伴 提交于 2019-12-06 03:26:20

问题


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.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 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


回答1:


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.)




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3918325/android-save-sound-as-ringtone-and-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!