我在我的应用程序的主活动中添加了一些代码,以连续生成和播放正弦波,并创建了一个PresetReverb对象来测试它对正弦波的效果。但是,每次我启动应用程序时,应用程序都会崩溃,并在logcat中生成以下内容:
08-08 14:36:10.566: E/AudioEffect(19466): set(): AudioFlinger could not create effect, status: -1
08-08 14:36:10.566: E/AudioEffects-JNI(19466): AudioEffect initCheck failed -1
08-08 14:36:10.566: E/AudioEffect-JAVA(19466): Error code -1 when initializing AudioEffect.
08-08 14:36:10.566: W/dalvikvm(19466): threadid=11: thread exiting with uncaught exception (group=0x40ac2228)
08-08 14:36:10.576: E/AndroidRuntime(19466): FATAL EXCEPTION: Thread-11436
08-08 14:36:10.576: E/AndroidRuntime(19466): java.lang.RuntimeException: Cannot initialize effect engine for type: 47382d60-ddd8-11db-bf3a-0002a5d5c51bError: -1
08-08 14:36:10.576: E/AndroidRuntime(19466): at android.media.audiofx.AudioEffect.<init>(AudioEffect.java:387)
08-08 14:36:10.576: E/AndroidRuntime(19466): at android.media.audiofx.PresetReverb.<init>(PresetReverb.java:136)
08-08 14:36:10.576: E/AndroidRuntime(19466): at me.kevinossia.mystuff.MainScreen$2.run(MainScreen.java:47)
以下是该活动的代码:
package me.kevinossia.mystuff;
import me.kevinossia.mystuff.tutorial.R;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.audiofx.PresetReverb;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainScreen extends Activity
{
Thread t;
int sr = 44100;
boolean isRunning=true;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
Button goBack = (Button) findViewById(R.id.back);
goBack.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent goBack = new Intent(getApplicationContext(), HomeScreen.class);
startActivity(goBack);
}
});
t = new Thread()
{
public void run()
{
setPriority(Thread.MAX_PRIORITY);
int buffsize = AudioTrack.getMinBufferSize(sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffsize, AudioTrack.MODE_STREAM);
PresetReverb reverb = new PresetReverb(0,0);
audioTrack.attachAuxEffect(reverb.getId());
reverb.setPreset(PresetReverb.PRESET_LARGEROOM);
reverb.setEnabled(true);
audioTrack.setAuxEffectSendLevel(1.0f);
short samples[] = new short[buffsize];
int amp = 32767;
double twopi = 2*Math.PI;
double fr = 262.f;
double ph = 0.0;
audioTrack.play();
while(isRunning)
{
fr = 262;
for(int i=0; i < buffsize; i++)
{
samples[i] = (short) (amp*Math.sin(ph));
ph += twopi*fr/sr;
}
audioTrack.write(samples, 0, buffsize);
}
audioTrack.stop();
audioTrack.release();
}
};
t.start();
}
@Override
public void onPause()
{
super.onPause();
isRunning = false;
try
{
t.join();
} catch (InterruptedException e)
{
e.printStackTrace();
}
t = null;
}
}
显然在创建PresetReverb对象的代码行存在问题……但我无法弄清楚原因。究竟是什么导致了应用程序崩溃?