Android工作室的语音录制程序与Java

时间:2017-07-14 17:07:25

标签: android

我的代码:

public class MainActivity extends AppCompatActivity {

    private Button mRecordBtn;
    private TextView mRecordLabel;

    private MediaRecorder mRecorder;

    private String mFileName = null;

    private static final String LOG_TAG = "Record_log";

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

        mRecordLabel = (TextView) findViewById(R.id.recordLabel);
        mRecordBtn = (Button) findViewById(R.id.recordBtn);

        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName = "/recorded_audio.3gp";


        mRecordBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent motionEvent) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN ){

                    startRecording();
                   mRecordLabel.setText("Recording in Progress");


                }
                else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                    stopRecording();
                    mRecordLabel.setText("Recording Stopped");

                }
                return false;
            }
        });

    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start(); //I am getting a bug on this line
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;


    }
}

当我在三星Galaxy s7边缘运行此代码时,以及Android studio提供的nexxus 7模拟器应用程序崩溃。我发布了所有的代码,因为我不确定它的确切位置。按下按钮时崩溃了。它被压缩并保持记录,并释放以停止录制

1 个答案:

答案 0 :(得分:0)

检查问题是否是录制音频的权限。