2017-03-07 90 views
0

Iam開發具有鈴聲設置的android應用程序。問題是我想將我的鈴聲設置爲8秒。 Iam新的android。請任何人都可以幫我設置。謝謝。真的很抱歉我的英語。這裏是我的代碼在android中設置鈴聲響鈴時間

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     mMediaPlayer = new MediaPlayer(); 
     mMediaPlayer.setDataSource(this, alert); 
     final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { 
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); 
      mMediaPlayer.setLooping(true); 
      mMediaPlayer.prepare(); 
      mMediaPlayer.start(); 

回答

1

這裏我使用Timer來停止鈴聲。

long ringDelay = 3500; 
Uri notification =RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
final Ringtone alarmRingtone= RingtoneManager.getRingtone(getApplicationContext(), notification); 
alarmRingtone.play(); 
TimerTask task = new TimerTask() { 
@Override 
public void run() { 
    alarmRingtone.stop(); 
} 
}; 
Timer timer = new Timer(); 
timer.schedule(task, ringDelay);