4

我有一个具有所需功能的应用程序。

但是,在某些时候会显示祝酒词,我希望在显示祝酒词的同时播放双哔声,以提醒用户注意正在显示的消息。

我不确定在 android 中播放声音的最佳方法是什么,或者是否有一些我可以访问用于警报的默认声音。

更新

我的主要活动文件中有以下代码:

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

            }

我在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
4

3 回答 3

5

您可以将音频文件放在项目的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());
    
    
    }
于 2012-04-06T14:18:26.120 回答
2

使用MediaPlayer,

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

有很多关于如何使用的知识,在上面的链接中有介绍。这是一个说明用法的衬衫片段,

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();
于 2012-04-06T14:04:57.133 回答
1
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();
}

将您的音乐文件放在资产文件夹中

于 2012-04-06T14:05:11.000 回答