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)
您可以通过 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/ToneGenerator和 https://www.youtube.com/watch?v=HVu7K9W1_BM