audio recorder android source code example using android studio
Hello Guys. Welcome to our new tutorial of audio recorder android source code example using android studio. using this source code you can create audio recording application .to be able to record audio using this application. MediaRecorder class will help you to record audio using your android device. please follow the code given below to record audio files in android.
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import android.content.pm.PackageManager; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.tutorial.personal.androidstudiopro.R; import java.io.IOException; import java.util.Random; import static android.Manifest.permission.RECORD_AUDIO; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; public class Recordingapp extends AppCompatActivity { Button buttonStart, buttonStop, buttonPlayLastRecordAudio, buttonStopPlayingRecording ; String AudioSavePathInDevice = null; MediaRecorder mediaRecorder ; Random random ; String RandomAudioFileName = "Androidstudiopro"; public static final int RequestPermissionCode = 1; MediaPlayer mediaPlayer ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recordingapp); buttonStart = (Button) findViewById(R.id.button15); buttonStop = (Button) findViewById(R.id.button25); buttonPlayLastRecordAudio = (Button) findViewById(R.id.button35); buttonStopPlayingRecording = (Button)findViewById(R.id.button45); buttonStop.setEnabled(false); buttonPlayLastRecordAudio.setEnabled(false); buttonStopPlayingRecording.setEnabled(false); random = new Random(); buttonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(checkPermission()) { AudioSavePathInDevice = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + CreateRandomAudioFileName(5) + "AudioRecording.3gp"; MediaRecorderReady(); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } buttonStart.setEnabled(false); buttonStop.setEnabled(true); Toast.makeText(Recordingapp.this, "Recording started", Toast.LENGTH_LONG).show(); } else { requestPermission(); } } }); buttonStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mediaRecorder.stop(); buttonStop.setEnabled(false); buttonPlayLastRecordAudio.setEnabled(true); buttonStart.setEnabled(true); buttonStopPlayingRecording.setEnabled(false); Toast.makeText(Recordingapp.this, "Recording Completed", Toast.LENGTH_LONG).show(); } }); buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) throws IllegalArgumentException, SecurityException, IllegalStateException { buttonStop.setEnabled(false); buttonStart.setEnabled(false); buttonStopPlayingRecording.setEnabled(true); mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(AudioSavePathInDevice); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); Toast.makeText(Recordingapp.this, "Recording Playing", Toast.LENGTH_LONG).show(); } }); buttonStopPlayingRecording.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { buttonStop.setEnabled(false); buttonStart.setEnabled(true); buttonStopPlayingRecording.setEnabled(false); buttonPlayLastRecordAudio.setEnabled(true); if(mediaPlayer != null){ mediaPlayer.stop(); mediaPlayer.release(); MediaRecorderReady(); } } }); } public void MediaRecorderReady(){ mediaRecorder=new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); mediaRecorder.setOutputFile(AudioSavePathInDevice); } public String CreateRandomAudioFileName(int string){ StringBuilder stringBuilder = new StringBuilder( string ); int i = 0 ; while(i < string ) { stringBuilder.append(RandomAudioFileName. charAt(random.nextInt(RandomAudioFileName.length()))); i++ ; } return stringBuilder.toString(); } private void requestPermission() { ActivityCompat.requestPermissions(Recordingapp.this, new String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO}, RequestPermissionCode); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case RequestPermissionCode: if (grantResults.length> 0) { boolean StoragePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean RecordPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED; } break; } } public boolean checkPermission() { int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE); int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO); return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED; } } |
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_alignParentLeft="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:text="Start Record" app:layout_constraintBottom_toTopOf="@+id/button25" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button25" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button" android:layout_centerHorizontal="true" android:text="Stop Record" app:layout_constraintBottom_toTopOf="@+id/button35" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button15" /> <Button android:id="@+id/button35" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button2" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:text="Play Record" app:layout_constraintBottom_toTopOf="@+id/button45" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button25" /> <Button android:id="@+id/button45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_alignParentEnd="true" android:text="STOP PLAYING RECORDING " app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button35" /> </android.support.constraint.ConstraintLayout> |
Output:
Finally, run this project.
This is how the audio recorder app Clock look like.