我需要编写一个非常简单的 android 应用程序,但我有一个严重的问题。
这是我的课:
package com.music.playa;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PlayMusic extends Activity {
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void playSound() {
try {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
} catch (Exception ex) {
}
String[] sounds = { "sound1", "sound2", "sound3" };
mediaPlayer = null;
int path = getResources().getIdentifier(
sounds[new Random().nextInt(sounds.length)], "raw",
getPackageName());
mediaPlayer = MediaPlayer.create(this, path);
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// do something...
}
});
mediaPlayer.setVolume(1, 1);
mediaPlayer.start();
}
}
问题是,当我一直按下按钮调用 PlaySound 函数时,过了一会儿(大约按下按钮 40-50 次),控制台会抛出很多错误:
05-28 12:56:00.202: E/MediaPlayer(755): error (-38, 0)
05-28 12:56:01.153: E/MediaPlayer(755): error (-19, 0)
05-28 12:56:01.153: E/MediaPlayer(755): Error (-19,0)
声音停止播放。
我应该怎么办?
安卓 SDK 2.3.3