用于播放钢琴声音的Android AudioTrack类



im正在尝试实现一个钢琴应用程序,用户可以在显示器上显示的虚拟钢琴上演奏。由于低延迟,我尝试使用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。每次我喜欢播放声音时,我都必须用新的AudioTrack(…)创建一个新的AudioPrack对象,然后编写(…)并播放(…)

我认为它无法足够快地实例化,因此异常。。。

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

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

如果有人能帮助解决这个问题,那就太好了,也许用流模式而不是静态模式,在静态模式下,声音被预处理,或者一个空字节缓冲区被写入音轨,然后点击按钮,散列图声音被流式传输到音轨。

我认为您应该看看SoundPool并使用它。它似乎是专门为做你想做的事情而设计的。它不适用于动态生成的声音,但由于你在弹钢琴,我认为你在SDCard上已经有了WAV文件的样本。

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

相关内容

  • 没有找到相关文章

最新更新