Hi I need to stop sound from mediaplayer on screen Lock As I am using
if(mp!= null && mp.isPlaying()){
mp.stop();
}
in onPause() . But there is no result. So, How can I stop my media player sound after screen is locked.
To say exactly, I have 16 mp3 files, which will come in random.if a sound is playing and the screen is locked, present sound is Stopped and next one sound is playing.After screen is unlocked, it will works as proper & when the screen is locked , Again same is repeating on my Android 2.3.6 version mobile. How to overcome this.
Thank you.
I have already faced with similar issue, the following code solved my problem:
#Override
public void onPause()
{
super.onPause();
PowerManager mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!mPowerManager.isScreenOn())
if (mp!= null && mp.isPlaying())
mp.stop();
}
You would need to handle it via Screen On & Off intents.
See the link below for details:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
I think You need to set up your code to stop sound in receiver of
Screen off
here is more detail about how to deal with screen lock receiver . http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
Related
when I lock my Android Screen,my app is still playing music(it means the mediaPlayer is working),but after 15 minutes,the music will be stoped,dou you know why?
(I did not use Service or BindService at all,just Use MediaPlayer Class)
Yes it happens, why?
because I think you are using MediaPlayer in Activity, and android release some untouched resources after some time to give space to other processes.
you have to use foreground Service to prevent this problem.
Here is example of MediaPlayer inside Service.
https://stackoverflow.com/a/8209975/6676466
Release the player memory by using onDestroy() method
#Override
public void onDestroy() {
if (mp!= null) mp.release();
}
I'm creating this incredibly simple app, since I'm just starting with Android and I have no experience with Java what so ever. Anyhow, all I have is a timer and a button. Obviously the button starts the timer which counts down from 60 ( 1 minute ) and then vibrates when it's done. All worked fine up until the point I decided to press the lock screen button to put the phone to sleep. I found out that the timer in my app stops going until I unlock the phone. This also happens if I set the auto sleep time to less than 60 seconds and the phone falls asleep on it's own. My question is - how can I have that chronometer running even when the screen is not active?
You need to make this using BroadCastRecievers for system-calls. (Related Helpful question)
You can also play off the life-cyles of the Activity using the PowerManager.
Example using PowerManager:
#Override
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
// The screen has been locked
// Continue your timer
}
}
I'm writing an Android alarm app. I can get the activity to come up all right when the alarm is triggered (it wakes the phone up, turns off the keyguard and shows the alarm view), but I can't for the life of me get the alarm to sound if the device is in sleep mode when the alarm goes off. (It does sound perfectly when the device is awake and the keyguard is on.) I am using a wake lock. I've tried using the MediaPlayer and the SoundPool with no success. Is there some kind of permission that I'm missing? (I already have WAKE_LOCK, DISABLE_KEYGUARD, and RECEIVE_BOOT_COMPLETED permissions.)
When debugging using the SoundPool I perform the load which returns a valid sound ID integer (1), but the onLoadComplete listener (this is where the sound gets played) is never fired. It fires just fine when the device is awake.
Anyone out there have any ideas or have run into the same problem?
Found the solution! I had the call to play the audio in the onCreate() method of the class. I moved it to onResume() as this is when you know the device is fully awake and the activity is visible, on top and in focus.
I recently encountered the same problem. The way I solved this was reading your answer, but also checking the activity flow by logging what was happening. Basically onCreate, onStart, onResume, onPause, onStop, onStart, onResume was being fired in that order. This was an activity that was started as an alarm screen.
I kept the initialization code in the onStart method, making sure to use .prepare() instead of prepareAsync() since I'm using local sounds.
try {
Log.d(LOG_TAG, "Setting media player URI: " + alarmTone.toString());
//mMediaPlayer = MediaPlayer.create(this, );
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getApplicationContext(), alarmTone);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.setVolume(100, 100);
mMediaPlayer.prepare();
} catch (Exception ex) {
Log.d(LOG_TAG, "Exception from media player: " + ex.getMessage());
}
Then inside the onPause and onStop methods, I added:
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
Inside the onResume method I added the media playing code:
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
Log.d(LOG_TAG, "Playing alarm through Media Player");
mMediaPlayer.start();
}
Huzzah, it works!
I want to take pictures from the Android device's camera periodically over a matter of hours, to create a time lapse video effect.
I set an Alarm Manager with an AlarmManager.RTC_WAKEUP flag set to start up a service every few minutes.
The service holds a partial wakelock, does some work, and then calls a Broadcast Receiver through the Alarm Manager which starts up an Activity.
The activity is created (or is resumed), turns on it's own wakelock, and sets up the camera preview surface. Once the surface is setup the SurfaceHolder listener's surfaceChanged() method is called, which finally takes a picture.
If the device is awake, everything works perfectly as expected. But if the device is asleep, once the Activity's onResume() method is finished the Activity is instantly paused. The camera's preview surface never finishes initializing, and no picture will ever be taken.
So the questions I have are:
Is there any way to wake up the phone programmatically? I even try using:
PowerManager powerManager =
(PowerManager)this.getSystemService(Context.POWER_SERVICE);
powerManager.userActivity(SystemClock.currentThreadTimeMillis(),false);
But that doesn't wake up the phone if it is asleep.
Is there any way to take a picture without using a preview surface view?
Is there a way to take a picture that doesn't rely on asynchronous callbacks? Can I put all the code in the Activities onResume() method to take a picture?
Is there any way to keep the Activity's onResume() method running long enough so that the camera's preview has enough time to initialize and call all the listeners?
I am using the wakelocks correctly, and I have all the permission's set properly in the manifest file. My activity isn't kept awake long enough for the asynchronous listeners to properly work.
And to compound the issue, I'm trying to keep everything Android 1.6 compatible, because that is the only test device I have access to.
This is frustrating stuff!
I have finally gotten somewhere now.
I have to create a wakelock using these two flags
PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
Then the device wakes up, and starts at the keyguard screen.
But the only way I can get past the keyguard screen and take a picture is to use these flags on the Activity's window:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
But this is only available with Android 2.0, and doesn't work in 1.6.
You can also disable the Keyguard screen with
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
km.newKeyguardLock(TAG).disableKeyguard();
provided you have the DISABLE_KEYGUARD permission.
That's available since API Level 1.
Are you doing something like this in your onResume method
.... onResume() {
....
WakeLock myWakeLock = .....;
...
}
If so, as soon as the method exits, the WakeLock is released, and the device is free to do whatever it feels like doing ( which is likely to go back to sleep )
and you will need to store the WakeLock in the class somewhere, not as a function local.
I'm developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I'll pause my playback. When the Alarm in snoozed or dismissed, I'll then resume the playback.
I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling the Alarm notification part. I don't need to set the Alarm on, it could've been set by the user, and I don't need to programmatically. All I need is just handle that notification.
Any ideas on this would be extremely useful?
Thanks,
Asheesh
HI Asheesh Vashishtha,
Correct me on this, but AFAIK whenever any other application even if it is the alarm clock, is activated, your activity will surely go in background. So i guess u can override the OnPause and OnResume functions to put your bit of code. As far as snooze or other things are concerned, they all will result in the Alarm Activity getting destroyed(or paused, don know much about it) and your activity will get resumed. So that wont be a matter of concern for u!
Hope this helps...
AFAIK, there is no way for you to be notified of what the Alarm Clock application does, any more than you get notified about any other third-party alarm clock.
Note that AlarmManager -- what you were probably reading about -- is not the same as the Alarm Clock application.
Sorry!
I ran into a similar situation while developing a media player. My solution was to use the AudioManager's OnAudioFocusChangeListener.
You implement the listener in the class like so
public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {
Then you override onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
//Just fall through by omitting break
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
if (isPlaying()) {
pause();
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
break;
default:
break;
}
}
The key here is AudioManager.AUDIOFOCUS_LOSS_TRANSIENT. This was the code the listener kept receiving when the alarm clock would go off (on The Note 5). So I simply handled AudioManager.AUDIOFOCUS_LOSS_TRANSIENT the same as AudioManager.AUDIOFOCUS_LOSS by pausing the media player and letting go of the audio focus.
When we setup the media player, I added this line before adding the data source
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure your code for starting the media player also has this line in it (I have it in the start code and onResume code in case the alarm went off while the app was in the background).
mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
That line helps you get back the audio focus when you hit the play button after dismissing the alarm clock.
You should also let go off audio focus when you're finished with the media player. I put this line of code in the onStop and onDetach methods.
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
It's not as much setup as you may think and it allows you to adjust your media player whenever unexpected audio is introduced (such as an alarm clock or timer goes off).