2011-07-22 133 views
3

我正在創建鬧鐘應用程序。我列出了所有可用的鈴聲(不僅是標準鬧鐘鈴聲)。當用戶從列表中點擊音調時,我使用MediaPlayer播放。在此期間,我還希望能夠使用音量按鈕(在電話上)調整鬧鐘音量。但是當我按下這些按鈕時,我調整了媒體音量,而不是報警音量。安卓鬧鐘調整鬧鐘音量

有沒有解決這個辦法嗎?

這裏是我使用的顯示色調

代碼
private void chooseTone(final ArrayList<String> tones, final ArrayList<String> paths) { 
    //final String lastRingtone = tuneName.getText().toString(); 
    //int i = tones.indexOf(lastRingtone); 
    int i ; 
    try { 
     i = paths.indexOf(crt.tune); 
    } catch (NullPointerException ex) { 
     i = -1; 
    } 
    final AlertDialog alert = new AlertDialog.Builder(this).setTitle("Ringtone"). 
      setSingleChoiceItems(tones.toArray(new String[]{}), i, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialogInterface, int index) { 
        String currentPath = paths.get(index); 
        try { 
         if (mp != null) 
          mp.stop(); 
         mp = new MediaPlayer(); 
         mp.setDataSource(currentPath); 
         mp.prepare(); 
         mp.seekTo(0); 
         mp.start(); 
        } catch (Exception ex) { 
        } 
        selectedRingtoneIndex = index; 
       } 
      }).create(); 

    alert.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialogInterface, int i) { 
        tuneName.setText(tones.get(selectedRingtoneIndex)); 
        //uri = Uri.parse(paths.get(selectedRingtoneIndex)); 
        crt.tune = paths.get(selectedRingtoneIndex); 
        try { 
         mp.stop(); 
        } catch (Exception ex) {} 
       } 
      }); 
    alert.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialogInterface, int i) { 
        try { 
         mp.stop(); 
        } catch (Exception ex) {} 
       } 
      }); 
    alert.show(); 
} 
+0

加百列,你有沒有解決過這個問題? – pgsandstrom

+0

@sandis不是真的:(我終於讓它成爲了,並添加了一個選項來配置設置中的鬧鐘音量。希望我已經能夠做到這一點,儘管 – Gabriel

回答

0

用途:

setVolumeControlStream(AudioManager.STREAM_ALARM); 
+0

感謝您的幫助,直到用戶開始播放手機鈴聲雖然鈴聲在播放,但是,您只能調整媒體音量,有沒有辦法解決這個問題?我認爲必須有一種方法來告訴MediaPlayer播放音樂爲「警報」,並且而不是「媒體」。 – Gabriel

0

我想你也應該設置AudioStreamType用於媒體播放器。

mp = new MediaPlayer(); 
mp.setAudioStreamType(AudioManager.STREAM_ALARM);