我有一个要求,我需要显示一个类似屏幕的拨号盘,并且每当用户按下拨号盘按钮时,我都必须生成 1khz 音调(不是 DTMF 音调)。
我已使用以下链接中的代码生成 1 khz 音调:
当我开始在拨号盘屏幕上拨号按钮直到按下 21 时,它成功生成了音调,但在第 22 次尝试后,我收到应用程序无响应 (ANR) 错误,我需要关闭应用程序。
下面是我的代码:
final float duration = 0.3f; // seconds
final int sampleRate = 4000;
final int numSamples = (int)duration * sampleRate + 100;
final double sample[] = new double[numSamples];
final double freqOfTone = 1000; // hz
final byte generatedSnd[] = new byte[2 * numSamples];
final Handler handler = new Handler();
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
playSound();
}
private void playSound()
{
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone();
handler.post(new Runnable() {
public void run() {
playSound1();
}
});
}
});
thread.start();
}
void playSound1(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
void genTone(){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
每次按下拨号盘上的按钮时都会调用 onClick() 方法。
使用上面的代码,我在 Logcat 中得到了这个输出:
05.500 E/AudioFlinger( 85): no more track names available
09-21 05:20:05.500 E/AudioTrack( 133): AudioFlinger could not create track, status: -12
09-21 05:20:05.503 E/SoundPool( 133): Error creating AudioTrack
09-21 05:20:05.535 E/AudioFlinger( 85): no more track names available
09-21 05:20:05.535 E/AudioTrack( 6080): AudioFlinger could not create track, status: -12
09-21 05:20:05.535 E/AudioTrack-JNI( 6080): Error initializing AudioTrack
09-21 05:20:05.535 E/AudioTrack-Java( 6080): [ android.media.AudioTrack ] Error code -20 when initializing AudioTrack.
09-21 05:20:05.535 D/AndroidRuntime( 6080): Shutting down VM
09-21 05:20:05.535 W/dalvikvm( 6080): threadid=1: thread exiting with uncaught exception (group=0x40015578)
09-21 05:20:05.539 E/AndroidRuntime( 6080): FATAL EXCEPTION: main
09-21 05:20:05.539 E/AndroidRuntime( 6080): java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
09-21 05:20:05.539 E/AndroidRuntime( 6080): at android.media.AudioTrack.play(AudioTrack.java:824)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at com.android.dial.DialPadScreen.playSound1(DialPadScreen.java:274)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at com.android.dial.DialPadScreen$1$1.run(DialPadScreen.java:248)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at android.os.Handler.handleCallback(Handler.java:587)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at android.os.Looper.loop(Looper.java:123)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at java.lang.reflect.Method.invoke(Method.java:507)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-21 05:20:05.539 E/AndroidRuntime( 6080): at dalvik.system.NativeStart.main(Native Method)
09-21 05:20:05.542 E/ ( 133): Dumpstate > /data/log/dumpstate_app_error
09-21 05:20:05.542 W/ActivityManager( 133): Force finishing activity com.android.dial/.DialPadScreen
我正在打印“audioTrack”的状态,直到第 21 次我得到值 1(STATE_INITIALIZED),之后我得到值“0”(STATE_UNINITIALIZED)。不知道为什么状态正在改变。
请帮我做些什么来防止我的应用程序中出现这种关闭问题。或请建议是否有任何替代方法可以做到这一点。