4

我正在尝试检测 MediaRecorder 是否被其他应用程序使用,以停止我的记录器并继续供以后使用。我看到了 Androids Voice recorder 应用程序,它完全符合我所说的,但是如何?谢谢你。

4

2 回答 2

2

当另一个程序尝试访问 MediaRecorder 时,您要么收到错误消息,要么崩溃。换句话说,您拥有对它的独占访问权。

您无法直接获取 MediaRecorder 的状态,但是您可以验证您是否正在录制。这是执行您想要的代码:

package com.aplayer;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

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

        MediaRecorder 
        recorder = new MediaRecorder();
          recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile("/sdcard/test.wav");

        try {
            recorder.prepare();

        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            Log.d("ERROR ","IllegalStateException");

            Toast msg = Toast.makeText(APlayerActivity.this, "IllegalStateException", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("ERROR ","IOException");
            e.printStackTrace();

            Toast msg = Toast.makeText(APlayerActivity.this, "IOException", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        } 

        try {
        recorder.start();

        Toast msg = Toast.makeText(APlayerActivity.this, "Recording", Toast.LENGTH_LONG);
        msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
        msg.show();

        } catch (Exception e) {
        Log.e("ERROR", "start() failed");

        Toast msg = Toast.makeText(APlayerActivity.this, "Recording failed", Toast.LENGTH_LONG);
        msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
        msg.show();

        }

   }
}
于 2012-06-15T14:48:42.500 回答
1
 public boolean isMicrophoneAvailable() {
    boolean available = true;
    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_DEFAULT, 44100);
    try {
        if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
            available = false;
        }
        recorder.startRecording();
        if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
            recorder.stop();
            available = false;

        }
        recorder.stop();
    } finally {
        recorder.release();
    }

    return available;
}
于 2019-10-09T12:30:04.803 回答