我试图实现钢琴应用程序,用户可以在显示器上显示的虚拟钢琴上进行游戏。由于低延迟我试图使用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(....)创建一个新的AudioTrack对象,然后写(...)并播放(....)
我认为它不能足够快地实例化,因此例外......
以下是代码:声音存储在不同异步任务类
中的散列映射中 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;
}
}
如果有人可以帮助解决这个问题,那将是很好的,可能是使用流模式而不是静态模式,其中声音被预处理或者空字节缓冲区被写入audiotrack并且在按钮点击时,散列图声音被流式传输到audiotrack。 / p>
答案 0 :(得分:1)
我认为你应该看看SoundPool并使用它。它似乎专门用于做你想做的事情。它不适用于动态生成的声音,但是因为你正在弹钢琴,我假设你将样本作为SDCard上的WAV文件。
使用播放速度可以改变音高:双倍速度播放= 1倍频程,半速度= 1倍频程。您可能希望稍后使用更精确的方法:)