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.
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();
}
}
Related
Can someone point out where I am going wrong with this? When I press the image button it always plays pinktestaudio first even if I did not press the corresponding button. I have to press the image button twice to hear the correct sound.This occurs when the page is first loaded, after the first time it seems fine but still something that shouldn't happen.
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class ColorPage extends AppCompatActivity {
Context context = this;
//MediaPlayer mpPurple, mpBlue, mpRed, mpGreen, mpYellow, mpPink;
MediaPlayer media = null;
//private static MediaPlayer media = null;
#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.isPlaying()) {
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.isPlaying()) {
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.isPlaying()) {
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.isPlaying()) {
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.isPlaying()) {
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.isPlaying()) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Updated: changing the start of the if statement(all of them) to as follows fixed the issue.
if (media != null) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
}
It always plays pinktestaudio because you have done
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
at the end, so media will always be initialized with pinktestaudio.
even after you click a different button because in every buttons OnClickListener you do
if(media.isPlaying()) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.pinkaudiotest);
}
on clicking any button for first time media.isPlaying() will always be false so
media = MediaPlayer.create(context, R.raw.some_audio_file);
will not be executed.
But when you click again media.isPlaying() is true and all goes well.
In onCreate 6 MediaPlayer instances are created. The last one is used in each of you listeners.
Take a look at your code after refactoring:
public class ColorPage extends AppCompatActivity implements View.OnClickListener {
MediaPlayer mMediaPlayer = null;
#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);
pinkB.setOnClickListener(this);
yellowB.setOnClickListener(this);
purpleB.setOnClickListener(this);
blueB.setOnClickListener(this);
greenB.setOnClickListener(this);
redB.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int audio;
switch (view.getId()) {
case R.id.pinkButton:
audio = R.raw.pinkaudiotest;
break;
case R.id.yellowButton:
audio = R.raw.yellowaudiotest;
break;
case R.id.purpleButton:
audio = R.raw.purpleaudiotest;
break;
case R.id.blueButton:
audio = R.raw.blueaudiotest;
break;
case R.id.greenButton:
audio = R.raw.greenaudiotest;
break;
case R.id.redButton:
audio = R.raw.redaudiotest;
break;
default:
audio = R.raw.purpleaudiotest;
}
try {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
}
mMediaPlayer = MediaPlayer.create(getApplicationContext(), audio);
mMediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Good luck!
This is because you have assigned the media variable a number of times and the last time it was assigned for pinkaudiotest so to solve your problem you have to assign the variable media inside the events. So it will be assigned only when a button is clicked.
`.
Here is what you are supposed to do using your own same code:
You have SIX lines that looks alike they are of the form of
media = MediaPlayer.create(context, R.raw.purpleaudiotest);
Cut those lines and paste them inside the onClick method of the corresponding Image Button for example. I am going to do the first one for you and repeat it for all SIX times.
purpleB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I AM ADDING IT HERE
media = MediaPlayer.create(context, R.raw.purpleaudiotest); //ADDED INSIDE oClick(View view)
try {
if (media.isPlaying()) {
media.stop();
media.release();
media = MediaPlayer.create(context, R.raw.purpleaudiotest);
}
media.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Repeat that for all SIX ImageButton and problem will be gone.
I am new to Android and in my app I am working with MediaPlayer.
When I first tap the start button, the song plays, but when I tap the stop button and then I tap on the start button again, the song does not start playing again. Here's what I have so far:
public class PlayngUrlFiles extends AppCompatActivity {
Button start, pause, stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playingurls_layout);
start = (Button) findViewById(R.id.button1);
pause = (Button) findViewById(R.id.button2);
stop = (Button) findViewById(R.id.button3);
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
}
});
}
}
Try this :
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp != null) {
mp.stop();
mp.release();
}
}
});
just check like this way
if(mdpl.isPlaying()){
mdpl.pause();
} else {
mdpl.start();
}
You can not call start, it media player in on stop state. Take a look on the state diagram of media player MediaPlayer state diagram.
When you call stop, you have to call release(), and with start you get a new media player, or just call reset.
try this
public void onStopBtnClick(View view) {
if (mAudioHelper != null) {
mAudioHelper.stop();
displayMessage("Stopping!");
}
}
Here is code to set sound file.
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
mPlayer = new MediaPlayer();
AppLog.d("Created new Media player");
try {
mPlayer.setDataSource(fileName);
} catch (IOException e) {
AppLog.e("Can't open " + fileName + " file", e);
}
Before playing u should call the mPlayer.prepareAsync() function.
if you want to pause/play you have to use this code:
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
If you want to stop player use this:
if (mPlayer != null){
mPlayer.stop();
mPlayer.release();
}
mPlayer = null;
If you want to start song again use first functions and next with this steps
This code may help you..
#Override
protected void onDestroy() {
super.onDestroy();
destroyMediaPlayer();
}
private void destroyMediaPlayer() {
if (mediaPlayer != null) {
try {
mediaPlayer.release();
Log.d("here", "destroy");
} catch (Exception e) {
e.printStackTrace();
}
}
}
SOLUTION 1
for this solution, I took some info from here
public class PlayngUrlFiles extends AppCompatActivity {
Button start, pause, stop;
MediaPlayer mp;
/**
* remain false till media is not completed, inside OnCompletionListener make it true.
*/
private boolean initialStage = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playingurls_layout);
start = (Button) findViewById(R.id.button1);
pause = (Button) findViewById(R.id.button2);
stop = (Button) findViewById(R.id.button3);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (initialStage) {
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
new Player()
.execute("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
} else {
if (mp && !mp.isPlaying())
mp.start();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp && mp.isPlaying())
mp.pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp) {
mp.stop();
mp.release();
mp = null;
initialStage = true;
}
}
});
}
/**
* preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
* #author piyush
*
*/
class Player extends AsyncTask<String, Void, Boolean> {
private ProgressDialog progress;
#Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean prepared;
try {
mp.setDataSource(params[0]);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
initialStage = true;
mp.stop();
mp.reset();
}
});
mp.prepare();
prepared = true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.d("IllegarArgument", e.getMessage());
prepared = false;
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
}
return prepared;
}
#Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progress.isShowing()) {
progress.cancel();
}
Log.d("Prepared", "//" + result);
mp.start();
initialStage = false;
}
public Player() {
progress = new ProgressDialog(PlayngUrlFiles.this);
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
this.progress.setMessage("Buffering...");
this.progress.show();
}
}
}
SOLUTION 2
in order to avoid data downloding, try this
public class PlayngUrlFiles extends AppCompatActivity {
Button start, pause, stop;
boolean prepared;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playingurls_layout);
start = (Button) findViewById(R.id.button1);
pause = (Button) findViewById(R.id.button2);
stop = (Button) findViewById(R.id.button3);
final MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
prepared = false;
mp.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
prepared = true;
mp.start();
}
});
try {
mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
} catch (Exception e) {
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp) {
try {
if(!prepared) {
mp.prepareAsync();
prepared = true;
} else {
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp) {
mp.pause();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp) {
mp.stop();
prepared = false;
}
}
});
}
}
#MRodrigues was right about media player states. But instead of calling release() after stop, Modify your code as below.
Currently,
try {
mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
modify to,
try {
mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
} catch (Exception e) {
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mp.isPlaying()) {
mp.prepare();
}
mp.start();
}
});
In my code, I have a start/stop button.I am trying to play an audio clip from the raw folder. On completion of an audio clip, the start/stop button has to change from stop mode to start mode. I don't want to loop the audio. For this,I have called OnCompletionListener. However, the listener is called only once. If the media player is reset and created again, OnCompletionListener is no longer called.
public void playsong(View view) {
if (mySound != null)
{
if (!pause) {mySound.start();
btnStartStop.setBackgroundResource(R.drawable.button);
pause = true;
} else {
if (mySound.isPlaying()) {
mySound.stop();
do_it();
}
}
}
}
mySound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mySound) {
do_it();
}
});
public void do_it() {
mySound.reset();
mySound = MediaPlayer.create(this, R.raw.a);
pause = false;
btnStartStop.setBackgroundResource(R.drawable.button2);
}
Try this code ?
In this example oncompletionlistner is called everytime
btnDemo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(objPlayer!=null){
if(objPlayer.isPlaying())
stopPlaying(objPlayer);
}
objPlayer = MediaPlayer.create(getApplicationContext(),R.raw.demoaudio);
if(objPlayer.isPlaying())
objPlayer.pause();
else
objPlayer.start();
Log.d("null", "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d("null", "Problem in Playing Audio");
}
objPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
objPlayer.release();
objPlayer = null;
};
});
}
});
I Have created a branch activity .Now i wanted to add two button on that branch activity.
When i click on 'sound on' button then my beep sound on start and when i clicked on 'sound off' then my beep sound off. and also they hide simultaneously.
Thank's
MY Code on Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
soundBttnOn =(Button) findViewById(R.id.soundBttnOn);
soundBttnOn.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
startMediaPlayer();
}
}
);
soundBttnoff =(Button) findViewById(R.id.soundBttnOff);
soundBttnoff.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
stopMediaPlayer();
}
}
);
}
private void startMediaPlayer() {
mediaPlayer = MediaPlayer.create(SoundLayout.this,R.raw.keybutton5);
mediaPlayer.start();
}
private void stopMediaPlayer() {
if( mediaPlayer != null ) {
MediaPlayer mp = null;
mp.stop();
mp.release();
}
}
It showing no problem but it is not working too..:P..I am not able to implement sound.
You can do simple google search to find tons of samples for adding button. However for playing sound file check out the MediaPlayer class.
Button startBtn, stopBtn;
//get the reference of Button
....
final MediaPlayer mp = new MediaPlayer();
startBtn.onClickListener() {
public void onClick(View v) {
try {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopBtn.onClickListener() {
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
//hide buttons
stopBtn.setVisibiltiy(View.GONE);
startBtn.setVisibility(View.GONE);
}
}
};
PS: For only on and off, you don't need two buttons, you could do with one button.
EDIT:
For single button, just use the playing state of Media player for deciding about the action to take on button click.
singleBtn.onClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()) mp.stop();
else {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
I have two buttons in my media player that streams a radio station, play and pause. I want to make it only one button that has two function. First click I want to play it and second click I want to pause it. And when I click it again I want to play it. I need help. Here is my code.
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
play.performClick();
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
}
private void play() {
Uri myUri = Uri.parse("Shoutcast URL");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
#Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
Create just one Button and add something like a buttonstatus. Then you can check the status in your listener.
For example:
boolean isPlaying = false;
playPause = (Button) findViewById(R.id.play);
playPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isPlaying) {
pause();
}else{
play();
}
isPlaying = !isPlaying;
}
});
Boolean b = false;
Button play = (Button) findViewById(R.id.play);
play..setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(b == false)
{
//write play code here
b= true;
}
else
{
// enter pause code here
}
}
}
You can use Toggle button:
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
play();
} else {
pause();
}
}
});
You can also remove the green bar from toggle button.
Toggle button Mkyong example
Android dev tutorial
You don't need to declare any boolean variable to check player state. There is a method already available in mediaplayer class named - isPlaying().
Try my code sequence for playpause using single button.
public void playpause(){
if(!mp.isPlaying()){ // here mp is object of MediaPlayer class
mp.start();
//showNotification("Player State Plying");
}
else if(mp.isPlaying()){
mp.pause();
//showNotification("Player State Pause");
}
}