Playing an android notification sound

Welcome to ScriptMode
Rami
Posts: 36
Joined: Tue Aug 25, 2015 9:39 am

Playing an android notification sound

Postby Rami » Wed Mar 02, 2016 7:38 am

I was wondering how I could play a notification sound without playing it over the media stream. Right now I can do this via the media player, however I don't want it to play as a media file, I want it to play as a notification or alert or ringtone. heres an example of what my code looks like right now:

MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();

Paul
Posts: 137
Joined: Mon Aug 24, 2015 4:53 pm

Re: Playing an android notification sound

Postby Paul » Fri Mar 04, 2016 1:28 pm

If anyone's still looking for a solution to this, I found an answer at How to play ringtone/alarm sound in Android

try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
You can change TYPE_NOTIFICATION to TYPE_ALARM, but you'll want to keep track of your Ringtone r in order to stop playing it... say, when the user clicks a button or something


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest