android stop audio when phone lock after play about 10-20 min - android

I have a app radio, which use exoplayer
Almost good work, but when phone in background radio play about 10-20 min and stop and I turn phone radio continue play. How can I fix it programatically?
Mediaplayer have a method like setWakeMode(), but I do not find same to Exoplayer. Sorry for my English

I faced the same problem with exoPlayer, when phone screen locked AudioTrack shutdown after few minute, So you can add WakeLock or WifiLock in your code to keep exoplayer running in background,Even if the phone’s screen is locked.
if (intent.getAction().equals(ACTION_PLAY)) {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.requestAudioFocus(this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
wifiLock = ((WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
wifiLock.acquire();
if(exoPlayer == null){
//Your code Exoplayer her
}
} else if (action.equalsIgnoreCase(ACTION_STOP)) {
if (exoPlayer != null) {
exoPlayer.stop();
}
if (wifiLock != null && wifiLock.isHeld()) {
wifiLock.release();
}
}
}

Related

Suppressing system audio volume to play notification audio from inside app

My app runs as a service in background and can give notifications based on some events. Currently, to play notification files, I use MediaPlayer in my app :
MediaPlayer mPlay = MediaPlayer.create(this, R.raw.sound);
mPlay.start();
But, if a song is already playing on the device (Music player, radio etc), and my notification plays when receiving the event, both the sounds (Music audio & notification audio) is heard in parallel.
So, is there a way that I can suppress the system audio volume so that my notification is heard clearly and once notification audio is over, the system audio can playback again?
Thanks for any help
I got the answer to this question in the following link:
http://developer.android.com/training/managing-audio/audio-focus.html
We need to request for audio focus (transient/permanent) and then can play the app audio:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
Log.i(TAG, "...audiofocus granted....");
MediaPlayer mPlay = MediaPlayer.create(this, R.raw.sound);
mPlay.start();
}
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
Log.i(TAG, "....audiofocus loss transient in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
Log.i(TAG, "....audiofocus gain in listener......");
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.i(TAG, "....audiofocus loss in listener......");
//am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
Hope it helps someone.

Check if the camera shutter sound is on or off in the settings Android

I'm using a custom camera application , i want the camera to enable the shutter sound if it was on on the device and turn it off otherwise , so is there a default method which will check that , or can i get the settings information of the camera to find that .
I tried this code , but it sounds that is check on the device sound.
AudioManager audioService = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
Toast.makeText(MainActivity.this, "Normal", Toast.LENGTH_SHORT)
.show();
}
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(id, info);
if (info.canDisableShutterSound) {
mCamera.enableShutterSound(false)
}
How to mute camera shutter sound on android phone

Play alarm sound just once

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);

How to pause the background music while recording audio in android

I am developing an audio record application in android. So if any background music is already playing in the device music player, that should be paused before it starts recording and the background music should resume whenever the recording is either stopped or paused. And the same should work while playing the recorded audio. Can anybody help me out to get through this scenario? Thanks in advance..:)
get audioManager and check
audioManager.isMusicActive()
if true
private void toggleNativePlayer(Context context) {
Intent intent = new Intent("com.android.music.musicservicecommand");
intent.putExtra("command", "togglepause");
context.sendBroadcast(intent);
}
and after you finish recording run this code again to start play musig again
You can stop other applications from playing music, by requesting AudioFocus. When you are granted AudioFocus, other apps stop playing music and then you can play/record as per your needs.
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(focusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so you can start recording.
}
But, there is only one AudioManager. So, Audio focus is assigned to each application that requests it one by one. This means that if another application requests audio focus, your application will lose it. You will be notified of the loss of audio focus through the onAudioFocusChange handler of the Audio Focus Change Listener (afChangeListener) you registered when requesting the audio focus.
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS) :
//Lost focus. Stop recording
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
//Gained AudioFocus. Start recording tasks
break;
default: break;
}
}
};

Play an alarm during a call

I am creating an application that i have to play a sound. If the phone is ringing or in a call, i want to play the sound in the earphone. The code bellow works perfectly only if the screen is on, but in a call, when i put the phone in my ear, the screen gets off and my "alarm" doesn't play. any hint?
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int state = tm.getCallState();
mMediaPlayer.setLooping(true);
if(state == TelephonyManager.CALL_STATE_OFFHOOK || state == TelephonyManager.CALL_STATE_RINGING){
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setScreenOnWhilePlaying(true);
audioManager.setSpeakerphoneOn(false);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
audioManager.setMode(AudioManager.MODE_IN_CALL);
Log.d("GOFC","Tocando ou fora do gancho");
} else {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
vibrator.vibrate(pattern, 1);
Log.d("GOFC","Normal");
}
mMediaPlayer.prepare();
mMediaPlayer.start();
You might need to use WakeLock. You should listen for events when phone is picked up to acquire WakeLock and when phone is hanged to release WakeLock.
This is just a wild guess though.

Categories

Resources