如何检测MediaRecorder是否被其他应用程序使用?

时间:2012-06-15 11:09:31

标签: java android

我正在尝试检测其他应用程序是否使用了MediaRecorder,以停止我的刻录机并继续供以后使用。我看到了Androids录音机应用程序,它正是我正在谈论的内容,但如何? 谢谢。

2 个答案:

答案 0 :(得分: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();

        }

   }
}

答案 1 :(得分:0)

 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;
}