2010-03-25

Ringtones confusion and volume settings of audio streams

People asked me about how to add their favorite music as alarm ringtone. In Android, there are four categories of ringtones,
  1. Phone ringtones.
  2. Alarm ringtones.
  3. Notification ringtones.
  4. UI ringtones.
In built-in ringtone picker, it filters out ringtones that don't belong to the category you queried. From a user's perspective, if you want to have your favorite music as your alarm ringtones, you need to either place it in the right directory or install RingsExtended to do it.

If you don't want to install RingsExtended, put your favorite songs into /sdcard/media/audio/alarms in your SD card. You will see them showing up on the built-in ringtone picker for you to select.

From a developer's perspective, another thing you need to take into account is the volume up/down buttons of your phone. You need to decide which audio stream is affected by volume up/down buttons in your application. In my case, OpenAlarm is an alarm application and whenever you click volume up/down buttons, only alarm volume should be affected.

The mistake I made was that I only change the volume of an internal MediaPlayer charged of playing ringtone. It's not enough. In Android, audio settings are controlled by AudioManager and concepts of audio streams are used to control volumes of sounds in different situations. In documentation, they are
  • STREAM_ALARM
  • STREAM_DTMF
  • STREAM_MUSIC
  • STREAM_NOTIFICATION
  • STREAM_SYSTEM
So, all you have to do is to implement things mentioned above
  • Have your activities aware of volume change by

    setVolumeControlStream(AudioManager.STREAM_ALARM);
     
     
  • Apply proper audio stream settings to your MediaPlayer.

    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);