Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing beep sounds into Android application

Tags:

android

audio

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
like image 298
Rob Avatar asked Feb 21 '23 21:02

Rob


1 Answers

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());
    
    
    }
like image 110
Ravi1187342 Avatar answered Feb 24 '23 18:02

Ravi1187342