麦克风和后台服务中的扬声器使用的自动语音监听器

时间:2019-01-05 13:06:10

标签: android

我正在开发一个具有自动语音监听功能的应用程序(如果该应用程序也处于后台服务中),如果用户在任何时候使用麦克风,都需要记录语音并保存在内部存储中

我尝试在应用程序运行时记录麦克风的运行状态,但是关闭应用程序无法运行或使用麦克风发送了语音消息,但也未保存在内部存储中

https://developer.android.com/guide/topics/media/mediarecorder#java

我已经参考了这个示例来录制麦克风

我的代码:

public class MainActivity extends AppCompatActivity {

AudioManager audioManager;
private static final String LOG_TAG = "AudioRecordTest";
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private static String mFileName=null;
private MediaRecorder mRecorder = null;
public static final int REQUEST_CODE = 1234;

//Requesting permission
private boolean permissionToRecordAccepted = false;
private String [] permissions = {Manifest.permission.RECORD_AUDIO};

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFileName = getExternalCacheDir().getAbsolutePath();
    mFileName+= "/audiorecordtest.mp3";
    ActivityCompat.requestPermissions(this,permissions,REQUEST_RECORD_AUDIO_PERMISSION);

    startRecording();

    audioManager = (AudioManager)getApplicationContext().getSystemService(MainActivity.AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    audioManager.setSpeakerphoneOn(false);

    try{
        if (null != mRecorder) {
            mRecorder.stop();
            mRecorder.reset();
            mRecorder.release();

            mRecorder = null;
        }
    }catch(RuntimeException stopException){

    }

   }

   private void startRecording(){

    mRecorder=new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setOnErrorListener(errorListener);
    mRecorder.setOnInfoListener(infoListener);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
        mRecorder.start();
        Toast.makeText(this, "Workingg Da", 
    Toast.LENGTH_SHORT).show();
        Log.v(LOG_TAG,"Working");
    } catch (IllegalStateException e){
        Log.v(LOG_TAG,"IllegalState Exception");
    } catch (IOException e){
        Log.v(LOG_TAG,"prepare()failed");
        Toast.makeText(this, "Failed Recording", 
 Toast.LENGTH_SHORT).show();
    }
   }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case REQUEST_RECORD_AUDIO_PERMISSION:
            permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
            break;
    }
    if (!permissionToRecordAccepted ) finish();

}

private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(MainActivity.this,
                "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(MainActivity.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};

我希望得到输出,当使用麦克风或扬声器的用户需要记录时,即使应用已关闭并存储在内部存储中,任何监听器方法也请张贴在此处,这将有助于我完成操作。

感谢朋友

0 个答案:

没有答案