Android 直接调用系统的录音机,并返回录音结果


Android 直接调用系统的录音机,并返回录音结果_第1张图片

package com.zhangyu.mysysrecorder;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView tvRecorder;
    private final int REQUEST_CODE = 111;

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

    private void initView() {
        tvRecorder = (TextView) findViewById(R.id.tvRecorder);
        tvRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置intent的属性为录音设置
                Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //请求
            if (requestCode == REQUEST_CODE) {
                //得到录音的音频文件及路径
                String dataFile = data.getDataString();
                Log.d(TAG, "dataFile: " + dataFile);
            }
        }
    }
}

2019-12-22 17:19:24.729 16758-16758/com.zhangyu.mysysrecorder D/MainActivity: dataFile: content://media/external/file/1205670

你可能感兴趣的:(Android基础)