好吧,当我将切换按钮按到 ON 时,我可以启动线程并且它工作正常,该线程是启动音频记录和音轨。但是当我关闭切换按钮时我无法停止线程......因为我不能做线程。停止......我想做的只是停止录音和音轨......如果有任何方法可以启动/停止它们/ o 制作线程也可以。另外,由于我无法停止线程,所以当我执行切换按钮 ON - OFF - ON 时,它会出错,因为无法启动线程 2 次。
问问题
380 次
4 回答
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;
}
};
于 2012-07-01T13:28:02.613 回答
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);
}
}
});
}
}
于 2012-07-01T13:31:32.997 回答
0
stop()
Thread的方法已被弃用(因为它本质上是不安全的),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;
}
}
});
于 2012-07-01T13:43:18.317 回答
0
只要存在,线程就isRecording
存在true
。因此,您可以根据需要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")
}
}
});
于 2012-07-01T14:16:30.217 回答