Rob
Rob

Reputation: 1539

Implementing beep sounds into Android application

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

Upvotes: 4

Views: 16718

Answers (3)

Ravi1187342
Ravi1187342

Reputation: 1277

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());
    
    
    }

Upvotes: 5

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22637

Use MediaPlayer,

http://developer.android.com/reference/android/media/MediaPlayer.html

There's a lot to know about how to use, which is covered in the link above. Here's a shirt snippet that illustrates the usage,

final MediaPlayer mediaPlayer = new MediaPlayer();
try {
    mediaPlayer.reset();
    mediaPlayer.setDataSource(...);
    mediaPlayer.prepare();
} catch (IllegalStateException e) {
    mediaPlayer.release();
} catch (IOException e) {
    mediaPlayer.release();
} catch (IllegalArgumentException e) {
    mediaPlayer.release();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

    public void onCompletion(MediaPlayer mp) {
        mediaPlayer.release();
            }
    });
mediaPlayer.start();

Upvotes: 2

MAC
MAC

Reputation: 15847

try {
    AssetFileDescriptor afd = getAssets().openFd("gavel_single.wav");
    mMediaplayer = new MediaPlayer();
    mMediaplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    afd.close();
    mMediaplayer.prepare();
    mMediaplayer.start();
    mMediaplayer.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mMediaPlayer) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
    });
} catch (Exception e) {
        e.printStackTrace();
}

put your music file in asset folder

Upvotes: 1

Related Questions