Android : Playing Image and audio simulteniously in android

on Saturday, September 13, 2014


I am trying to play audio and image concurrently in android...i have take one image-view and two buttons for go to back and go to next..when i click on next button and go on its working perfectly fine but as soon as i clicked on back button only audio file changes not the image. if i again clicked on back button then image changes but with another audio file..please help me out with this..thanks here is my code



public class Main extends Activity{

Button btn_back,btn_play,btn_next;
ImageView img_view;
int current_img = 0;
int current_aud = 0;
MediaPlayer mp;

int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,
R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,
R.drawable.img11,R.drawable.img12};

int[] audio = {R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,R.raw.aud,R.raw.a,
R.raw.aud,R.raw.a,R.raw.aud,R.raw.a};

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn_back = (Button)findViewById(R.id.button1);
btn_play = (Button)findViewById(R.id.button2);
btn_next = (Button)findViewById(R.id.button3);
img_view = (ImageView)findViewById(R.id.imageView1);
img_view.setImageResource(R.drawable.ic_launcher);
mp =new MediaPlayer();

btn_play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop();
}
});

btn_next.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
current_img = current_img % images.length;
img_view.setImageResource(images[current_img]);
current_img ++;
try {
if(mp != null){
if(mp.isPlaying()){
mp.stop();
// mp.release();
}
}


current_aud = current_aud % audio.length;

mp.reset();
mp = MediaPlayer.create(Main.this, audio[current_aud]);
mp.start();
current_aud ++;





} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}
});

btn_back.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

current_img = (current_img + images.length) % images.length;
current_img --;
img_view.setImageResource(images[current_img]);
try {
if(mp != null){
if(mp.isPlaying()){
mp.stop();
// mp.reset();
}
}



current_aud = (current_aud + audio.length)% audio.length;
mp.reset();
mp = MediaPlayer.create(Main.this, audio[current_aud]);
current_aud --;
mp.start();





} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

});

0 comments:

Post a Comment