android – Are there any built-in/default sounds that can be utilized in an App?-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

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.

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *