0

My Code:

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;


    }
}

when I ran this code on my Samsung Galaxy s7 edge, as well as on the nexxus 7 emulator given by android studio the app crashes. I posted all of the code because im not sure exactly where its messing up. It crashes when I press the button. It as made to be a press and hold to record, and release to stop recording

Manohar
  • 22,116
  • 9
  • 108
  • 144

1 Answers1

0

Check if the issue is with permission recording audio.

mnp343
  • 329
  • 1
  • 6