How do I prevent the end of Android Studio activities when I go back?

Asked 2 years ago, Updated 2 years ago, 96 views

If I go back during the activity, the music turns off How can I keep listening to it without turning it off?

Code

-=================


package com.example.android.wearable.recipeassistant;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class MusicSetting extends Activity {
    MediaPlayermp; // Object for Music Playback
    int pos; // when the playback stopped
    private Button bStart;
    private Button bPause;
    private Button bRestart;
    private Button bStop;
    Private SeekBar; // Sikbar indicating music playback location
    Boolean isPlaying = false; // variable to determine if it is playing
    class MyThread extends Thread {
        @Override
        Public void run() { // method called back when thread starts
            // Move the tickbar stick little by little (Repeat until the end of the song)
            while(isPlaying) {
                sb.setProgress(mp.getCurrentPosition());
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_setting);
        sb = (SeekBar)findViewById(R.id.seekBar1);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onStopTrackingTouch(SeekBar seekBar) {
                isPlaying = true;
                int ttt = seekBar.getProgress(); // where the user has moved
                mp.seekTo(ttt);
                mp.start();
                new MyThread().start();
            }
            public void onStartTrackingTouch(SeekBar seekBar) {
                isPlaying = false;
                mp.pause();
            }
            public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
                if (seekBar.getMax()==progress) {
                    bStart.setVisibility(View.VISIBLE);
                    bStop.setVisibility(View.INVISIBLE);
                    bPause.setVisibility(View.INVISIBLE);
                    bRestart.setVisibility(View.INVISIBLE);
                    isPlaying = false;
                    mp.stop();
                }
            }
        });

        bStart = (Button)findViewById(R.id.button1);
        bPause = (Button)findViewById(R.id.button2);
        bRestart=(Button)findViewById(R.id.button3);
        bStop  = (Button)findViewById(R.id.button4);

        bStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // MediaPlayer object initialization, playing
                mp = MediaPlayer.create(
                        getApplicationContext(), // the controller of the current screen
                        R.raw.music); // music file
                mp.setLooping(false); // true:Repeat indefinitely
                mp.start(); // Start playing songs

                int a = mp.getDuration(); // Playtime of the song (miliSecond)
                sb.setMax(a);// set the maximum range of the check bar to the playback time of the song
                newMyThread().start(); // Start thread to draw check bar
                isPlaying = true; // Checkbar thread to repeat

                bStart.setVisibility(View.INVISIBLE);
                bStop.setVisibility(View.VISIBLE);
                bPause.setVisibility(View.VISIBLE);
            }
        });

        bStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // End of music
                isPlaying = false; // Thread termination

                mp.stop(); // stop
                mp.release(); // release resource
                bStart.setVisibility(View.VISIBLE);
                bPause.setVisibility(View.INVISIBLE);
                bRestart.setVisibility(View.INVISIBLE);
                bStop.setVisibility(View.INVISIBLE);

                sb.setProgress(0); // Checkbar initialization
            }
        });

        bPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Pause
                pos = mp.getCurrentPosition();
                mp.pause(); // pause
                bPause.setVisibility(View.INVISIBLE);
                bRestart.setVisibility(View.VISIBLE);
                isPlaying = false; // Thread stop
            }
        });
        bRestart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Restart from where it stopped
                mp.seeTo(pos); // Move to Pause
                mp.start(); // Start
                bRestart.setVisibility(View.INVISIBLE);
                bPause.setVisibility(View.VISIBLE);
                isPlaying = true; // Change flag to play
                newMyThread().start(); // start thread
            }
        });
    } } // end of onCreate
    @Override
    protected void onPause() {
        super.onPause();
        isPlaying = false; // Thread stop
        if (mp!=null) {
            mp.release(); // release resource
        }
        bStart.setVisibility(View.VISIBLE);
        bStop.setVisibility(View.INVISIBLE);
        bPause.setVisibility(View.INVISIBLE);
        bRestart.setVisibility(View.INVISIBLE);
    }
}

I want to turn off the music when the app turns off Help me

actvitiy activity-stop-event

2022-09-21 21:24

2 Answers

During the activity where the song plays, press the "Go Back" button You're saying that you want the song to come out even if you move on to the previous activities, right?

Make it free.


2022-09-21 21:24

As you answered above, it must be implemented within the service in order to be able to play even when Activity is over.


2022-09-21 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.