Issue
I have everything else working fine but when I minimize that app and then go back and try to resume my image buttons will not play the audio files associated with them. I have to go back to the start screen of the app and go back to the activity page with the image buttons just to get them to play the audio again. any hints would help.
public class ColorPage extends AppCompatActivity {
Context context = this;
MediaPlayer media = null;
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(media!=null){
media.stop();
media.release();
media = null;
}
}
@Override
protected void onResume() {
super.onResume();
if(media == null) {
media.start();
}
}
@Override
protected void onPause() {
super.onPause();
if(media != null) {
media.pause();
media.release();
media = null;
}
}
this is the code I'm using. everything else seems to work except the onResume().
I also used if(media != null) but all it did was cause the last audio file to play automatically every time I opened the activity page. If(media == null) was just the last thing I tried.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_page);
ImageButton pinkB = (ImageButton) findViewById(R.id.pinkButton);
ImageButton yellowB = (ImageButton) findViewById(R.id.yellowButton);
ImageButton purpleB = (ImageButton) findViewById(R.id.purpleButton);
ImageButton blueB = (ImageButton) findViewById(R.id.blueButton);
ImageButton greenB = (ImageButton) findViewById(R.id.greenButton);
ImageButton redB = (ImageButton) findViewById(R.id.redButton);
media = MediaPlayer.create(context, R.raw.purpleaudiotest);
purpleB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.purpleaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
media = MediaPlayer.create(context, R.raw.blueaudiotest);
blueB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.blueaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
media = MediaPlayer.create(context, R.raw.redaudiotest);
redB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.redaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
media = MediaPlayer.create(context, R.raw.greenaudiotest);
greenB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.greenaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
media = MediaPlayer.create(context, R.raw.yellowaudiotest);
yellowB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.yellowaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
pinkB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
This is the rest of the code for the class ColorPage so my question is a bit more clear.
Solution
Your checks in the each of the color button onClick only initialize the media when it is not null, (very strange) and in this case it also happens to be the problem. Your onPause nulls out the media, and onResume doesn't reinitialize it, so that if (media!=null) is always false, in fact, you probably gets npe on media.start() after that. handle the click like so.
if (media != null) {
media.stop();
media.release(); // release previous media if not null
}
// initialize this outside of the if block
media = MediaPlayer.create(context, R.raw.pinkaudiotest); // whatever color in each
media.start();
You've set media to null in onPause, assuming you want to just start where the audio has left off onResume. If you don't want to automatically start, there doesn't seem to be a point to implement onResume at all.
@Override
protected void onResume() {
super.onResume();
if(media != null) {
media.start();
}
}
Answered By - Andrew Lam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.