gpt4 book ai didi

android - 录音时的线程问题

转载 作者:行者123 更新时间:2023-11-29 18:22:06 25 4
gpt4 key购买 nike

我正在尝试使用 AudioRecord 录制音频样本。我使用了一个线程来执行记录方法。线程似乎有问题。有什么想法吗?

package com.tecmark;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class recorder extends Activity {




private Thread thread;
private boolean isRecording;
private AudioRecord recorder;
private FileOutputStream os;
private BufferedOutputStream bos;
private DataOutputStream dos;
private TextView text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



}



public void onClickPlay(View v){


}


public void record(){


Log.i("inside record method", "******");

text = (TextView)findViewById(R.id.TextView01);
text.setText("recording");

int audioSource = MediaRecorder.AudioSource.MIC;
int sampleRate = 22050;
int channel = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int encoding = AudioFormat.ENCODING_PCM_16BIT;
int result = 0;



File path = Environment.getExternalStorageDirectory();
Log.v("file path", ""+path.getAbsolutePath());

File file = new File(path, "test.wav");

if(file.exists()){
file.delete();
}

path.mkdirs();
Log.v("file path", ""+file.getAbsolutePath());

recorder = new AudioRecord(audioSource, sampleRate,channel,encoding,
AudioRecord.getMinBufferSize(sampleRate, channel,encoding));
Log.i("recorder", "recorder object created");

try {
os = new FileOutputStream(file);
bos = new BufferedOutputStream(os);
dos = new DataOutputStream(bos);
} catch (Exception e1) {

e1.printStackTrace();
}


int bufferSize = AudioRecord.getMinBufferSize(sampleRate,channel,encoding);
byte[] buffer = new byte[bufferSize];

recorder.startRecording();
isRecording = true;

try{

while (isRecording){



result = recorder.read(buffer, 0, bufferSize);
for(int a=0; a<result;a++){
dos.write(buffer[a]);

if(!isRecording){
recorder.stop();

break;
}

}

}
dos.flush();
dos.close();

}catch(Exception e) {

e.printStackTrace();
}

}// end of record method




public void onClickStop(View v){
Log.v("onClickStop", "stop clicked");

isRecording=false;


}



public void onClickReverse(View v){
Log.v("onClickReverse", "reverse clicked");


}

public void onClickRecord(View v){

thread = new Thread(new Runnable() {


public void run() {
isRecording = true;
record();
}
});

thread.start();

isRecording = false;


}

}//end of class

这是错误。

01-30 12:27:50.434: ERROR/AndroidRuntime(2226): Uncaught handler: thread Thread-8 exiting due to uncaught exception
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.view.ViewRoot.checkThread(ViewRoot.java:2706)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.view.ViewRoot.invalidateChild(ViewRoot.java:573)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:599)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2396)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.view.View.invalidate(View.java:4945)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.widget.TextView.checkForRelayout(TextView.java:5366)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.widget.TextView.setText(TextView.java:2684)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.widget.TextView.setText(TextView.java:2552)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at android.widget.TextView.setText(TextView.java:2527)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at com.tecmark.recorder.record(recorder.java:54)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at com.tecmark.recorder$1.run(recorder.java:151)
01-30 12:27:50.459: ERROR/AndroidRuntime(2226): at java.lang.Thread.run(Thread.java:1096)

最佳答案

您正在 record() 中调用 findViewById(),但 record() 并未在您调用时在 UI 线程中运行在新线程的 run() 方法中。正如错误所说:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

record() 方法中删除对 findViewById()text.setText() 的调用。相反,请尝试在创建新线程之前在 onClickRecord() 中设置您的 UI 元素。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.TextView01);
}
...
public void onClickRecord(View v){
text.setText("recording");
thread = new Thread(new Runnable() {
public void run() {
isRecording = true;
record();
}
});
thread.start();
isRecording = false;
}

关于android - 录音时的线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4843092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com