7

I put some code in for continuous generation and playback of a sine wave inside my main activity for my app and created a PresetReverb object to test it out on the sine wave. But, every time I launch it, the app crashes, and it generates this in the 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)

Here is the code for the activity:

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;
}


}

Apparently there is a problem with the line that creates the PresetReverb object...but I can't figure out why. What exactly is causing my app to crash?

4

1 回答 1

14

尝试添加

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

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

于 2013-08-09T02:20:20.893 回答