我想让一个按钮播放提示音,以表明它已被按下。我想知道如何使用默认的Android蜂鸣声(例如调整铃声音量时),而不是导入自己的mp3音乐文件或使用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);
        // Uncomment the following line if you aim to play it repeatedly
        // mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }
}

我找到了另一个答案:
try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}

功劳归给https://stackoverflow.com/a/9622040/737925

关于java - 如何访问Android的默认蜂鸣声?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6462105/

10-10 13:36