我有一个具有所需功能的应用程序.
Howerver,在某些时候会显示toast,我希望在显示祝酒词的同时播放双哔声以提醒用户显示正在显示的消息.
我不确定在Android中播放声音的最佳方法是什么,或者是否有一些我可以访问用于警报的默认声音.
一些指导将不胜感激!
谢谢
UPDATE
我的主要活动文件中有以下代码:
public void playAlertTone(final Context context){
Thread t = new Thread(){
public void run(){
MediaPlayer player = null;
int countBeep = 0;
while(countBeep<2){
player = MediaPlayer.create(context,R.raw.beep);
player.start();
countBeep+=1;
try {
Thread.sleep(player.getDuration()+100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
Run Code Online (Sandbox Code Playgroud)
我在res/raw中有一个名为beep的声音文件
如何在显示toast的if语句中调用此方法,以便2同时出现?
更新2:
这是我试图调用警报方法的代码:
if (elapsedTime > hourAlert)
{
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("HOUR PASSED");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 160);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
playAlertTone(getApplicationContext()); // Edited here now call
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您可以将音频文件放在项目的res/raw文件夹中
并在一个线程中播放音频
public void playAlertTone(final Context context){
Thread t = new Thread(){
public void run(){
MediaPlayer player = null;
int countBeep = 0;
while(countBeep<2){
player = MediaPlayer.create(context,R.raw.beep);
player.start();
countBeep+=1;
try {
// 100 milisecond is duration gap between two beep
Thread.sleep(player.getDuration()+100);
player.release();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
//call it like this from your activity' any method
if(myCondition){
Toast.makeText(getApplicationContext(), text, duration).show();
playAlertTone(getApplicationContext());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14751 次 |
| 最近记录: |