How to access a variable from another class without creating a new instance


In the process of writing my first Android application, a simple media player, I simply practiced my semi-decent java / mark-up skills.
The application will display the current screen as the “main” page along with the song browser and pop-up music controls. I did a bit of work in both the first and the last, but I have a problem with MediaPlayer. If I start playing a song, it will play and pause until I exit the music control dialog, and then enter it again. At the moment, it seems that a second instance of the MediaPlayer object is being created.

Main Player Screen

package org.practicesession.zoomplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class zoomPlayer extends Activity implements OnClickListener {
    static MediaPlayer myPlayer;
    private static int stateMediaPlayer;

    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View returnButton = findViewById(R.id.button_return);
        returnButton.setOnClickListener(this);
        View playerControls = findViewById(R.id.album_art_panel);
        playerControls.setOnClickListener(this);
        View playlistView = findViewById(R.id.song_info_panel);
        playlistView.setOnClickListener(this);
        View shuffleButton = findViewById(R.id.player_shuffle_button);
        shuffleButton.setOnClickListener(this);
        View repeatButton = findViewById(R.id.player_repeat_button);
        repeatButton.setOnClickListener(this);
        View ratingButton = findViewById(R.id.player_rating_button);
        ratingButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.album_art_panel:
            Intent i = new Intent(this, PlayerControls.class);
            startActivity(i);
            break;
        }

    }

    public static void setStateMediaPlayer(int i){
        stateMediaPlayer = i;
    }

    public static int getStateMediaPlayer(){
        return stateMediaPlayer;
    }
}

Popup controls

package org.practicesession.zoomplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

//Extends to implement the functionality of the main page
public class PlayerControls extends Activity implements OnClickListener{
    private final int stateMP_Playing = 1;
    private final int stateMP_Pausing = 2;


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playcontrols);

        View exitButton = findViewById(R.id.controls_exit_pane);
        exitButton.setOnClickListener(this);
        View volUpButton = findViewById(R.id.controls_vol_up);
        volUpButton.setOnClickListener(this);
        View playButton = findViewById(R.id.controls_play);
        playButton.setOnClickListener(this);
        View volDownButton = findViewById(R.id.controls_vol_down);
        volDownButton.setOnClickListener(this);

        musicPlayback();
    }
    public void musicPlayback(){
        zoomPlayer.myPlayer = new  MediaPlayer();
        zoomPlayer.myPlayer = MediaPlayer.create(this, R.raw.outta);
        zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.controls_exit_pane:
            finish();
            break;
        case R.id.controls_play:
            switch(zoomPlayer.getStateMediaPlayer()){
                case stateMP_Playing:
                    zoomPlayer.myPlayer.pause();
                    zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
                    break;
                case stateMP_Pausing:
                    zoomPlayer.myPlayer.start();
                    zoomPlayer.setStateMediaPlayer(stateMP_Playing);
                    break;
            }
            break;
        }
    }


}

, Java, - . set/get, , .. .

, , , ... , , , . , , , .

!

+3
4

, , , . MediaPlayer.

Android. Java new . MediaPlayer , onCreate() PlayerControls. , startActivity() PlayerControls.

, Android Service, . , - . startForeground(), , Android, , , (, ).

+3

, , MediaPlayer? zoomPlayer.myPlayer = MediaPlayer(); ,

+3

You create a MediaPlayer in a popup from the onCreate method. Instead of creating an instance in a popup, do one of the actions in Activity.

0
source

As Brent said; maybe just create it if it doesn't exist yet:

public void musicPlayback()
{
    if (zoomPlayer.mPlayer == null)
    {
        zoomPlayer.myPlayer = MediaPlayer.create(this, R.raw.outta);
    }
    zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
}

(You should not create anyway new MediaPlayer- just use the static method create().

0
source

Source: https://habr.com/ru/post/1791552/


All Articles