How do I access a variable from another class without it creating a new instance
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.setOnClick开发者_JAVA百科Listener(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;
}
}
}
It seems to be a normal java problem rather than anything else. I've tried set/get methods, static variables, removing inheritance etc. to no avail
Do note too that the code above is the result of fooling around with this for hours so it's not as tidy as it can be...for example, the controls screen originally inherited from main screen as many of the imports and methods are shared. I removed this to see if it was causing the problem but no joy
Thanks for any help!
If I start a song playing, it will play and pause fine until I exit the music controls dialog and then re-enter it. At this point it seems to create a second instance of the MediaPlayer object.
That's what your code tells Android to do. In Java, the new
operator creates a new instance of a class. You are creating a new MediaPlayer
every time onCreate()
of a PlayerControls
activity is called. That will occur every time you call startActivity()
to launch a PlayerControls
activity.
Typically, in Android, a music player would be implemented via a Service
that does the actual music playback. That way, the user is not tied to having any specific activity of yours around. The service would use startForeground()
to make sure it is not shut down by Android, plus this puts an icon in the status bar to remind the user that your app is what's causing the music (probably also giving them rapid access back to your activity).
I'm just guessing here but is it because your creating a new MediaPlayer each time? zoomPlayer.myPlayer = new MediaPlayer(); Try creating it elsewhere and then just initializing it there
You're instantiating the MediaPlayer in the popup from the onCreate method. Instead of instantiating it in the popup, do it once in the Activity.
As Brent said; perhaps just create it if doesn't already exist:
public void musicPlayback()
{
if (zoomPlayer.mPlayer == null)
{
zoomPlayer.myPlayer = MediaPlayer.create(this, R.raw.outta);
}
zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
}
(You shouldn't create a new MediaPlayer
anyway - just use the static create()
method.
精彩评论