Java 如何访问 Android 的默认提示音?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:09:41  来源:igfitidea点击:

How do I access Android's default beep sound?

javaandroidbeep

提问by user812892

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?

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

采纳答案by Mohammad Ersan

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();
    }
}

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 https://stackoverflow.com/a/9622040/737925

信用转到https://stackoverflow.com/a/9622040/737925

回答by ahcox

... use the default android beep sound (like when you adjust the ringer volume) ...

... 使用默认的 android 提示音(例如当您调整铃声音量时)...

On my Cyanogen 7 Nexus One and my old stock T-Mobile Pulse Mini (the latter from memory), as far as I can hear, this is is exactly the default beep sound on volume change:

在我的 Cyanogen 7 Nexus One 和我的旧库存 T-Mobile Pulse Mini(记忆中的后者)上,据我所知,这正是音量变化时的默认提示音:

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

You seem to be asking for an alternative to ToneGenerator, but I think it gives you exactly what you want in two lines.

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

Here are some other likely ToneGeneratorsounds I tried that were not a match (the first two might be useful as alternates to the volume beep):

以下是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);

回答by amine.b

the easy way is to use instance of ToneGenerator classe:

简单的方法是使用 ToneGenerator 类的实例:

    //declaration
    ToneGenerator toneG;
    //using any where`
    if(val>=taux_max)
    {
        taux_text.setTextColor(warnning_col);
        toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); //200 is duration in ms
    }