I have an application with the desired functionality.
Howerver, at certain times toasts are displayed and I want a double beep to play at the same time that the toasts are displayed to alert the user to the messages being displayed.
I'm not sure what the best approach is for playing sounds in android or if there is some default sounds that I could access to use for the alerts.
UPDATE
I have the following code in my main activity file:
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();
}
I have a sound file named beep in res/raw
How can I call this method in an if statement where a toast is displayed so the 2 occur at the same time?
UPDATE 2:
Here is the code where I'm trying to call the alerting method:
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
You can put your audio file in res/raw folder of your Project
and play audio in a thread
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With