我想让一个按钮发出嘟嘟声,表示它已被按下。我想知道如何使用默认的安卓嘟嘟声(就像当你调整铃声音量时),而不是导入我自己的mp3音乐文件或使用ToneGenerator?
发布于 2011-07-14 15:51:21
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();
}
发布于 2020-07-02 23:44:02
你可以通过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
发布于 2013-02-22 08:33:11
最简单的方法是使用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
}
https://stackoverflow.com/questions/6462105
复制相似问题