How to play song again after stop it in android?
Hello Friends, I make one app in android, in which i take three radio button in radiogroup and take two buttons "play" and "stop". my problem is that when i play the song it is playing well but when i click on stop button and then click on play button then song is not playing, how to do that ? please help me if anybody know. below is code that i write:
package com.mediaplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {
MediaPlayer song1,song2,song3;
int whatsong = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);
song1 = MediaPlayer.create(MediaplayerActivity.this, R.raw.fluet);
song2 = MediaPlayer.create(MediaplayerActivity.this, R.raw.mogra_na_phool);
song3 = MediaPlayer.create(MediaplayerActivity.this, R.raw.airtel);
Button bPlay = (Button) findViewById(R.id.bPlay);
Button bStop = (Button) findViewById(R.id.bStop);
rgMusic.setOnCheckedChangeListener(this);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(song1.isPlaying())
{
song1.pause();
song1.seekTo(0);
}
if(song2.isPlaying())
{
song2.pause();
song2.seekTo(0);
}
if(song3.isPlaying())
{
song3.pause();
song3.seekTo(0);
}
switch (whatsong) {
case 1:
try {
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
case 2:
try {
song2.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song2.start();
break;
case 3:
try {
song3.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song3.start();
break;
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (whatsong) {
case 1:
song1.stop();
break;
case 2:
song2.stop();
break;
case 3:
song3.stop();
break;
}
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int rbId) {
switch (rbId) {
case R.id.rbMusic1:
whatsong = 1;
break;
case R.id.rbMusic2:
whatson开发者_运维问答g = 2;
break;
case R.id.rbMusic3:
whatsong = 3;
break;
}
}
}
now use this edited code. This code will help you. If it is not running properly inform me.
package com.mediaplayer;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {
MediaPlayer song1;
int whatsong = 0;
private ArrayList<Integer> songIds;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);
songIds=new ArrayList<Integer>();
songIds.add(R.raw.fluet);
songIds.add(R.raw.mogra_na_phool);
songIds.add(R.raw.airtel);
song1 = MediaPlayer.create(Nothing.this, R.raw.fluet);
Button bPlay = (Button) findViewById(R.id.bPlay);
Button bStop = (Button) findViewById(R.id.bStop);
rgMusic.setOnCheckedChangeListener(this);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(song1!=null)
{
song1.release();
}
switch (whatsong) {
case 1:
try {
song1 = MediaPlayer.create(Nothing.this, songIds.get(0));
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
case 2:
try {
song1 = MediaPlayer.create(Nothing.this, songIds.get(1));
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
case 3:
try {
song1 = MediaPlayer.create(Nothing.this, songIds.get(2));
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(song1!=null){
song1.release();
}
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int rbId) {
switch (rbId) {
case R.id.rbMusic1:
whatsong = 1;
break;
case R.id.rbMusic2:
whatsong = 2;
break;
case R.id.rbMusic3:
whatsong = 3;
break;
}
}
}
this problem is raise because you just detect whether song is play ? but you have also detect with true or false ?
like ::
if(song1.isPlaying()==true)
{
song1.pause(); //or
song1.stop():
}
else{
song1.start();
}
Update
for pausing the mediaplayer i used...
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
and for resuming the player from the position wer it stopped lately is by......
Mediaplayer.seekTo(length);
Mediaplayer.start();
for pausing and resuming media player you use this. i think it helped you.
if(song1.isPlaying()==true)
{
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
}
else
{
Mediaplayer.seekTo(length);
Mediaplayer.start();
}
mp.stop();
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.seekTo(0);
精彩评论