如何访问安卓的默认蜂鸣声?

2022-08-31 17:22:57

我想让按钮播放哔哔声,以指示它已被按下。我想知道如何使用默认的Android哔哔声(就像当你调整铃声音量时),而不是导入我自己的mp3音乐文件或使用ToneGenerator?


答案 1

...使用默认的Android哔哔声(就像当你调整铃声音量时)...

在我的Cyanogen 7 Nexus One和我的旧库存T-Mobile Pulse Mini(后者来自内存)上,据我所知,这正是音量变化时的默认蜂鸣声:

     final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
     tg.startTone(ToneGenerator.TONE_PROP_BEEP);

您似乎在要求 替代 ,但我认为它为您提供了两行您想要的东西。ToneGenerator

以下是我尝试过的其他一些可能的声音,它们不是匹配的(前两个可能有用,作为音量哔哔声的替代):ToneGenerator

     // Double beeps:     tg.startTone(ToneGenerator.TONE_PROP_ACK);
     // Double beeps:     tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
     // Sounds all wrong: tg.startTone(ToneGenerator.TONE_CDMA_KEYPAD_VOLUME_KEY_LITE);

答案 2
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


推荐