我对 Android 应用程序开发相当陌生,我正在尝试播放 /res/raw 文件夹中的随机 .mp3。
已修复到目前为止我有这个,但我遇到了 FileNotFoundException。
FIXED仅在第一次单击时播放随机声音,之后除非重新打开应用程序,否则它是相同的声音。
新问题现在播放随机声音,但是当我多次单击该按钮时,声音同时开始播放,并且仍然在日志中收到“start() mUri is null”消息。
更新代码
MediaPlayer player;
int soundIndex;
AssetFileDescriptor descriptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* gets a random index from an array of sounds
*
* @return
*/
public int getRandomSoundIndex(){
int soundIndex;
int[] sound = SOUNDZ;
Random random = new Random();
soundIndex = random.nextInt(sound.length);
return sound[soundIndex];
}
/**
* Plays that random sound on button click
* @param button
*/
public void playRandomSound(View button){
//where button is physically located
button = (Button) findViewById(R.id.button1);
//get random sound index
soundIndex = getRandomSoundIndex();
//make media player
player = MediaPlayer.create(this, soundIndex);
//play sound
player.start();
}
这是日志:
09-21 17:42:32.528: D/MediaPlayer(4282): start() mUri 为空