My alarm works well without silent and vibrate mode. When my phone is in silent and vibrate mode, How can I run the alarm. Also, how can I know alarm or music is finished.
private void mediaPlayerStart()
{
try
{
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alert == null)
{
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
ringtone = RingtoneManager.getRingtone(getApplicationContext(), alert);
ringtone.play();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Generally, if the user decided to mute his phone, it might be a good idea to keep it silent. However, if you're making an app for your own use, you can simply turn the volume up like so:
AudioManager mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, mgr.getStreamMaxVolume( AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
Related
I am trying to stream voice/audio (two way) between two Android devices Tablet and Mobile (over java sockets).
The Tablet can play received audio(voice) clearly, but the Mobile plays received audio as noise.
Then i set this audio mode in the code on tablet:
audioManager.setMode(AudioManager.MODE_IN_CALL);
This now results in Mobile receiving clear voice.
But the tablet goes silent, it does not play the received audio (or rather its not audible).
I am not sure what combination (if any) of AudioManager mode i should use here?
It's possible to handle the sound you want to play as Alarm.
Create a new class named AlarmController and try this code.
This worked for me on Android 4.4.2 (Huawei ascend P7) with each system volume (Media, Ringtone, Alarm) set to 0.
Context context;
MediaPlayer mp;
AudioManager mAudioManager;
int userVolume;
public AlarmController(Context c) { // constructor for my alarm controller class
this.context = c;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//remeber what the user's volume was set to before we change it.
userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
mp = new MediaPlayer();
}
public void playSound(String soundURI){
Uri alarmSound = null;
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
try{
alarmSound = Uri.parse(soundURI);
}catch(Exception e){
alarmSound = ringtoneUri;
}
finally{
if(alarmSound == null){
alarmSound = ringtoneUri;
}
}
try {
if(!mp.isPlaying()){
mp.setDataSource(context, alarmSound);
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
mp.prepare();
mp.start();
}
} catch (IOException e) {
Toast.makeText(context, "Your alarm sound was unavailable.", Toast.LENGTH_LONG).show();
}
// set the volume to what we want it to be. In this case it's max volume for the alarm stream.
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
}
public void stopSound(){
// reset the volume to what it was before we changed it.
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, userVolume, AudioManager.FLAG_PLAY_SOUND);
mp.stop();
mp.reset();
}
public void releasePlayer(){
mp.release();
}
I hope this works for you. :)
sir, is it possible to ring an alarm even when ringer is muted? if yes, how. and also, can i set its volume to something in between and not just maxvolume? thanks in advance
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("firealert")) {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
}//end if
}//end for
Things to look at:
AudioManager.getRingerMode / setRingerMode. Set it to RINGER_MODE_NORMAL to ensure the phone isn't muted or set to vibrate. (Although you might want to check the state first, so you can store it and reset the value after your alarm is cleared.)
if ( aManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL )
aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
If you want to set volume to 50% then check AudioManager.getStreamMaxVolume and then divide that by 2, for example. It would probably be better to let the user configure it, though. Use a SeekBar to have him set a preferred volume (which, again, you could reset back to normal after your alarm plays).
RingerVolumeBar = (SeekBar) findViewById(R.id.seekBar1);
RingerVolumeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser)
{
aManager.setStreamVolume(AudioManager.STREAM_RING,
progress, AudioManager.FLAG_PLAY_SOUND);
// record new setting so you can load it up or reset it back later
...
}
});
I have a working alarm app, but wanted to add a feature where the user gets the choice between "Play alarm continuously till acknowledged" and "play alarm sound once".
I then looked at my alrm ringing code expecting to see some kind of "repeat" flag which I could optionally remove - but there was none. So how do I play the alarm sound just once?
My existing code looks like this:
private void playSound(Context context, Uri alert)
{
mMediaPlayer = new MediaPlayer();
try
{
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)
{
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
catch (IOException e)
{
// oops!
}
}
Actually in each alarm sound there is a FLAG named ANDROID_LOOP which force your sound to loop. Unfortunatly you can't change that flag even using MediaPlayer.setLooping(false).
But you still can manually stop your player after a certain time. For example getDuration will give you the length of your sound.
int duration = mMediaPlayer.getDuration();
Runnable stopSoundRunnable = new Runnable() {
#Override
public void run() {
mMediaPlayer.stop();
}
};
mSoundHanlder.postDelayed(stopSoundRunnable, duration);
I am writing an application on android 4.0 which will play the current ringtone when I press a button.
But in the ringtone is played only one time. I need it to repeat for a few times.
My current code:
Uri notifi = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
final Ringtone r = RingtoneManager.getRingtone(c, notifi);
r.play();
Try this code I have used this before and able to play Ringtone continuously until you stop
try {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch(Exception e) {
}
Tried the above codes on lollipop and only this worked for me
//activating looping ringtone sound
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
player = MediaPlayer.create(this, notification);
player.setLooping(true);
player.start();
You can have a timer regularly check if the ringtone is still playing. For example, every second:
mRingtone.play();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (!mRingtone.isPlaying()) {
mRingtone.play();
}
}
}, 1000*1, 1000*1);
I have read that ringtone has to have ANDROID_LOOP tag. Ref: http://xanderx.com/2010/08/25/making-ringtones-loop-on-android/
You can also try to play this file using AudioManager and set it looping. Ref: http://developer.android.com/reference/android/media/MediaPlayer.html#setLooping(boolean)
I achieved this issues by set looping true with Ringtone and ringtone manager.
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.setLooping(true);
r.play();
Here I am starting ringtone and when we want to stop ringtone we can call
r.stop();
method to stop ringtone
In an Android I am attempting to create an instance of MediaPlayer to play an alarm tone. I realize that there is an alarmmanager, but at this point I'm not interested in going to the trouble of creating a service. For now I'd like to grab the users default alarm tone load, loop and play it. Unfortunately my below code is causing the error (to to is not a typo):
Unable to to create media player
Immediately before this error the following debug statement is logged:
Couldn't open file on client side, trying server side
I've logged the path to the alert and it is listed as: /system/alarm_alert. As of now this code has only been run in an emulator targeted at Android 2.2 API Level 8. What is wrong with the below code that would cause this error?
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
// I can't see this ever being null (as always have a default
// notification) but just incase alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Log.d("alertPath", alert.getPath());
MediaPlayer mediaPlayer;
setVolumeControlStream(AudioManager.STREAM_ALARM);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, alert);
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
mediaPlayer.start();
} catch (IOException e) {
} catch (Exception e) {
Log.d("mp", e.getMessage());
}