Android: button is not changing to pause state when i click - java

i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button
when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong
but i need the song to be paused so whereever i used
mp.start();
function which will be start to play the music from the url i added the code as
if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}
but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously
please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function
the full code of the project is
public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {
MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
//mp.start();
/*if(mp.isPlaying()== true)
{
mp.start();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.pause();
mp.release();
iv.setImageResource(R.drawable.play);
}*/
}
#Override
public void onClick(View v) {
try
{
pd = new ProgressDialog(this);
pd.setMessage("Buffering.....");
//pd.show();
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
mp.prepareAsync();
mp.setOnCompletionListener(this);
}
catch(Exception e)
{
Log.e("StreamAudioDemo", e.getMessage());
}
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();
}}
thanks for your help in advance
i have commented some code above i have to re correct it

you can have a try at your onclickListener,when this player is playing,if you want to pause it ,you should to call MediaPlayer.pause() ,if you want to release it data,you can call MediaPlayer.release(),then ,you can play next music.

use this
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
// mp.start(); - this should be removed
if(mp.isPlaying()== true)
{
mp.pause();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.start();
mp.release();
iv.setImageResource(R.drawable.play);
}
}

Related

Adding music in android studio (java, MediaPlayer)

buttonMusic = findViewById(R.id.buttonMus);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick();
}
private Button buttonMusic;
private MediaPlayer musicSound;
public void buttonClick() {
buttonMusic.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
}
);
}
public void soundPlay(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.stop();
}else {
sound.start();
sound.setLooping(true);
} }
Hello.
The code launches the music, is able to stop it, but it wont play again after pressing the play button, after pausing the song that is.
you aren't pausing song, you are stop()ing it. use sound.pause() for pausing :)
after stop() MediaPlayer you have to prepare it again, use prepare() or prepareAsync(). MediaPlayer.create( makes that for you at the beginning, also if you would create MediaPlayer using its constructor you would also want to prepare()/preapreAsync() before calling start() in onClick
check out state diagram of MediaPlayer

How to stop the sound/audio when you press the back button in the phone

how can i stop the audio from playing when i press the back button in my phone. when i go to another activity the audio is still playing unless i close the app. here's my code:
FloatingActionButton fab7 = (FloatingActionButton) this.findViewById(R.id.fab7);
final MediaPlayer mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
});
1.) declare MediaPlayer mp; outside onCreate
2.) override onStop and apply the same logic of click listener inside onStop
why onStop not onBackPress?
Because you want to pause the player when you go outside app as well as when you go to another activity
MediaPlayer mp;
onCreate(..){
mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
pausePlayer();
}
});
}
#Override
public void onStop(){
super.onStop()
pausePlayer();
}
void pausePlayer(){
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
#Override
public void onBackPress() {
pausePlayer();
mp.stop();
}
Use this to detect and pause the Mediaplayer
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed Song paused");
mp.pause();
}
return super.onKeyDown(keyCode, event);
}
write mediaplayer.stop();
on start of back activity when u gone
Override Activity#onBackPress() method to stop your player when pressing Back button:
#Override
public void onBackPress() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
If you want to stop your player when your Activity finish, and don't care why (may be Back press, another Activity started, app crashed,..), you can override Activity#onStop() so you will make sure your music is immediately stop whenever your Activity gone!
#Override
public void onStop() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onStop();
}
Btw, note that you should use mp.stop() instead of mp.pause() then mp.release() to avoid memory/resources leak!

Playing audio repeat Android studio

