2012-04-06 85 views
4

我有一個具有所需功能的應用程序。在android應用程序中執行嗶聲聲音

Howerver,在特定的時間吐司顯示,我希望在顯示吐司的同時播放雙音,以提醒用戶顯示的信息。

我不確定在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(); 

      } 

我在資源名爲蜂鳴聲音文件/原

如何在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 

任何想法?

+0

發佈您正在顯示的代碼鋪設烤麪包並調用此方法 – Ravi1187342 2012-04-06 15:15:52

+0

查看UPDATE 2下的代碼片段 – Rob 2012-04-06 15:22:31

+0

我編輯了這個'playAlertTone(getApplicationContext());//現在編輯調用' – Ravi1187342 2012-04-06 15:27:35

回答

5

,你可以把你的音頻文件RES /你的項目

的原料文件夾並在一個線程中播放音頻

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


    } 
+0

好吧,我正在嘗試這種方法。我已經將此方法添加到了我的主要活動中,但我不確定如何在if語句中調用它。例如,我有一個計時器在10秒鐘後顯示敬酒,我怎麼能稱呼上面的方法來與敬酒同時播放嘟嘟聲。有任何想法嗎? – Rob 2012-04-06 14:35:42

+0

你可以打電話。將Context作爲參數傳遞給此方法。測試你的方法,讓我知道發生了什麼。 – Ravi1187342 2012-04-06 14:42:26

+0

想要這樣playAlertTone(context); ? - 它表示上下文無法解析爲變量 – Rob 2012-04-06 14:45:27

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

把你的音樂文件的資源文件夾