Android应用程序不断崩溃audioEffect错误代码-1

时间:2013-08-08 22:04:41

标签: android crash audioeffect

我在我的应用程序的主要活动中添加了一些代码用于连续生成和回放正弦波,并创建了一个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对象的行存在问题...但我无法弄清楚原因。究竟是什么导致我的应用程序崩溃?

1 个答案:

答案 0 :(得分:14)

尝试添加

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

到清单文件中的权限列表。