Skip to content

Instantly share code, notes, and snippets.

@zi6xuan
Created March 29, 2018 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zi6xuan/4c776dd6039e02f06b591c7a10446b19 to your computer and use it in GitHub Desktop.
Save zi6xuan/4c776dd6039e02f06b591c7a10446b19 to your computer and use it in GitHub Desktop.
部分手机或系统播放系统提示音,会循环播放,设置一个超时自动停止
public static void playSystemSound(int type) {
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if (am == null) return;
final int ringerMode = am.getRingerMode();
switch (ringerMode) {
case AudioManager.RINGER_MODE_SILENT: //
{
//do nothing
}
break;
case AudioManager.RINGER_MODE_VIBRATE: //
{
Vibrator vbtor = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
if (vbtor != null) vbtor.vibrate(new long[]{0, 200, 200, 200, 200, 200}, -1);
}
break;
case AudioManager.RINGER_MODE_NORMAL:
{
Uri uri = RingtoneManager.getDefaultUri(type);
final Ringtone rt = RingtoneManager.getRingtone(mContext.getApplicationContext(), uri);
rt.play();
//timeout
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(rt.isPlaying()){
rt.stop();
}
}
},1500);
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment