嘿,伙计们在Android中使用MediaPlayer类时遇到了一个问题
在你建议SoundPool之前,我需要一个OnCompletion事件,当声音片段完成播放时触发,否则我会使用它。
所以问题是,当同时激活3个或更多个剪辑时,我只获得最后2个事件的OnCompletion,而不是所有这些事件,任何人都知道这个问题的解决方案或者知道它为什么会发生?
// When the user clicks on the Chicken Image, this Function is called
public void onChickenClicked(View view)
{
// ToastMsg( "You Clicked the Chicken", Toast.LENGTH_SHORT );
// Then play the sound,
mMediaPlayer[CHICKEN] = MediaPlayer.create(MainActivity.this, R.raw.chicken_sound );
// Start playing the sound
mMediaPlayer[CHICKEN].start();
// This is to catch when the sound clip has ended, this will be
// used to stop the animation for the chicken
mMediaPlayer[CHICKEN].setOnCompletionListener( new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
System.err.println("Chicken On Complete");
// Stop the animation of the Chicken here
mAnimation.cancel();
mAnimation.reset();
// Then switch the image back to the original Chicken pic
ImageView imgView = (ImageView) findViewById(R.id.imageViewofChicken);
imgView.setImageResource(R.drawable.chicken);
mMediaPlayer[CHICKEN].reset();
}} );
// Finds the ImageView, replaces the base img with the alternate img, and plays the sound
animatePicture( R.id.imageViewofChicken, R.drawable.chicken_tongue);
}
public void onCowClicked(View view)
{
// ToastMsg( "You Clicked the Cow", Toast.LENGTH_SHORT );
// Then play the sound,
mMediaPlayer[COW] = MediaPlayer.create(MainActivity.this, R.raw.cow_sound );
// Start playing the sound
mMediaPlayer[COW].start();
// This is to catch when the sound clip has ended, this will be
// used to stop the animation for the chicken
mMediaPlayer[COW].setOnCompletionListener( new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
System.err.println("Cow On Complete");
// Stop the animation of the Chicken here
mAnimation.cancel();
mAnimation.reset();
// Then switch the image back to the original Chicken pic
ImageView imgView = (ImageView) findViewById(R.id.imageViewofCow);
imgView.setImageResource(R.drawable.cow);
mMediaPlayer[COW].reset();
}} );
// Finds the ImageView, replaces the base img with the alternate img, and plays the sound
animatePicture( R.id.imageViewofCow, R.drawable.cow_tongue);
}
//there are more but you get it idea here.
任何人都对如何让所有OnCompleteListeners开火都有任何想法?
答案 0 :(得分:0)
所以我想通了,我只是用SoundPool替换了MediaPlayer,然后用了
private final ScheduledExecutorService mScheduler = Executors.newScheduledThreadPool(MAX_ANIMALS);
根据我在stackoverflow上使用MediaPlayer加载声音时看到的函数来处理声音剪辑的时间,然后以毫秒为单位获取持续时间,并将其用作调度程序的延迟({{1} })。
这是一个黑客,但它完成了。