如何访问Android的默认提示音? [英] How do I access Android's default beep sound?

查看:607
本文介绍了如何访问Android的默认提示音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个按钮,播放蜂鸣声,以表明它已经pssed $ P $。我想知道如何使用默认的Andr​​oid提示音(比如当您调整铃声音量),而不是导入自己的MP3音乐文件,或使用ToneGenerator?

解决方案

 公共无效playSound(上下文的背景下)抛出:IllegalArgumentException  - ,
                                              SecurityException异常,
                                              IllegalStateException异常,
                                              IOException异常{

    乌里soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    MediaPlayer的mMediaPlayer =新的MediaPlayer();
    mMediaPlayer.setDataSource(背景下,soundUri);
    最后AudioManager audioManager =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    如果(audioManager.getStreamVolume(AudioManager.STREAM_ALARM)!= 0){
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mMediaPlayer.setLooping(真正的);
        mMediaPlayer prepare()。
        mMediaPlayer.start();
    }
}
 

我发现了另一个答案:

 尝试{
    URI通知= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    铃声R = RingtoneManager.getRingtone(getApplicationContext(),通知);
    r.play();
}赶上(例外五){
    e.printStackTrace();
}
 

归功于 http://stackoverflow.com/a/9622040/737925

I would like to make a button play a beep sound to indicate it has been pressed. I want to know how to use the default android beep sound (like when you adjust the ringer volume), instead of importing my own mp3 music file or using ToneGenerator?

解决方案

public void playSound(Context context) throws IllegalArgumentException, 
                                              SecurityException, 
                                              IllegalStateException,
                                              IOException {

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    MediaPlayer mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(context, soundUri);
    final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }
}

I found another answer:

try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}

credit goes to http://stackoverflow.com/a/9622040/737925

这篇关于如何访问Android的默认提示音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