我制作了一个代码,允许我录制麦克风的音频。奇怪的是,当我点击我的按钮时,它似乎有某种延迟,按钮会在视觉上按下半秒钟然后启动它。这是有原因的吗?我怎么能解决这个问题?
我现在所拥有的:
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bRecord:
if (mStartRecording) {
mRecordButton.setText("Stop Recording");
onRecord(true);
mStartRecording = false;
} else {
mRecordButton.setText("Start Recording");
mStartRecording = true;
onRecord(false);
}
break;
}
修改 感谢您的帮助,我决定使用一项服务,但我正在努力一点点。我创建了:
public class RecordService extends IntentService {
private MediaRecorder mRecorder = null;
private String mFileName;
private static final String LOG_TAG = "RecordService";
public RecordService() {
super("RecordService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
}
和
case R.id.bRecord:
if (mStartRecording) {
mRecordButton.setText("Stop Recording");
//onRecord(true);
Intent service = new Intent(myContext, RecordService.class);
myContext.startService(service);
mStartRecording = false;
} else {
mRecordButton.setText("Start Recording");
mStartRecording = true;
onRecord(false);
}
break;
对此有疑问,我现在如何停止此服务(=停止录制)(再次点击该按钮)并将想法传达回活动?我试图在服务中添加一个停止函数,但是当我调用service.stop()来停止它时它似乎不起作用..
答案 0 :(得分:3)
我的猜测是,由于您在与UI相同的线程中开始录制,因此在startRecording()中的所有代码都已完成执行之前,UI不会响应。您可能需要考虑在自己的线程中调用onRecord。
答案 1 :(得分:1)
注意: AsyncTask
为您处理线程处理。如果你写一个Service
,你必须产生自己的线程,否则它将在UI线程上运行。
其中一种方法:
<强>服务强>
@Override
protected void onHandleIntent(Intent intent) {
//your recorder code
//get the messenger from intent
Bundle extras = intent.getExtras();
if (extras != null) {
Messenger messenger = (Messenger) extras.get("MESSENGER");
try {
//send a message back to the activity
messenger.send(...);
} catch (android.os.RemoteException e1) {
Log.w(getClass().getName(), "Exception sending message", e1);
}
}
}
<强>活动:强>
处理程序的定义
private Handler handler = new Handler() {
public void handleMessage(Message message) {
//do something with the message from the service
};
};
启动服务的代码
Intent intent = new Intent(this, YourService.class);
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
startService(intent);