22

I'm trying to add just a few simple sounds (beep, boop, click, etc) to an Android app, so I was just wondering if there are any built-in sounds in the Android OS or SDK that could be utilized. If so, how might they be accessed? My only guesses have been somewhere in the mediastore or soundpool classes... I'm pretty new, so any help/tips you could offer would be greatly appreciated. Thank you.

Matt
  • 221
  • 1
  • 2
  • 3

4 Answers4

27

You can play the default ringtone with:

MediaPlayer player = MediaPlayer.create(this,
    Settings.System.DEFAULT_RINGTONE_URI);
player.start();

You can replace DEFAULT_RINGTONE_URI with DEFAULT_NOTIFICATION_URI or DEFAULT_ALARM_ALERT_URI for the various other default sounds.

Will Tate
  • 33,439
  • 9
  • 77
  • 71
11

Here is one way to generate a beep.

Create a raw resouce file with extention .rtttl and put "c5:d=4,o=5,b=250:c5" in it (no quotes)

Then add this code:

protected MediaPlayer _mediaPlayer;

public void playFromResource(int resId)
    {
    if (_mediaPlayer != null)
        {
        // _mediaPlayer.stop();     freeze on some emulator snapshot
        // _mediaPlayer.release();
        _mediaPlayer.reset();     // reset stops and release on any state of the player
        }
    _mediaPlayer = MediaPlayer.create(this, resId);
    _mediaPlayer.start();
    }

Then call playFromResource and pass it the resource id if your raw rtttl resource.

Regis St-Gelais
  • 3,156
  • 5
  • 27
  • 40
1

1- The help on create states that we must not call prepare after a create so I removed the prepare code line.

2- This works fine on the device but it is crashing on the emulator ???

3- Any other sound than alarm, notification and ringtone ??

Regis St-Gelais
  • 3,156
  • 5
  • 27
  • 40
  • I removed the player.prepare() line from my answer as I double checked the documentation and you were correct. I'm not sure why its crashing the emulator, maybe it doesn't have any sound files to play? Can you provide the `logcat` output of the error/crash? I don't believe there are any other default system sounds other than the 3 provided. – Will Tate Feb 02 '11 at 15:42
  • You'r wright. On the emulator, there are no sound available to pick from when we go into settings --> Sound --> Notification Ringtone – Regis St-Gelais Feb 02 '11 at 16:40
1

Here is a one liner:

MediaPlayer.create(MyActivity.this, R.raw.my_sound).start();
Eduard
  • 111
  • 1
  • 10