0

我制作了一个代码,允许我从麦克风录制音频。奇怪的是,当我单击我的按钮时,它似乎有某种滞后,按钮在视觉上保持按下半秒钟,然后启动它方法。这是有原因的,我该如何解决?

我现在拥有的:

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() 来停止它时它似乎不起作用..

4

2 回答 2

3

我的猜测是,由于您在与 UI 相同的线程中开始录制,因此 UI 不会响应,直到 startRecording() 中的所有代码都完成执行。您可能需要考虑在其自己的线程中调用 onRecord。

于 2013-03-05T08:08:31.983 回答
1
  • 您可以将AsyncTask用于短操作(最多几秒钟。)
  • 使用服务进行长时间运行的操作

注意: 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);
于 2013-03-05T08:15:00.150 回答