我不知道如何在Java
中停止Thread
。
我有一个创建并启动Thread
的function
。现在,当我启动程序时,我想让它启动Thread
。在此之后,如果按下button
,则我希望Thread
停止,当再次按下button
时,我希望Thread
重新开始,或重新开始(无关紧要)。
我创建Thread
的function
private void startRecordingUserInput() {
AudioDispatcher dispatcher =
AudioDispatcherFactory.fromDefaultMicrophone(44100, 2048, 0);
PitchDetectionHandler pdh = new PitchDetectionHandler() {
@Override
public void handlePitch(PitchDetectionResult res, AudioEvent e) {
final float pitchInHz = res.getPitch();
runOnUiThread(new Runnable() {
@Override
public void run() {
pitchText.setText("" + pitchInHz);
gameView.setSingerArrowPitch(pitchInHz);
gameView.setCurrentTime(mid);
gameView.invalidate();
}
});
}
};
AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 44100, 2048, pdh);
dispatcher.addAudioProcessor(pitchProcessor);
Thread audioThread = new Thread(dispatcher, "Audio Thread");
audioThread.start();
}
以及我启动它并想要对Thread
执行start/stop
的部分。
//Start the Thread
startRecordingUserInput();
playRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isPlaying){
//Start the Thread again if it is not running or
//make a new one
isPlaying = true;
} else if(isPlaying){
//Stop the Thread
isPlaying = false;
}
}
});
https://stackoverflow.com/questions/50804364
复制相似问题