Android4.0.3中的提示音
一、ToneGenerator的使用
参照com.android.contacts.dialpad.DialpadFragment
ToneGenerator只能播放在ToneGenerator中定义好的TONE_TYPE。
1、常量申明
[java] /** Tone音的长度,单位:milliseconds */
private static final int TONE_LENGTH_MS = 150;
/** 主音量的比例:以80%的主音量播放Tone音 */
private static final int TONE_RELATIVE_VOLUME = 80;
/** 主音量的音频种别 */
private static final int DIAL_TONE_STREAM_TYPE =AudioManager.STREAM_MUSIC;
/** Tone音的长度,单位:milliseconds */
private static final int TONE_LENGTH_MS = 150;
/** 主音量的比例:以80%的主音量播放Tone音 */
private static final int TONE_RELATIVE_VOLUME = 80;
/** 主音量的音频种别 */
private static final int DIAL_TONE_STREAM_TYPE =AudioManager.STREAM_MUSIC;
2、变量申明
[java] // Tone音播放器
private ToneGenerator mToneGenerator;
// Tone相关的同步锁
private Object mToneGeneratorLock = new Object();
// 设定中的Tone音播放设置
private boolean mDTMFToneEnabled;
// Tone音播放器
private ToneGenerator mToneGenerator;
// Tone相关的同步锁
private Object mToneGeneratorLock = new Object();
// 设定中的Tone音播放设置
private boolean mDTMFToneEnabled;
3、Tone的初始化
[java] public void onResume() {
super.onResume();
// 读取设定的值
mDTMFToneEnabled =Settings.System.getInt(getActivity().getContentResolver(),
Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
// 失败了也无所谓,不是啥重要的东西
synchronized (mToneGeneratorLock) {
if (mToneGenerator == null) {
try {
// we want the user to be ableto control the volume of the dial tones
// outside of a call, so we usethe stream type that is also mapped to the
// volume control keys for thisactivity
mToneGenerator = newToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
getActivity().setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
} catch (RuntimeException e) {
Log.w(TAG, "Exceptioncaught while creating local tone generator: " + e);
mToneGenerator = null;
}
}
}
}
public void onResume() {
super.onResume();
// 读取设定的值
mDTMFToneEnabled =Settings.System.getInt(getActivity().getContentResolver(),
Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
// 失败了也无所谓,不是啥重要的东西
synchronized (mToneGeneratorLock) {
if (mToneGenerator == null) {
try {
// we want the user to be ableto control the volume of the dial tones
// outside of a call, so we usethe stream type that is also mapped to the
// volume control keys for thisactivity
mToneGenerator = newToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
getActivity().setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
} catch (RuntimeException e) {
Log.w(TAG, "Exceptioncaught while creating local tone generator: " + e);
mToneGenerator = null;
}
}
}
}
4、释放Tone资源
[java] public voidonPause() {
super.onPause();
synchronized (mToneGeneratorLock) {
if (mToneGenerator != null) {
mToneGenerator.release();
mToneGenerator = null;
}
}
}
public voidonPause() {
super.onPause();
synchronized (mToneGeneratorLock) {
if (mToneGenerator != null) {
mToneGenerator.release();
mToneGenerator = null;
}
}
}
5、播放Tone音
[java] /**
* 播放TONE_LENGTH_MS milliseconds的Tone音.
* 只有在设定中选择了播放Tone,并且不是静音模式才会播放Tone音。
* @param tone a tone code from {@linkToneGenerator}
*/
void playTone(int tone) {
// 设定中没有选中的话,就不播
if (!mDTMFToneEnabled) {
return;
}
// 静音模式的时候也不播,需要每次都检查,因为没有Activity切换也能设成静音模式
// 设定中的那个就不需要,因为要设定必须要先切入设定Activity才行
AudioManager audioManager =
(AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
int ringerMode =audioManager.getRingerMode();
if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
|| (ringerMode ==AudioManager.RINGER_MODE_VIBRATE)) {
return;&nbs
补充:移动开发 , Android ,