I made a video chat app on android. When I'm in the middle of a video chat, and I move the app to background mode, the video chat pauses. When I move it back to foreground mode, the video chat resumes. This is the desired behavior. However, when I'm in the middle of a video chat, and I press the power button to turn off the screen, the video chat continues. I want turning off the screen to behave just like background mode. Any ideas?
You can pause the video chat when the app goes in onPause().
i.e add your video chat pause logic in the overridden onPause().
Try following code with your default videoview
#Override
public void onPause() {
Log.d(TAG, "onPause called");
super.onPause();
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
videoView.pause();
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.seekTo(stopPosition);
videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}
Related
I want to pause media player in case the user use another program to watch video
or the user receive a phone call like any multimedia app (YouTube ,...)
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
thanks
You need to override android methods which relate to the lifecycle of an android application
#Override
protected void onStop() {
super.onStop();
mediaPlayer.stop(); //put your stop criteria here
}
#Override
public void onPause() {
super.onPause();
mediaPlayer.pause(); //put your pause criteria here
}
#Override
public void onResume() {
super.onResume();
mediaPlayer.play(); //put your resume criteria here
}
you can simply override these methods to do your job.
MediaPlayer can be implemented as a service. Pause the service when the event happens using the BroadcastReceiver and you can again resume it using that. It would work in this manner.
You have to use AudioFocus (https://developer.android.com/guide/topics/media-apps/audio-focus). In this way you will receive an Event when another App is taking "the ownership" of Audio Device to play its content/audio and then another Event will be raised when that App stops to play it and the "ownership" return to your App.
I have kind of a game with 2 activities: Start Activity (with high score, start button and tutorial button) and main game activity which is is based on countdown timer, when time is up, game returns to start activity.
Problem is when user starts game and then hits home button and goes back to home screen (just leaving game by home button) Everything is ok until time is up, then Start activity appears on screen with lost message.
I've tried various combined methods like onPause witch finish(); inside and so on but it doesn't work or causes app force close.
I can't handle home button click like in onBackPressed() which I did in that case.
Is there any way to suspend app and pause all threads while it isn't in foreground?
I suggest two way
1
Create global variable like
private isInForgrand = false;
And in onStop() or onPause() and onResume() change it
#Override
public void onStop() {
isInForgrand = false;
super.onStop();
}
#Override
public void onResume() {
super.onResume();
isInForgrand = true;
}
And in onFinish() check it
#Override
public void onFinish() {
if(isInForgrand){
//do what you want
}else{
//your app NOT in Forgrannd
}
2
You can cancel CountDownTimer in onStop()
#Override
public void onStop() {
super.onStop();
mCountDownTimer.cancel();
}
I am using Java and xml to create an application for the summer. This app has music in the background, that I would like to pause and then play depending on the state it is in. When i lock my screen the music does not pause it keeps playing. How would i fix this, I have tried to use the method on this site:
https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents
I have not been able to successfully get a working prototype, and I am working in android studio.
myPlayer = MediaPlayer.create(c(this is the context), myField(This is the raw file));
myPlayer.start();
myPlayer.setLooping(true);
myPlayer.setVolume(0.7f,0.7f);
What could I add to pause the mediaplayer when the lock button is pressed or the phone goes to sleep and then play it when the phone is unlocked?
#Override
protected void onPause() {
super.onPause();
if (myPlayer != null) {
myPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (myPlayer != null) {
myPlayer.start();
}
}
I want to play an audio file in the background of my app. Easy enough. I want the music to persist and NOT stop or pause while switching between activities in my app. Also fairly easy and accomplished simply by doing this in the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mp = MediaPlayer.create(MainActivity.this, R.raw.lostmexicancity);
mp.setLooping(true);
mp.start();
}
The problem? Getting the music to stop when I press the HOME button.
Killing the sound when the user presses the back button seems easy. Here's what I have for that and works great:
public void onPause() {
if(this.isFinishing()){ //BACK was pressed from this activity
mp.stop();
}
super.onPause(); }
Not complicated, but this does not catch presses of the HOME button. If the Home button is pressed, the music keeps playing even while the user no longer sees my app.
I have seen answers that involve setting permission in the manifest to Get Tasks which I shouldn't have to do and appears dangerous to users. Besides that, the solution didn't even work. I've seen solutions that involve using a service, but none of those work either because the home button STILL plays the music just like before because there doesn't seem to be a way to catch it and it doesn't 'finish' the app (not to mention that every time someone suggest using a service for this task multiple people come in and state that this is not a proper use for services)
It seems the only way to kill the music when the Home button is pressed is to use a non-conditional stop() within onPause, but that's no good because that's called when I swap activities with intents, causing the music to end between activities which is no good.
I have trouble imagining that such a common function like background music is this hard, but I've seen post after post with the same issue as me and no proper answers other than ones that would kill the music between activities within the app.
How do all the other apps on the Google play store accomplish this and yet there appears to be no clear answer online? I could just stop and start the music with each onPause(), but that would cause unprofessional gaps in audio not to mention it would start the background audio from the beginning over and over again which is unacceptable.
I'm a bit new to Android Programming (few months) and today, I faced the same problem you did (maybe you still do?)
I made it work as the following :
Lets say I have MainActivity, and in MainActivity I have Btn2 which leads to SecondActivity, and Btn3 which leads to ThirdActivity.
I declared at the beginning of MainActivity :
public static boolean shouldPlay = false;
I then implemented my onStop() method :
public void onStop() {
super.onStop();
if (!shouldPlay) { // it won't pause music if shouldPlay is true
player.pause();
player = null;
}
}
If the boolean shouldPlay is set to true, then my onStop() won't be called entirely and my music won't turn off. I then have to decide when I set it to true. When I switch from MainActivity to SecondActivity, I do it through an Intent and that's when I'll set shouldPlay to true :
Button Btn2 = (Button) findViewById(R.id.Btn2);
Btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
shouldPlay = true;
startActivity(intent);
}
});
And the same is done for Btn3.
Now, the last thing we want to be looking for is that if I was to go back to MainActivity after visiting SecondActivity or ThirdActivity, shouldPlay would then have been set to true. The first thing I tried was to set it to false as soon as Second and ThirdActivity are called (in their onCreate()) but it want to work, maybe because the onStop() from Main and onCreate() from others are called simultaneously (frankly I don't really get life cycle for now).
What worked is simply to set shouldPlay to false every time we launch onCreate() of Main :
shouldPlay = false;
This works properly for me.
Let me know if it does for you,
Cheers,
bRo.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_HOME){
Log.d("Jorgesys", "Home button pressed!!!");
}
return super.onKeyDown(keyCode, event);
}
but hey! This no longer works as of 4.0 + , Read this: Capture Home Key Event
Try with Back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Log.d("Jorgesys", "Back button pressed!!!");
}
return super.onKeyDown(keyCode, event);
}
To stop the media player, you are using the method:
isFinishing(): if the activity is finishing, returns true; else
returns false, but you are only pausing the activity not finishing the
activity.
public void onPause() {
if(this.isFinishing()){ //INCORRECT, Here you are pausing you activity not finishing.
mp.stop();
}
super.onPause();
}
so change to:
public void onPause() {
if(mp.isPlaying())
{
mp.stop();
}
super.onPause();
}
When your activity is on pause, evaluates if your MediaPlayer is playing, if this is true then stops the audio.
I have an app that, for now, runs a loop where it updates the game state ~25 times/s and tries so draw it 40 times/s. the update works fine, it shows just a bit more than 25. but on the emulator, the game draws under 20 fps, no matter how much there is to draw. even if I only paint the background green it has low fps.
I sent the app to someone who tested it on his phone, there it runs correctly with 40fps drawing speed and 25fps updating speed.
I tested it on the emulator with different API levels, the drawing fps is always way too slow. I use eclipse. do I have to make any special setting for the emulator or so?
There was another problem too, the guy who tested it on his phone got an error message when starting the app, it said the app wouldn't react and the option for wait and shutdown. after waiting 5-10 seconds the content view would be displayed. I create the content view and assign the variable in the onCreate method, without starting the loop. In the onStart method I set the contentView and start the loop, but it runs an empty while loop until I set a boolean in the onResume method. any ideas?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view= new MainMenu(this); }
protected void onStart() {
super.onStart();
this.setContentView(view);
view.startGameView();
}
public void startGameView() {
loop=new GameLoop(this);
loop.start();
}
protected void onResume() {
super.onResume();
view.resumeGameView();
}
public void resumeGameView() {
loop.b_pause=false;//loop starts to execute code for drawing/updating
}
the code for the activity change:
public void changeActivity(Class c, String str)
{
Intent i=new Intent(this, c);
startActivity(i);
}