AudioTrack:玩()呼吁未初始化AudioTrack [英] AudioTrack: play() called on uninitialized AudioTrack
问题描述
我和AudioTrack类实验。基本上,我的应用程序具有当用户触摸屏幕上的特定对象以生成声音。我用这个例子作为的指引。结果
我的应用程序似乎工作,因为它应该,但通常触摸屏幕约一分钟后,它崩溃了:结果
I'm experimenting with AudioTrack class. Basically, my app has to generate a sound when the user touches a specific object on screen. I've used this example as a guide.
My app seems to work as it should but usually after touching the screen for about a minute it crashes:
07-02 20:40:53.459: E/AndroidRuntime(11973): FATAL EXCEPTION: Thread-10
07-02 20:40:53.459: E/AndroidRuntime(11973): java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
07-02 20:40:53.459: E/AndroidRuntime(11973): at android.media.AudioTrack.play(AudioTrack.java:824)
07-02 20:40:53.459: E/AndroidRuntime(11973): at com.mysounds_experimental.SoundThread.playSound(SoundThread.java:108)
07-02 20:40:53.459: E/AndroidRuntime(11973): at com.mysounds_experimental.SoundThread.run(SoundThread.java:69)
从产生的声音结果
Methods from class that generates sounds
public void initAudioTrack() {
int bufferSize = AudioTrack.getMinBufferSize(sampleRate
, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC
, sampleRate
, AudioFormat.CHANNEL_CONFIGURATION_MONO
, AudioFormat.ENCODING_PCM_16BIT
, bufferSize
, AudioTrack.MODE_STREAM);
}
private void playSound(){
audioTrack.write(generatedSnd, 0, numSamples);
audioTrack.play();
}
public void stopPlaying() {
audioTrack.flush();
audioTrack.stop();
audioTrack.release();
}
@Override
public void run() {
while (mRun) {
try{
Thread.sleep(200);
while(soundCycle){
if(freqOfTone != -1f) {
generateTone();
playSound();
Thread.sleep(200);
}
}
} catch(InterruptedException e){
// soundCycle = false;
// soundPool.stop(BEEP);
}
}
}
这是从自定义的方法查看我的线程是用结果
This is a method from a custom view my thread is used
@Override
public boolean onTouchEvent(final MotionEvent ev) {
int currentXPosition = (int) ev.getX();
int currentYPosition = (int) ev.getY();
if(ev.getX() < smBitmap.getWidth())
if(ev.getY() < smBitmap.getHeight()){
tempCol = smBitmap.getPixel(currentXPosition, currentYPosition);
}
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
sThread.freqOfTone = getFreqPreset(tempCol);
if(col != tempCol){
// sThread.initAudioTrack();
sThread.interrupt();
if(shouldInit) {
shouldInit = false;
sThread.initAudioTrack();
}
sThread.soundCycle = true;
col = tempCol;
invalidate();
}
break;
}
case MotionEvent.ACTION_MOVE: {
sThread.freqOfTone = getFreqPreset(tempCol);
if (tempCol == -1 || tempCol == 0) {
sThread.soundCycle = false;
shouldInit = true;
// sThread.stopPlaying();
sThread.interrupt();
invalidate();
} else {
if(col != tempCol){
sThread.interrupt();
col = tempCol;
invalidate();
}else {
sThread.soundCycle = true;
col = tempCol;
invalidate();
}
}
break;
}// case ACTION_MOVE
case MotionEvent.ACTION_UP: {
sThread.soundCycle = false;
shouldInit = true;
// sThread.stopPlaying();
sThread.interrupt();
col = -1;
mActivePointerId = INVALID_POINTER_ID;
break;
}// case ACTION_UP
}
return true;
}
任何想法,为什么会出现这种情况?
Any ideas why is this happening?
推荐答案
我认为你需要调用播放()调用写()之前。
I would think that you need to call play() before you call write().
但是,我也注意到,当你创建大量AudioTrack实例,即使你认为你清理了一切写,有时一玩()没有工作,和轨道是初始化。
But I've also noticed that when you create lots of AudioTrack instances even if you think you're cleaning everything up write, sometimes a play() fails to work, and the track is uninitialized.
您会想尝试...赶上这IllegalStateException异常,并避免调用write(),直到播放()的作品没有抛出异常。
You will want to try...catch this IllegalStateException, and avoiding calling write() until play() works without throwing an exception.
这篇关于AudioTrack:玩()呼吁未初始化AudioTrack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!