我想让一个按钮播放嘟嘟声,表示它已被按下。我想知道如何使用默认的安卓嘟嘟声(比如当你调整铃声音量时),而不是导入我自己的mp3音乐文件或使用ToneGenerator?
使用默认的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);
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
您可以通过ToneGenerator类访问Android的默认beeb声音。
import android.media.AudioManager;
import android.media.ToneGenerator;
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 200);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
关于它们的声音的更多信息:https://developer.android.com/reference/android/media/ToneGenerator和https://www.youtube.com/watch?v=HVu7K9W1_BM
简单的方法是使用ToneGenerator classe:的实例
//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
}