I am having a listview in my app. Each listview item has button which will play an audio from a url received from web service. But my problem is that if I click play button from the next item then both start playing together. I am having problem in this. I want only one to play at a time. Right now I am creating new Media player object everytime button is clicked, but I also tried creating a single global object but in this case it only plays first time and not after it. What is the possible solution of it.
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Make the mp variable global and remove this:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
because if you take a look at this reference http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram it says:
Once the MediaPlayer object is in the End state, it can no longer be
used and there is no way to bring it back to any other state.
And when you call mp.release(); the media player WILL go to that state.
Then make your onClickListener look something like this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
//... and so on
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
The trick there is to make it back to Idle state so you can set the new data source and start playing again. It's all about the states...
Related
I'm testing Android sound implementation. I have an "imagenButton" and when I push it, a method "musica()" is called. Is really simple:
public void musica(View v) {
img = (ImageView) findViewById(R.id.Migrunido);
if(m.isPlaying()) {
m.stop();
}
else {
m.start();
}
}
It works, but want when I press the button, the sound repeats. I was reading a lot about threads and I have to prepare it before "stop()" but can't fix it.
I have to prepare my method? Or what is the problem?
Thanks
try
public static void playAudio(int id) {
mediaPlayer = MediaPlayer.create(context,id);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
}else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
if this doesn't work try mediaPlayer.prepare(); before you start.
I am trying to find out how to use the android's MediaPlayer method setNextMediaPlayer which should smoothly transition from one player (song) to another. But do not know how to use the method since there is a lack of documentation.
This is what i do and it does not work:
final MediaPlayer mp1 = new MediaPlayer();
final MediaPlayer mp2 = new MediaPlayer();
try {
mp1.setDataSource("http://song1.MP3");
mp2.setDataSource("http://song2.mp3");
mp1.prepareAsync();
mp1.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
mp1.setNextMediaPlayer(mp2);
mp1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.prepareAsync();
mp2.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp2.start();
}
});
}
});
} catch (IllegalArgumentException e) {}
So the first song plays. But after it finishes the second does not start.
Got it to work.
mp1.setDataSource(song1);
mp1.prepareAsync();
mp1.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
//mp1.start();
//mp1.seekTo(1000*100);
mp2.setDataSource(song2);
mp2.prepare();
mp1.setNextMediaPlayer(mp2);
The only problem is that the second mediaplayer cannot be called as async (i.e. mp2.prepareAsync()). I do not know why, but if you try it like that it does not start, which is a bad thing :/.
MediaPlayer setNextMediaPlayer gives a lot of problem what i do is this:
in the onCompletion() reset() the mediaplayer, setDataSource() and prepareAsync().
Something like this
mp1.setDataSource("http://song1.MP3");
mp1.prepareAsync();
mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp1.start();
}
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.reset();
//ADD FLAG FOR STOP
try {
mp1.setDataSource("http://song2.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mp1.prepareAsync();
}
});
Then in the onCompletion you need some flag to check if the last song was played to stop, because the code as i posted will loop in the last song.
I have several MediaPlayers in the same activity. It works this way:
MediaPlayer mpOne starts playing, if a variable has a value "yes" MediaPlayer mptwo starts playing, else mpThree starts.
When the audio ends, starts another process identical
This is my code:
MediaPlayer mpOne,mpTwo,mpThree,mpFour,mpFive
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morning);
mpOne= MediaPlayer.create(this, R.raw.one);
mphora.setLooping(false);
mpTwo= MediaPlayer.create(this, R.raw.two);
mphora.setLooping(false);
mpThree= MediaPlayer.create(this, R.raw.three);
mphora.setLooping(false);
mpFour= MediaPlayer.create(this, R.raw.four);
mphora.setLooping(false);
mpFive= MediaPlayer.create(this, R.raw.five);
mphora.setLooping(false);
mpOne.start();
mpOne.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
if (variable.equals("yes"){
mpTwo.start();
} else {
mpThree.start();
}
});
mpTwo.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpThree.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mpFour.start();
});
mpFour.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
// same proccess
});
}
My problem is when I want to stop all audios.
Currently I have a button that does the following:
stop.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(mpOne.isPlaying()){mpOne.stop();mpOne.release();}
else if(mpTwo.isPlaying()){mpTwo.stop();mpTwo.release();}
else if(mpThree.isPlaying()){mpThree.stop();mpThree.release();}
else if(mpFour.isPlaying()){mpFour.stop();mpFour.release();}
else if(mpFive.isPlaying()){mpFive.stop();mpFive.release();}
}
}
});
The problem is that sometimes it works and sometimes the audio still playing.
I've searched and found similar problems, but not a solution
Can anyone tell me a safe way to stop all playback?
Thank you very much in advance.
regards
You can do only one MediaPlayer instance and at onCompletion setting a different audio source
How to play audio and wait to finish to countinue to play again?? . In this i can play so many time :D
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
if (mMediaPlayer.isPlaying() == false) {
mMediaPlayer.start();
mMediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
}
});
The code is working fine for every button click. If you wish the audio to be repeated automatically, use
mMediaPlayer.setLooping(true);
If you wish that when Ist sound is completely played then, second one should start on a button click,
try disabling ur button click while the first sound is playing and then enable the click onCompleteListener... Some thing like this:
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
ivSelect.setClickable(false); //Disabling the button click
mMediaPlayer.start();
}
Enable click on Audio completion
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
ivSelect.setClickable(true);
}
});
Try this
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
mMediaPlayer.release();
}
guys scenario is that suppose i click a button, the sound plays and within the duration of that track i again click the button and want to play it from the beginning. i tried with the following code, but no success.
code is :
public class SoundtestActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.gun_shot);
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
mp.reset();
mp.start();
}
}));
}
}
Try this code
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp.start();
}
});
Or replace mp.start(); by mp.reset(); in this code
Best way and the easiest way is =
mp.setLooping(true);
And yes it does work, just simply add this line after mp.start() and nothinfg else is required
You just need to comment the following lines and shift your Media player instance in the button's listener method, bingo your done with multiple media players playing parallel to each other
Button click=(Button) findViewById(R.id.bt1);
click.setOnClickListener((new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(this, R.raw.gun_shot); // ADD THIS LINE HERE
if(mp.isPlaying()){
mp.stop(); //ADDED TO STOP FIRST
mp.release(); //ADDED TO RELEASE RESOURCES
}
mp.start();
}
}));