Android,我不知道如何停止这个声音循环

时间:2013-11-04 09:32:03

标签: android audio

正如标题所示,我对声音循环有问题,因为我不知道如何打破循环。当循环运行时,我无法控制设备的音量。

public void onClick(View view) {
            // TODO Auto-generated method stub
                boolean on = ((ToggleButton) view).isChecked();
                if (on){
                     boolean is = true;
                     android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
                     int buffersize = AudioRecord.getMinBufferSize(20000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
                     AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 20000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
                     AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 20000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
                     atrack.setPlaybackRate(20000);
                     byte[] buffer = new byte[buffersize];
                     arec.startRecording();
                     atrack.play();
                     while(is){
                         arec.read(buffer, 0, buffersize);
                         atrack.write(buffer, 0, buffer.length);
                     }
                     arec.stop();
                        atrack.stop();
                        is = false;
                           }
                           }});

        }

3 个答案:

答案 0 :(得分:0)

您可以使用AudioTrack.stop()AudioTrack.pause()

答案 1 :(得分:0)

嗯......你首先要设置布尔值“is = true”。然后你有一个while循环“while(is)”这是一个无限循环,因为“is”将永远为真,并且永远不会在你设置它为“false”的行。你想在while循环中做什么?

答案 2 :(得分:0)

使用arec.release();和atrack.release()代替arec.stop();atrack.stop 这可能会对你有所帮助,祝你好运...... :)