0

我试图实现一个钢琴应用程序,用户可以在显示器上显示的虚拟钢琴上演奏。由于低延迟,我尝试使用 AudioTrack 类,这会带来很多麻烦。我的问题是快速播放声音。目前我使用按钮来测试它。如果我快速按下按钮,我会得到这个例外:

09-11 15:06:19.089: E/AudioTrack(3729): AudioFlinger could not create track, status: -12
09-11 15:06:19.097: E/AudioTrack-JNI(3729): Error initializing AudioTrack
09-11 15:06:19.097: E/AudioTrack-Java(3729): [ android.media.AudioTrack ] Error code -20 when initializing AudioTrack.
09-11 15:06:19.097: D/AndroidRuntime(3729): Shutting down VM
09-11 15:06:19.097: W/dalvikvm(3729): threadid=1: thread exiting with uncaught exception (group=0x4001d888)
09-11 15:06:19.097: E/AndroidRuntime(3729): FATAL EXCEPTION: main
09-11 15:06:19.097: E/AndroidRuntime(3729): java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.media.AudioTrack.play(AudioTrack.java:764)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at com.example.audiotrackvssoundpoollayer.MainActivity.onClick(MainActivity.java:216)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.view.View.performClick(View.java:2461)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.view.View$PerformClick.run(View.java:8890)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.os.Handler.handleCallback(Handler.java:587)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.os.Looper.loop(Looper.java:123)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at android.app.ActivityThread.main(ActivityThread.java:4632)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at java.lang.reflect.Method.invoke(Method.java:521)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-11 15:06:19.097: E/AndroidRuntime(3729):     at dalvik.system.NativeStart.main(Native Method)

我在静态模式下使用 AudioTrack。每次我喜欢播放声音时,我都必须使用 new AudioTrack(....) 创建一个新的 AudioTrack 对象,然后 write(...) 和 play(....)

我认为它不能足够快地实例化,因此例外......

这是代码:声音存储在不同异步任务类的哈希图中

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button buttonA1 = (Button) findViewById(R.id.buttonA1);
            buttonA1.setOnClickListener(this);
            Button buttonA2 = (Button) findViewById(R.id.buttonA2);
            buttonA2.setOnClickListener(this);
            Button buttonA3 = (Button) findViewById(R.id.buttonA3);
            buttonA3.setOnClickListener(this);
            Button buttonA4 = (Button) findViewById(R.id.buttonA4);
            buttonA4.setOnClickListener(this);
            Button buttonA5 = (Button) findViewById(R.id.buttonA5);
            buttonA5.setOnClickListener(this);
            Button buttonA6 = (Button) findViewById(R.id.buttonA6);
            buttonA6.setOnClickListener(this);
            Button buttonA7 = (Button) findViewById(R.id.buttonA7);
            buttonA7.setOnClickListener(this);
            Button buttonA8 = (Button) findViewById(R.id.buttonA8);
            buttonA8.setOnClickListener(this);

            //***AudioTrack***
            AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
            float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            volume = actualVolume / maxVolume;
            minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                            AudioFormat.ENCODING_PCM_16BIT);

            assetManager = getResources().getAssets();



        }

public void onClick(View button) {


        switch(button.getId()) {

        case R.id.buttonA1: 
            audioTrack1 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack1.write(soundMap.get(3), 0, minBufferSize * 65);
            audioTrack1.play();
            break;
       case R.id.buttonA2:
            audioTrack2 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack2.write(soundMap.get(3), 0, minBufferSize * 65);
            audioTrack2.play();
            break;
        case R.id.buttonA3:
            audioTrack3 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack3.write(soundMap.get(3), 0, minBufferSize * 65);
            audioTrack3.play();
            break;      
        case R.id.buttonA4:
            audioTrack4 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack4.write(soundMap.get(3), 0, minBufferSize * 65);
            audioTrack4.play();
            break;
        case R.id.buttonA5:
            audioTrack5 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack5.write(soundMap.get(4), 0, minBufferSize * 65);
            audioTrack5.play();
            break;
        case R.id.buttonA6:
            audioTrack6 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack6.write(soundMap.get(5), 0, minBufferSize * 65);
            audioTrack6.play();
            break;
        case R.id.buttonA7:
            audioTrack7 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack7.write(soundMap.get(6), 0, minBufferSize * 65);
            audioTrack7.play();
            break;
        case R.id.buttonA8:
            audioTrack8 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                     AudioFormat.ENCODING_PCM_16BIT, minBufferSize * 65, AudioTrack.MODE_STATIC);
            audioTrack8.write(soundMap.get(7), 0, minBufferSize * 65);
            audioTrack8.play();
            break;
    }
}

如果有人可以帮助解决这个问题,那就太好了,也许使用流模式而不是静态模式,其中声音被预处理或空字节缓冲区被写入音轨,然后单击按钮,哈希图声音被流式传输到音轨。

4

1 回答 1

1

我认为你应该看看 SoundPool 并使用它。它似乎是专门为做你想做的事情而设计的。它不适用于动态生成的声音,但由于您正在弹奏钢​​琴,因此我假设您将样本作为 WAV 文件保存在 SDCard 上。

使用播放速度,您可以更改音高:以双倍速度播放 = 1 个八度音程,半速 = 1 个八度音程。您可能希望稍后使用更精细的方法:)

于 2012-10-06T20:02:48.070 回答