如何在oncheckedchanged中停止一个线程?

时间:2012-07-01 13:21:34

标签: android

当我将togglebutton按到ON并且它工作正常时,我可以启动该线程,该线程是启动audiorecord和audiotrack。 但是当我把togglebutton关闭时我无法阻止线程...因为我不能做thread.stop ...我想做的只是停止audiorecord和audiotrack ...如果有任何方法可以启动/停止它们w / o制作线程也没关系。 另外,由于我无法停止线程,当我执行togglebutton ON - OFF - ON时,它会出错,因为无法启动线程2次。

4 个答案:

答案 0 :(得分:0)

也许你可以使用一个变量并在函数运行中检查其状态,如:

   tbs = (ToggleButton) findViewById(R.id.tbS);
   boolean stopThread = false;
    tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked) {

            thread.start();
            } else {
          stopThread = true;
        }

    }
});
      Thread thread = new Thread() {
public void run() {
    isRecording = true;
    android.os.Process
            .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    int buffersize = AudioRecord.getMinBufferSize(8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, buffersize);
    int bufferzize = AudioTrack.getMinBufferSize(11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferzize,
            AudioTrack.MODE_STREAM);
    atrack.setPlaybackRate(11025);
    byte[] buffer = new byte[buffersize];
    if (arec.getState() == AudioRecord.STATE_INITIALIZED)
        arec.startRecording();
    if (atrack.getState() == AudioTrack.STATE_INITIALIZED)
        atrack.play();
    while (isRecording) {
        arec.read(buffer, 0, buffersize);
        atrack.write(buffer, 0, buffer.length);
    }

    if(stopThread)
        return;
}

};

答案 1 :(得分:0)

在这种情况下我会使用处理程序。

他们有两个功能

Handler audioHandler = new Handler();

audioHandler.post(startAudio);   // Starts the operations

audioHandler.removeCallbacks(startAudio); //Stops the Operation

以下代码段将为您提供帮助。

package org.sample;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class SampleActivity extends Activity {

    private Handler audioHandler;
    private Runnable startAudio;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        audioHandler = new Handler();

        startAudio = new Runnable() {

            @Override
            public void run() {
                isRecording = true;
                android.os.Process
                        .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
                int buffersize = AudioRecord.getMinBufferSize(8000,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
                arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, buffersize);
                int bufferzize = AudioTrack.getMinBufferSize(11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
                atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, bufferzize,
                        AudioTrack.MODE_STREAM);
                atrack.setPlaybackRate(11025);
                byte[] buffer = new byte[buffersize];
                if (arec.getState() == AudioRecord.STATE_INITIALIZED)
                    arec.startRecording();
                if (atrack.getState() == AudioTrack.STATE_INITIALIZED)
                    atrack.play();
                while (isRecording) {
                    arec.read(buffer, 0, buffersize);
                    atrack.write(buffer, 0, buffer.length);
                }

                audioHandler.post(startAudio);

            }
        };

        tbs = (ToggleButton) findViewById(R.id.tbS);
        tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    audioHandler.post(startAudio);
                } else {
                    audioHandler.removeCallbacks(startAudio);
                }

            }
        });

    }
}

答案 2 :(得分:0)

Thread的stop()方法已经不推荐使用(因为它本身就不安全),java提供了一种停止Thread的替代方法。这是一个非常简单的方法,如官方链接所述:

http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

您是否尝试将isRecording变量设为全局变量然后更改其状态:

 isRecording = false;

 Thread thread;

 Runnable r = new Runnable() {
public void run() {
    isRecording = true;
    android.os.Process
            .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    int buffersize = AudioRecord.getMinBufferSize(8000,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, buffersize);
    int bufferzize = AudioTrack.getMinBufferSize(11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
            AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferzize,
            AudioTrack.MODE_STREAM);
    atrack.setPlaybackRate(11025);
    byte[] buffer = new byte[buffersize];
    if (arec.getState() == AudioRecord.STATE_INITIALIZED)
        arec.startRecording();
    if (atrack.getState() == AudioTrack.STATE_INITIALIZED)
        atrack.play();
    while (isRecording) {
        arec.read(buffer, 0, buffersize);
        atrack.write(buffer, 0, buffer.length);
    }
}

};

tbs = (ToggleButton) findViewById(R.id.tbS);
tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked) {
            thread = new Thread(r)
            thread.start();
            } else {
          isRecording = false;
          thread = null;
        }

    }
});

答案 3 :(得分:0)

只要isRecordingtrue,线程就会处于活动状态。因此,您可以根据需要使用final StringBuilder来操纵isRecording的值。

  final StringBuilder isRecording=new StringBuilder("t");
Thread thread = new Thread() {
            public void run() {

                android.os.Process
                        .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
                int buffersize = AudioRecord.getMinBufferSize(8000,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
                arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, buffersize);
                int bufferzize = AudioTrack.getMinBufferSize(11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);
                atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, bufferzize,
                        AudioTrack.MODE_STREAM);
                atrack.setPlaybackRate(11025);
                byte[] buffer = new byte[buffersize];
                if (arec.getState() == AudioRecord.STATE_INITIALIZED)
                    arec.startRecording();
                if (atrack.getState() == AudioTrack.STATE_INITIALIZED)
                    atrack.play();
                while (isRecording.toString().equals("t")) {
                    arec.read(buffer, 0, buffersize);
                    atrack.write(buffer, 0, buffer.length);
                }
            }

        };
    tbs = (ToggleButton) findViewById(R.id.tbS);
        tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    isRecording.replace(0,0,"t");
                    thread.start();
                    } else {
                isRecording.replace(0,0,"f")
                }

            }
        });