Detecting tab change when using Android Navigation Component with Bottom Navigation - android

I currently have an app (Java) that uses the Navigation Component with Bottom Navigation. I have a fragment for each tab.
One of the fragments navigates to a secondary fragment that features an audio player (using Media Player). This audi fragment has the following code that stops the audio when I go back to the parent fragment:
#Override
public void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.pause();
mp = null;
}
}
The problem is that when the audio is playing and I tap on a different tab from the bottom navigation, the audio continues to play. I thought of using the following:
#Override
public void onStop() {
super.onStop();
if (mp != null) {
mp.pause();
mp = null;
}
}
And this solves the problem but it also prevents the audio from playing when the screen is turned off. (This doesn't happen when the onStop is not there).
The idea is to have the pause() function execute when the user taps on any of the bottom navigation tabs.
With my current structure, how can I get the sound to stop when the user taps on a bottom tab?

You can use "setOnItemSelectedListener()" to listen for clicks on Bottom Navigation.
If the user selects an item other than the one inside the audio player, stopAudioPlayer function is called
Example :-
navBottom.setOnNavigationItemSelectedListener(item -> {
if (item.getItemId() != R.id.nav_bottom_audio_player)
stopAudioPlayer();
return true;
});

Related

how can stop background runnig process of media player stop when pressed back button of mobile

i am searched many solution but could not find proper result when i press the back button of mobile i am using one counter variable and put the condition like when counter is 0 it called the music file.
but when i am comeout from that activity means press back button of mobile still that activity is runnig and called the music file .i am using this below code for music play.please anyone have solution then help me.i am using one timer function.like this when i==0 that time call one music file but when i press back button. this timer i wants to stop.
public void btntimer(){
new Handler().postDelayed((new Runnable() {
#Override
public void run() {
if(i!=32) {
matchNo();
}
else {
try{
showdialog();
media=1;
}catch (Exception e){
media=0;
}
updatepoints();
}
}
}), 4000);
}
song=MediaPlayer.create(this,R.raw.cartoon1);
song.start();
song.setLooping(false);
If I understand you correct, you want to stop playing music on back button pressed.
Call stop() in onBackPressed method.
#Override
public void onBackPressed() {
song.stop();
song.release();
song = null;
}

Ionic nav-view and external resource

I'm trying to display a video within my application. I've created a link to it such:
Start movie
Everything is fine and the video spins up, but when I push the back button the nav state is resetted and I have to restart my app in order to get back to the "main" page of my app. Is there any trick to maintain the nav state when starting my video?
Yes There is trick you can use the below code in your back button listener
below is the example for your reference you can adjust it by getting the status of your media player like mediaplayer.isPlaying()
final MediaPlayer mp1 = MediaPlayer.create(PlayaudioActivity.this, R.raw.beet);
mBackBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
}else{
finish();
}
}
});

retain videoview in fragment when rotating

I have a activity who hold 2 fragments (one SlidingMenu and other a VideoPlayer with control and other views).
How Can I retain the video playing status when I'm rotating the device? the video is a HLS Stream, so, I don't need to start again the buffering when rotate.
I start playing with the savedInstanceState, but i can't get it work
If you are using the same resources on on different screen orientations, you can prevent fragment from recreating.
If you need to recreate fragment, you can store playback progress, and after recreation scroll video to stored position
#Override
protected void onPause() {
...
if(playbackWasStarted) {
video.pause();
videoProgress = video.getCurrentPosition();
}
...
}
#Override
protected void onResume() {
...
if(playbackWasStarted && videoProgress!=0) {
video.seekTo(videoProgress);
video.start();
}
...
}
EDIT Oh, I didn't noticed that this question is veeeery old...

Andengine game music resumes on the lockscreen

I have developed a game using andengine
I am facing a problem when using the power Button. If I press the POWER Button while playing the game the screen turns off and the onPause() is called as expected. But when I press the HOME button or POWER button again to turn on the screen, onResume() method is called and but the lockscreen shows up.
In the onResume() method, I resume the music of the game.
So as a result lockscreen shows up , but the game music plays in the background. I do not want to play the music in the lockscreen. Please help me solving this.Thank you
The solution is here: Activity handle when screen unlocked
By registering a BroadcastReceiver filtering the Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_PRESENT actions, you will be able to handle these 3 cases:
Intent.ACTION_SCREEN_OFF: When the POWER button is pressed and the screen goes black.
Intent.ACTION_SCREEN_ON: When the POWER button is pressed again and lockscreen is showed up.
Intent.ACTION_SCREEN_PRESENT: When you pass the locksreen and are back in your game.
However, in my case (using a Galaxy S GT I9000 with Froyo 2.2) these actions are not called when dealing with the HOME button (and I think it's a general behavior).
One simple and quick (but maybe not the best) way to handle both HOME and POWER buttons to pause and resume your music could be to keep the onPause and onResume methods and use a simple boolean flag like this:
private boolean mPowerButton = false;
#Override
public void onPause() {
super.onPause();
// Pause your music
Log.d("Game activity", "Music paused");
}
#Override
public void onResume() {
super.onResume();
if (!this.mPowerButton) {
// Resume your music
Log.d("Game activity", "[HOME button] Music resumed inside onResume");
}
}
public class receiverScreen extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// This is the lockscreen, onResume has been already called at this
// step but the mPowerButton boolean prevented the resumption of music
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
LevelActivity.this.mPowerButton = true;
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
// Resume your music
Log.d("Game activity", "[POWER button] Music resumed inside ACTION_USER_PRESENT action");
LevelActivity.this.mPowerButton = false;
}
}
}
Hope it helps!

android mediaplayer is playing multiple instances from one player

so im trying to make a sound play through android. ive created a mediaplayer like this
MediaPlayer player; (outside onCreate())
player = MediaPlayer.create(this, R.raw.theninth); (inside onCreate())
and then i reference this player throughout the Activity. .start() on shake. .pause onclick etc.. it looks like this
if(mag >= 500) {
if(!started) {
mainView.setText(R.string.waitforit);
player.start();
started = true;
if(doThisOnce) {
timer.schedule(changeText, 26000);
doThisOnce = false;
}
}
player.setVolume((float)(2*mag/3.0),(float)(2*mag/3.0));
}
and
public void stopAudio(View v) {
mainView.setText(R.string.shakeme);
player.pause();
started = false;
}
and so the problem is the player doesnt pause when i tap the screen, instead it goes to the starting text of hte app, and and its like i reloaded the entire app, and i can shake it and then another intance of the same file from the same player will start playing.. what do?

Categories

Resources