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

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

谢谢. 最佳答案 当另一个程序试图访问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();

        }

   }
}
点赞