Wednesday, 7 August 2013

Play sound file one after another in android

Play sound file one after another in android

I am trying to play sound in android one after from "res/raw" folder. But
it is only playing the first sound from sounds array. is there any other
way to do it?
There is three sound file in raw folder
1.aa.ogg 2.ma.ogg 3.ar.ogg
package com.protonray.calculatorplus;
import java.util.List;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
public class LayoutNew extends Activity{
int count=0;
int[] sounds={R.raw.aa,R.raw.ma,R.raw.ar };
MediaPlayer mp=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_new);
Button play=(Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count =0 ;
//Play the Frist sound
mp =MediaPlayer.create(LayoutNew.this,sounds[count]);
mp.start();
count =1 ;
}
});
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if(count<3){
mp.stop();
//Play next sound
mp
=MediaPlayer.create(LayoutNew.this,sounds[count]);
mp.start();
count++;
}
}
});
}
}

No comments:

Post a Comment