I am playing some audio via a button is pressed. The audio plays fine, but i want to be able to spam the button and let the audio play play play play. Even stop where its playing and play again if i press the button. Now when i press the button, the audio just plays all the way through and nothing happens if i press the button while the audio is playing. Here is my code:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
mp.setLooping(true);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
Replace this code with your existing code. if you want to repeat your audio then you have to set the looping property to true.
OK - I hope I got this right:
You want to play the audio clip for every time you press the button and the clip should be played completely, before the next clip starts.
Here's what I'd do:
Create a new integer attribute.private int i = 0;
Increment it every time you press the button.
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IUP();
}
});
public synchronized void IUP(){
i++;
}
public synchronized void IDOWN(){
i--;
}
public synchronized int getI(){ return i; }
public void startAudio(){ mp.start(); }
public boolean isPlaying(){ return mp.isPlaying(); }
Use a seperate thread for the player.
Thread player_thread = new Thread(new Runnable(){
#Override
public void run(){
while(true){
if(getI() > 0 && !isPlaying()){
IDOWN();
startAudio();
}
}
}
}).start();
Or use an OnCompletionListener on the MediaPlayer.
Tell me if this is what you were looking for.
final MediaPlayer mp = MediaPlayer.create(this, R.raw.audio1);
Button play_button = (Button)this.findViewById(R.id.button1);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(play_button.getText().toString().equals("Stop")
{
mp.stop();
play_button.setText("Play");
}
else
{
mp.start();
play_button.setText("Stop");
}
}
});

When I click pause button it paused but when i clicked again play button , it starts the song from start

How can I start the song from where I pressed pause button, help me out am new to media in android and am unaware from the media properties all the buttons are working fine , but I want to start the song where from at which point I stopped it -
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
Button play, stop,pause;
Uri path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
path = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.chelseafc);
mediaPlayer = MediaPlayer.create(MainActivity.this,path);
mediaPlayer.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
});
}
#Override
protected void onDestroy() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer= null;
}
super.onDestroy();
}
}
You have a start button, pause button and stop button. There is no Resume button. Also when you click start after pressing pause, media loads again and starts from beginning (length = 0). You have to seek it to the paused position and resume it :
Define a global variable :
int length = 0;
In pause listener:
length=mediaPlayer.getCurrentPosition();
In resume (create a resume listener):
mediaplayer.seekTo(length);
mediaPlayer.start();
UPDATE
Ideally pause and resume button would be same except you would toggle on-off with changing image resource between play and pause pngs and execute relevant code with help of a boolean variable :
In pause button listener:
if(play)
{
//paused
play =false;
pause.setImageResource(R.id.pause);
length=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}else {
//resumed
play = true;
pause.setImageResource(R.id.resume);
mediaplayer.seekTo(length);
mediaPlayer.start();
}
Correct wherever necessary I typed in the editor not in IDE.
Use following code -
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.reset();
I think it will help you.

Replaying the sound as many times as button is clicked

I have the following code:
if (someId.matches("A") || someId.matches("a")) {
tvLetCap.setText("A");
tvLetLow.setText("a");
ivLetterIcon.setImageResource(R.drawable.apple);
btnDisplayWord.setText("A is for APPLE");
mpSound = MediaPlayer.create(this, R.raw.sound);
mpSound.setLooping(false);
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
/*try {
Uri uri = Uri.parse("android.resource://com.mypack.testing/" + R.raw.sound);
mpSound.setDataSource(getApplicationContext(),uri);
mpSound.prepare();
*/
mpSound.start();
btnPlay.setVisibility(View.GONE);
btnStop.setVisibility(View.VISIBLE);
btnStop.setOnClickListener(stopSound);
/*}
catch (IOException e){
Log.e("REPLAYING", "prepare() failed");
}*/
}
});
mpSound.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mpSound.release();
btnPlay.setVisibility(View.VISIBLE);
btnStop.setVisibility(View.GONE);
}
});
}
It plays fine with the codes commented out but whenever I try to replay the file my app FC. The sound file is located in res/raw/sound.mp3
How can I modify the code so it plays as many times as the btnPlay is pressed.
You've released the media player. If you want to reuse it, don't do that.
There are some methods for mediaplayer...
http://developer.android.com/reference/android/media/MediaPlayer.html
Maybe you need to use mpSound.reset() in onClick method for button (some time ago i had such mistake :p)? :)

Categories

Resources