如何访问Android的默认哔声?

use*_*892 54 java android beep

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

ahc*_*cox 78

...使用默认的android哔声(就像调整铃声音量时一样)......

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

     final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
     tg.startTone(ToneGenerator.TONE_PROP_BEEP);
Run Code Online (Sandbox Code Playgroud)

你似乎在寻求替代方案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);
Run Code Online (Sandbox Code Playgroud)


Moh*_*san 76

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到了另一个答案:

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

信用转到/sf/answers/673542831/

  • 我使用了那段代码,并对声音重演感到恼火.现在我看到了问题:`mMediaPlayer.setLooping(true);`你为什么要声音循环? (7认同)
  • @SimonAndréForsberg取决于开发者的愿望 (4认同)
  • 对我来说,`RingtoneManager.TYPE_NOTIFICATION`正在播放长长的和弦"新短信到达"音.我没有看到一个选项会让OP的标准音量发生变化. (4认同)
  • mMediaPlayer超出范围并且可以在声音播放完毕之前进行垃圾收集,或者声音已经传递给较低级别​​的组件以确保它完成播放由`mMediaPlayer.start()完成是否重要?呼叫? (2认同)
  • 请注意,此代码正在检查警报流的声级和播放.对于海报的情况,您需要为`AudioManager.STREAM_NOTIFICATION`交换`AudioManager.STREAM_AlARM`实例.有关其他选项,请参阅`AudioManager`文档,但通知似乎是正确的选择. (2认同)

Yig*_*lan 5

您可以通过 ToneGenerator 类访问 Android 的默认蜂鸣声。

import android.media.AudioManager;
import android.media.ToneGenerator;
Run Code Online (Sandbox Code Playgroud)
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 200);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
Run Code Online (Sandbox Code Playgroud)

有关其声音的更多信息:https://developer.android.com/reference/android/media/ToneGeneratorhttps://www.youtube.com/watch?v=HVu7K9W1_BM