0
我在調用新線程時遇到了一些問題。之前執行的線程
我正在製作一個錄音應用程序,並且我在不同的線程中調用了錄製/回放。
有一個按鈕可以開始錄製。我試圖通過一個handler.post對象和方法用新文本更新按鈕。
問題是需要花費很長時間來更新。文本不會更新,直到線程運行+5秒後纔會更新。
有人可以幫我嗎?請?
package com.EJH.Industries.microkr;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaSyncEvent;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
//CLASS VARIABLES
//CHAR SEQUENCE
CharSequence easyChar = "PLAYING";
public Handler textViewHandler = new Handler();
//CREATE THE RECORDING OBJECT
int audioSrc = MediaRecorder.AudioSource.MIC;
int sampleRate = 44100;
int chanConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int getMinBuffSize = 200*AudioRecord.getMinBufferSize(sampleRate, chanConfig, audioFormat);
int minBuffSize = (int) getMinBuffSize;
short audioBuff[] = new short[minBuffSize];
public AudioRecord micRecorder = new AudioRecord(audioSrc, 22050, chanConfig, audioFormat, minBuffSize);
//CREATE THE PLAYBACK OBJECT
int streamType = AudioManager.STREAM_MUSIC;
int playMode = AudioTrack.MODE_STREAM;
int playChanConfig = AudioFormat. CHANNEL_OUT_MONO;
public AudioTrack speakerPlay = new AudioTrack(streamType, sampleRate, playChanConfig, audioFormat, 8192, playMode);
public void startRec(){
micRecorder.startRecording();
micRecorder.read(audioBuff, 0, minBuffSize);
micRecorder.stop();
micRecorder.release();
}
public void startPlayback(){
speakerPlay.play();
speakerPlay.write(audioBuff, 0, minBuffSize);
speakerPlay.stop();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startBtn = (Button) findViewById(R.id.startButton);
startBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Thread recThread = new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
textViewHandler.post(new Runnable() {
public void run(){
startBtn.setText("Recording!");
}
});
startRec();
}
});
// RUN RECORDING FUNCTION
recThread.run();
try {
recThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread playThread = new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
startPlayback();
}
});
playThread.run();
try {
playThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
///////END onCreate//////////
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
謝謝你的迴應。我想startRec()在一個單獨的線程上。 doessnt移動它把它放在UI線程而不是它自己的? – user1493588
我移動了一些功能,並使其工作,我認爲這是線程。謝謝! – user1493588