Not switching back to the mode what user have used.
I mean when phone is in silent and i used to ring for some selected numbers but it does not
switch back to normal mode when phone state is idle.
here's my piece of code.........
if(IncomingNumber.equals(WhitelistedNumbers)){
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
System.out.println("Number Matches......!");
System.out.println("WhitelistedNumber(s): "+WhitelistedNumbers);
}else{
System.out.println("No Match(s) found.....!");
}
and to switch back to normal mode i m using........
case TelephonyManager.CALL_STATE_IDLE:
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
System.out.println("Set to Default Ringer Mode....!");
break;
Thanks in advance if any suggestion to sort the problem........
You need to save the previous ringer mode in a variable, and then restore it from that variable.
int previousMode;
if(IncomingNumber.equals(WhitelistedNumbers))
{
previousMode = am.getRingerMode();
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
....
}
and to restore:
am.setRingerMode(previousMode);
Related
I made Media Android TV application by using SimpleExoPlayer,
It can play some musics, some videos,
I want to track user when Mute|UnNute by using remote to trigger.
Therefore, I want to detect Mute|UnMute mode on current TV devices.
I research many pages, still not find out that,
Hope someone can help me the way,
Thank you,
p/s : These codes did not detect for Android TV
You can use AudioManager to check volume is mute or not mute.
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
break;
}
Currently I only get only this way to detect Mute|UnMute of Android system.
public static boolean isSoundOn(Context context) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
/*
* It can detect
* - from Android 6.0 : Check Stream Music
* */
boolean isSoundOn = false;
if (NlbUtil.isAndroidMarshmallow()) {
// from Android 6.0 : Stream Music
isSoundOn = am.getStreamVolume(AudioManager.STREAM_MUSIC) != 0; // If no sound system, return true
// isSoundOn = !am.isStreamMute(AudioManager.STREAM_MUSIC);
}
return isSoundOn;
}
I am recording a video using camera2api with a button to start the recording.Now when the recording starts, i need to get system time in milliseconds only if i press a specific button(other than the start rec button
) .
If not the system time, then at least the record time of the video being recorded.
The time should be stored only if i press the specific button. I have tried using system.currentTimeMillis() and systemclock.elapsedtime(), but once the recording starts and then i press the specific button to record time without stopping recorded ,it doesn't store any time details and shows 0.
Below is the code which i am using to store time:
case R.id.video: {
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startTSRecordingVideo();
starttime = System.currentTimeMillis();
Log.d(TAG, "onClick:time "+starttime);
switch (view.getId()){
case R.id.stop:{
stoptime= SystemClock.elapsedRealtime()-starttime;
Log.d(TAG, "onClick:timefor slowstop "+stoptime);
}
break;
case R.id.timeshift:{
slstarttime=SystemClock.uptimeMillis()-starttime;
Log.d(TAG, "onClick:timefor slowstart"+slstarttime);
}
break;
}
}
Please tell me, how to get the time without affect the recording.
You have misplaced the stop and timeshift button event and it will not get called. It should be directly on the switch case.
Try this.
case R.id.video:
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startTSRecordingVideo();
starttime = System.currentTimeMillis();
Log.d(TAG, "onClick:time "+starttime);
}
break;
case R.id.stop:
stoptime= SystemClock.elapsedRealtime()-starttime;
Log.d(TAG, "onClick:timefor slowstop "+stoptime);
break;
case R.id.timeshift:
slstarttime=SystemClock.uptimeMillis()-starttime;
Log.d(TAG, "onClick:timefor slowstart"+slstarttime);
break;
When we loose audio focus due to AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK we should reduce sound volume, untile audiofocus will be restored to AudioManager.AUDIOFOCUS_GAIN. However, it is fired if we registered listener before event.
How to check if audio focus is in state AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK if we registered after AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK event?
AudioManager.OnAudioFocusChangeListener audiofocusListener =
new AudioManager.OnAudioFocusChangeListener() {
#Override public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
}
break;
}
}
};
If I understand your question correctly, you want to know whether some other app is currently holding audio focus (and thus the audio output of your app should be ducked) before you actually have registered your audio focus listener.
If that's the case (some other app currently owns the audio focus), then your request for acquiring the audio focus will be denied, that is, your call to AudioManager.requestAudioFocus with audiofocusListener will return with AUDIOFOCUS_REQUEST_FAILED.
How to play an Audio clip that is stored in Phone Memory during the Phone call, and the same Audio clip should hear to Call receiver...
I am using simple code:
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
Really I don't know how to write code to play audio file automatically while receiver will pick my call only for a particular number.....
There's no API in place in Android for playing audio over the voice call uplink. There may be phones out there that support it (I have not tested every single Android phone) but in general it's not going to work.
The best you could to is route the music to the loudspeaker, set the media volume to max and hope that it gets picked up by the microphone. That would sound awful though, so it's not something I'd recommend.
I'm developing an application that can turn off the sound of Android Phone automatically. How can I detect the volume of the sound and turn it off programmatically?
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,
"It's time to work");
///so, i want to add the silet function here..help me, please?
}
Thanks in advance.
Have a look at the AudioManager, especially the getStreamVolume and setStreamVolume methods
EDIT
You can also use the method Nikola Despotoski provided with setRingerMode
A Service is a child of a Context so you can call directly getSystemService
See the updated code below (untested):
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,"It's time to work");
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
Register for AUDIO_SERVICE and then use the AudioManager to control the volume up/down or set profiles.
Or if you want to listen for changes in the Audio focus then make your Activity implements AudioManager.OnAudioFocusChangeListener. Override unimplemented method. Create switch that will take care of types of changes.
Like:
#Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_GAIN:
//do something
break;
case AudioManager.AUDIOFOCUS_LOSS:
break;
}
Or if you want to listen for changes on the audio output, like unplugging the headphones (switching to phone speaker) use ACTION_AUDIO_BECOMING_NOISY sticky broadcast
http://developer.android.com/reference/android/media/AudioManager.html#ACTION_AUDIO_BECOMING_NOISY
http://developer.android.com/reference/android/media/AudioManager.html#RINGER_MODE_SILENT
See here
Edit: This is the solution. There was no need to handle AudioFocus but just set different ringer profile or adjusting volume
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,
"It's time to work");
AudioManager audiomanager =(AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //or adjust volume here instead setting silent profile for the ringer
}