Android Mediaplayer Error (-19, 0)
I try to replay a sound when I click on a button. But I get the Error (-19, 0) (what ever this means^^) My code: final Button xxx = (Button)findViewById(R.id.xxx); xxx.se
Solution 1:
I was getting the same problem, I solved it by adding the following code:
mp1 = MediaPlayer.create(sound.this, R.raw.pan1);
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
publicvoidonCompletion(MediaPlayer mp) {
mp.release();
};
});
Solution 2:
You need to release the previous media player before starting the new one.
Declare MediaPlayer
as a instance variable and then:
mp = null;
finalButtonxxx= (Button)findViewById(R.id.xxx);
xxx.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
if (mp != null) {
mp.stop();
mp.release();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.plop);
mp.start();
}
});
Or in your case, since you always play the same sound you don't need to release the player and create a new one, simply reuse the old one.
finalMediaPlayermp= MediaPlayer.create(getApplicationContext(), R.raw.plop);
mp.prepare(); // Blocking method. Consider to use prepareAsyncfinalButtonxxx= (Button)findViewById(R.id.xxx);
xxx.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
mp.stop();
mp.start();
}
});
Solution 3:
I sloved this problem by this code:
publicstaticvoidplaySound() {
mMediaPlayer = newMediaPlayer();
try {
AssetFileDescriptor afd = context.getAssets().openFd("type.mp3");
mMediaPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(newOnCompletionListener() {
@OverridepublicvoidonCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
arg0.release();
}
});
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i hope to help you.
Post a Comment for "Android Mediaplayer Error (-19, 0)"