mediaplayer failure exception
Posted
by
Rahulkapil
on Stack Overflow
See other posts from Stack Overflow
or by Rahulkapil
Published on 2012-07-05T15:14:01Z
Indexed on
2012/07/05
15:15 UTC
Read the original article
Hit count: 296
I am working on an android application in which i have to play random sounds from my assets folder. there are some images also, when i click on any image from those images a sound must play regarding to that image from assets folder. i managed all but sometime my mediaplayer fails unexpectedly.
I am attaching my code also.
private Handler threadHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
/*first*/ try{
InputStream ims1 = getAssets().open("images/" +dataAll_pic_name1);
d1 = Drawable.createFromStream(ims1, null);
rl1.setVisibility(View.VISIBLE);
img1.setImageDrawable(d1);
AssetFileDescriptor afd = getAssets().openFd("sounds/" + str_snd1);
mp2 = new MediaPlayer();
mp2.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
/*second*/ try{
InputStream ims2 = getAssets().open("images/" +dataAll_pic_name2);
d2 = Drawable.createFromStream(ims2, null);
rl2.setVisibility(View.VISIBLE);
img2.setImageDrawable(d2);
AssetFileDescriptor afd = getAssets().openFd("sounds/" + str_snd2);
mp2 = new MediaPlayer();
mp2.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
/*third*/ try{
InputStream ims3 = getAssets().open("images/" +dataAll_pic_name3);
d3 = Drawable.createFromStream(ims3, null);
rl3.setVisibility(View.VISIBLE);
img3.setImageDrawable(d3);
AssetFileDescriptor afd = getAssets().openFd("sounds/" + str_snd3);
mp2 = new MediaPlayer();
mp2.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
/*four*/ try{
InputStream ims4 = getAssets().open("images/" +dataAll_pic_name4);
d4 = Drawable.createFromStream(ims4, null);
rl4.setVisibility(View.VISIBLE);
img4.setImageDrawable(d4);
AssetFileDescriptor afd = getAssets().openFd("sounds/" + str_snd4);
mp2 = new MediaPlayer();
mp2.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
startAnimation();
//randomSoundPlay();
timer.schedule( new TimerTask(){
public void run() {
System.out.println("Wait, what........................:");
try{
AssetFileDescriptor afd = getAssets().openFd("sounds/" + dataAll_sound_name);
mp2 = new MediaPlayer();
mp2.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp2.prepare();
mp2.start();
mp2.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
vg1.setClickable(true);
vg2.setClickable(true);
vg3.setClickable(true);
vg4.setClickable(true);
btn_spkr.setVisibility(View.VISIBLE);
txtImage();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
}, delay_que);
}
});
}catch(Exception e){
e.printStackTrace();
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
};
in above code random images and sound sets in my activity. now when i click on any image a sound must play but sometimes it fails.. i tried but unable to resolve this issue. help me out. thanks in advance.
© Stack Overflow or respective owner