在Toast,Eclipse之后插入声音

时间:2014-09-05 18:51:03

标签: android eclipse

我做了一个像记忆游戏这样的程序。我有个问题。我想插入一个声音(一声嘟嘟声或说“你输了”的东西),但我不知道该怎么做。我读过,但只是特定项目的一切。我尝试使用Toolkit(我阅读了一些东西),但我没有解决任何问题。你能帮助我吗?这是代码。

 Timer t = new Timer(false);
 t.schedule(tt, 1000);

 if (turns == homemanytries.intValue()) {
     Context context = getApplicationContext();
     CharSequence text = "Woah, took you too much tries!";
     int duration = Toast.LENGTH_LONG;
     Toast toast = Toast.makeText(context, text, duration);
     toast.show();
     onCreate(null);
 }

2 个答案:

答案 0 :(得分:0)

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.beep);
mp.setVolume(1f, 1f); //if you want to play sound when volume is 0
mp.start();

答案 1 :(得分:0)

要在代码下方播放声音,教程here 获取有松散声音的音频,并将其替换为R.raw.loose

MediaPlayer mp = MediaPlayer.create(context, R.raw.you_loose);
mp.start();