How to change the state of song from stop to start?
I have make one application in android in which i take three buttons in xml and in java i set the onclick event,
when play button is clicked song is prepared and play, when stop button is clicked song is stopped, when pause button is clicked song is paused.
But my problem is that when I clicked play button song is played after i click on stop button then song is stopped after I clicked on play button, but at this time song is not played,
the code I created for this app. is given below.
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(position > 0)
{
mp.seekTo(position);
mp.start();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.V开发者_StackOverflow中文版ISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
see this Updated code:
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp.isPlaying() == false)
{
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
Edit ::
you need to apply condition in play event like ::
if(mp.isplaying == false)
{
//play the song
}
bStop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
mp.release();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp.isplaying == false)
{
mp = MediaPlayer.create(this, R.raw.fluet);
}
}
});
精彩评论