Site icon C1CTech

Android Audio Recorder Example

&NewLine;<p>This tutorial is about how to implement audio recorder in android with the help of a simple application&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AudioRecorderApp" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"900" data-videoid&equals;"4&lowbar;rTcYor6fU" title&equals;"Android Audio Recorder Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;4&lowbar;rTcYor6fU"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;4&lowbar;rTcYor6fU&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Audio Recorder Example"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"s1">MediaRecorder Class<&sol;span><&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">In android&comma;&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><b>MediaRecorder<&sol;b><&sol;span>&nbsp&semi;class will provide a functionality to record audio or video files&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">In android&comma; to record an audio we need to use device’s <strong><span style&equals;"color&colon; &num;008000&semi;">microphone<&sol;span><&sol;strong> along with&nbsp&semi;<b>MediaRecorder<&sol;b>&nbsp&semi;class&period; In case&comma; if we want to record video&comma; we need to use device’s <strong><span style&equals;"color&colon; &num;008000&semi;">camera<&sol;span><&sol;strong> along with&nbsp&semi;<b>MediaRecorder<&sol;b>&nbsp&semi;class&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The Android multimedia framework provides built-in support for capturing and encoding common audio and video formats&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>A common case of using MediaRecorder to record audio works as follows&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"> MediaRecorder recorder &equals; new MediaRecorder&lpar;&rpar;&semi;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Sets the audio source to be used for recording &NewLine;&sol;&sol;i&period;e&period; Microphone audio source<&sol;strong><&sol;span>&NewLine; recorder&period;setAudioSource&lpar;MediaRecorder&period;AudioSource&period;MIC&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Sets the format of the output file produced during recording&period;&NewLine;<&sol;span><&sol;strong> recorder&period;setOutputFormat&lpar;MediaRecorder&period;OutputFormat&period;THREE&lowbar;GPP&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Sets the audio encoder to be used for recording&period;&NewLine;<&sol;span><&sol;strong> recorder&period;setAudioEncoder&lpar;MediaRecorder&period;AudioEncoder&period;AMR&lowbar;NB&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Sets the path of the output file to be produced&period;&NewLine;<&sol;span><&sol;strong> recorder&period;setOutputFile&lpar;PATH&lowbar;NAME&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Prepares the recorder to begin capturing and encoding data&period;&NewLine;<&sol;span><&sol;strong> recorder&period;prepare&lpar;&rpar;&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Begins capturing and encoding data to the file &NewLine; &sol;&sol;specified with setOutputFile&lpar;&rpar;&period;<&sol;span><&sol;strong>&NewLine; recorder&period;start&lpar;&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Recording is now started<&sol;strong><&sol;span>&NewLine; &period;&period;&period;&NewLine;&NewLine; recorder&period;stop&lpar;&rpar;&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Stops recording&period;<&sol;span><&sol;strong>&NewLine; recorder&period;reset&lpar;&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; You can reuse the object by going back to setAudioSource&lpar;&rpar; step<&sol;strong><&sol;span>&NewLine; recorder&period;release&lpar;&rpar;&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Now the object cannot be reused<&sol;strong><&sol;span><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>1 &period; Create a new project by going to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>File&nbsp&semi;<&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">&nbsp&semi;New Android Project<&sol;span>&comma;<&sol;b>&nbsp&semi;select&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span>&nbsp&semi;&comma; provide&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>app name<&sol;strong><&sol;span>&comma; select language to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>java <&sol;strong><&sol;span>and then finally click on&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>2 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> file and add the below permissions&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine;&NewLine; package&equals;"com&period;c1ctech&period;audiorecorderapp"&gt&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &lt&semi;&excl;--Permission for recording audio and storage of audio in user's device--&gt&semi;&NewLine;<&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><strong> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;RECORD&lowbar;AUDIO"&sol;&gt&semi;<&sol;strong><&sol;span><&sol;span><span style&equals;"color&colon; &num;0000ff&semi;">&NewLine;<strong> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;WRITE&lowbar;EXTERNAL&lowbar;STORAGE"&sol;&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;STORAGE"&sol;&gt&semi;<&sol;strong><&sol;span>&NewLine;&NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;requestLegacyExternalStorage&equals;"true"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;manifest&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>3 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> and add the below code&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;start&lowbar;rec"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"60dp"&NewLine; android&colon;text&equals;"Start Recording"&NewLine; android&colon;padding&equals;"10dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;50"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;rec"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Stop Recording"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"20dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;start&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;start&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;start&lowbar;rec" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;play&lowbar;rec"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Play Recording"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"20dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;rec" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;playing"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Stop Playing"&NewLine; android&colon;layout&lowbar;marginTop&equals;"20dp"&NewLine; android&colon;padding&equals;"10dp"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;play&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;play&lowbar;rec"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;play&lowbar;rec" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tvStatus"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"50dp"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;playing"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;playing"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;stop&lowbar;playing" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>4 &period; Open&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and add the below code&period; It uses <span style&equals;"color&colon; &num;008000&semi;"><strong>MediaRecorder<&sol;strong><&sol;span> that captures audio from a device microphone&comma; save the audio&comma; and play it back &lpar;with <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;media&sol;MediaPlayer"><span class&equals;"s2">MediaPlayer<&sol;span><&sol;a><&sol;span><&sol;strong>&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;audiorecorderapp&semi;&NewLine;&NewLine;import android&period;content&period;pm&period;PackageManager&semi;&NewLine;import android&period;media&period;MediaPlayer&semi;&NewLine;import android&period;media&period;MediaRecorder&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;os&period;Environment&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import java&period;io&period;IOException&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;import androidx&period;core&period;app&period;ActivityCompat&semi;&NewLine;import androidx&period;core&period;content&period;ContextCompat&semi;&NewLine;&NewLine;import static android&period;Manifest&period;permission&period;RECORD&lowbar;AUDIO&semi;&NewLine;import static android&period;Manifest&period;permission&period;WRITE&lowbar;EXTERNAL&lowbar;STORAGE&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private Button btnStart&comma; btnStop&comma; btnPlay&comma; btnStopPlay&semi;&NewLine;&NewLine; private TextView tvStatus&semi;&NewLine;&NewLine; private MediaRecorder mRecorder&semi;&NewLine;&NewLine; private MediaPlayer mPlayer&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; string variable is created for storing a file name<&sol;strong><&sol;span>&NewLine; private static String mFileName &equals; null&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; constant for audio permission&NewLine;<&sol;span><&sol;strong> public static final int REQUEST&lowbar;AUDIO&lowbar;PERMISSION&lowbar;CODE &equals; 1&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; get views by id&NewLine;<&sol;strong><&sol;span> tvStatus &equals; findViewById&lpar;R&period;id&period;tvStatus&rpar;&semi;&NewLine; btnStart &equals; findViewById&lpar;R&period;id&period;btn&lowbar;start&lowbar;rec&rpar;&semi;&NewLine; btnStop &equals; findViewById&lpar;R&period;id&period;btn&lowbar;stop&lowbar;rec&rpar;&semi;&NewLine; btnPlay &equals; findViewById&lpar;R&period;id&period;btn&lowbar;play&lowbar;rec&rpar;&semi;&NewLine; btnStopPlay &equals; findViewById&lpar;R&period;id&period;btn&lowbar;stop&lowbar;playing&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;setting buttons background color&NewLine;<&sol;strong><&sol;span> btnStop&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStopPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine;&NewLine; btnStart&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; start audio recording&period;&NewLine;<&sol;strong><&sol;span> startRecording&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; btnStop&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; stop audio recording&period;&NewLine;<&sol;strong><&sol;span> stopRecording&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; btnPlay&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine; &sol;&sol; play the recorded audio&NewLine; playAudio&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; btnStopPlay&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; stop playing the recorded audio&NewLine;<&sol;strong><&sol;span> stopPlaying&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void startRecording&lpar;&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; check permission method is used to check&NewLine; &sol;&sol; that the user has granted permission&NewLine; &sol;&sol; to record and store the audio&period;<&sol;strong><&sol;span>&NewLine; if &lpar;CheckPermissions&lpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;setting buttons background color<&sol;strong><&sol;span>&NewLine; btnStop&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnStart&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStopPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;initializing filename variable&NewLine; &sol;&sol; with the path of the recorded audio file&period;<&sol;strong><&sol;span>&NewLine; mFileName &equals; Environment&period;getExternalStorageDirectory&lpar;&rpar;&period;getAbsolutePath&lpar;&rpar;&semi;&NewLine; mFileName &plus;&equals; "&sol;AudioRecording&period;3gp"&semi;&NewLine;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;initializing media recorder class<&sol;strong><&sol;span>&NewLine; mRecorder &equals; new MediaRecorder&lpar;&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Sets the audio source to be used for recording&period;&NewLine;<&sol;strong><&sol;span> mRecorder&period;setAudioSource&lpar;MediaRecorder&period;AudioSource&period;MIC&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; set the output format of the audio&period;&NewLine;<&sol;span><&sol;strong> mRecorder&period;setOutputFormat&lpar;MediaRecorder&period;OutputFormat&period;THREE&lowbar;GPP&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; set the audio encoder for recorded audio&period;&NewLine;<&sol;strong><&sol;span> mRecorder&period;setAudioEncoder&lpar;MediaRecorder&period;AudioEncoder&period;AMR&lowbar;NB&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;set the output file location for recorded audio&NewLine;<&sol;strong><&sol;span> mRecorder&period;setOutputFile&lpar;mFileName&rpar;&semi;&NewLine; try &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;Prepares the recorder to begin capturing and encoding data&period;&NewLine;<&sol;strong><&sol;span> mRecorder&period;prepare&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub; catch &lpar;IOException e&rpar; &lbrace;&NewLine; Log&period;e&lpar;"TAG"&comma; "prepare&lpar;&rpar; failed"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; start the audio recording&period;&NewLine;<&sol;strong><&sol;span> mRecorder&period;start&lpar;&rpar;&semi;&NewLine;&NewLine; tvStatus&period;setText&lpar;"Recording Started"&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; if audio recording permissions are&NewLine; &sol;&sol; not granted by user this method will&NewLine; &sol;&sol; ask for runtime permission for mic and storage&period;<&sol;strong><&sol;span>&NewLine; RequestPermissions&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onRequestPermissionsResult&lpar;int requestCode&comma; String&lbrack;&rsqb; permissions&comma; int&lbrack;&rsqb; grantResults&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; this method is called when user will&NewLine; &sol;&sol; grant the permission for audio recording&period;<&sol;span><&sol;strong>&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case REQUEST&lowbar;AUDIO&lowbar;PERMISSION&lowbar;CODE&colon;&NewLine; if &lpar;grantResults&period;length &gt&semi; 0&rpar; &lbrace;&NewLine; boolean permissionToRecord &equals; grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; boolean permissionToStore &equals; grantResults&lbrack;1&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; if &lpar;permissionToRecord &amp&semi;&amp&semi; permissionToStore&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Permission Granted"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Permission Denied"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; public boolean CheckPermissions&lpar;&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; this method is used to check permission<&sol;strong><&sol;span>&NewLine; int result &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; WRITE&lowbar;EXTERNAL&lowbar;STORAGE&rpar;&semi;&NewLine; int result1 &equals; ContextCompat&period;checkSelfPermission&lpar;getApplicationContext&lpar;&rpar;&comma; RECORD&lowbar;AUDIO&rpar;&semi;&NewLine; return result &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED &amp&semi;&amp&semi; result1 &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&semi;&NewLine; &rcub;&NewLine;&NewLine; private void RequestPermissions&lpar;&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; this method is used to request the&NewLine; &sol;&sol; permission for audio recording and storage&period;<&sol;strong><&sol;span>&NewLine; ActivityCompat&period;requestPermissions&lpar;MainActivity&period;this&comma; new String&lbrack;&rsqb;&lbrace;RECORD&lowbar;AUDIO&comma; WRITE&lowbar;EXTERNAL&lowbar;STORAGE&rcub;&comma; REQUEST&lowbar;AUDIO&lowbar;PERMISSION&lowbar;CODE&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;play the recorded audio<&sol;strong><&sol;span>&NewLine; public void playAudio&lpar;&rpar; &lbrace;&NewLine; btnStop&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStart&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStopPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; using media player class for playing recorded audio&NewLine;<&sol;strong><&sol;span> mPlayer &equals; new MediaPlayer&lpar;&rpar;&semi;&NewLine; try &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; set the data source which will be our file name&NewLine;<&sol;strong><&sol;span> mPlayer&period;setDataSource&lpar;mFileName&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;prepare media player&NewLine;<&sol;span><&sol;strong> mPlayer&period;prepare&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; start media player&period;&NewLine;<&sol;span><&sol;strong> mPlayer&period;start&lpar;&rpar;&semi;&NewLine; tvStatus&period;setText&lpar;"Recording Started Playing"&rpar;&semi;&NewLine;&NewLine; &rcub; catch &lpar;IOException e&rpar; &lbrace;&NewLine; Log&period;e&lpar;"TAG"&comma; "prepare&lpar;&rpar; failed"&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; public void stopRecording&lpar;&rpar; &lbrace;&NewLine; btnStop&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStart&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnStopPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; stop the audio recording&period;&NewLine;<&sol;strong><&sol;span> mRecorder&period;stop&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; release the media recorder object&period;&NewLine;<&sol;span><&sol;strong> mRecorder&period;release&lpar;&rpar;&semi;&NewLine; mRecorder &equals; null&semi;&NewLine;&NewLine; tvStatus&period;setText&lpar;"Recording Stopped"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void stopPlaying&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; release the media player object&NewLine; &sol;&sol; and stop playing recorded audio&period;<&sol;span><&sol;strong>&NewLine; mPlayer&period;release&lpar;&rpar;&semi;&NewLine; mPlayer &equals; null&semi;&NewLine; btnStop&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; btnStart&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorPrimary&rpar;&rpar;&semi;&NewLine; btnStopPlay&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;colorAccent&rpar;&rpar;&semi;&NewLine; tvStatus&period;setText&lpar;"Recording Play Stopped"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onStop&lpar;&rpar; &lbrace;&NewLine; super&period;onStop&lpar;&rpar;&semi;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; releasing the media player and the media recorder object&NewLine; &sol;&sol; and set it to null<&sol;strong><&sol;span>&NewLine; if &lpar;mRecorder &excl;&equals; null&rpar; &lbrace;&NewLine; mRecorder&period;release&lpar;&rpar;&semi;&NewLine; mRecorder &equals; null&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;mPlayer &excl;&equals; null&rpar; &lbrace;&NewLine; mPlayer&period;release&lpar;&rpar;&semi;&NewLine; mPlayer &equals; null&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>Now when you run the app on Physical device it will look like this&colon;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;04&sol;Screenshot&lowbar;1617259698&period;png" alt&equals;"" class&equals;"wp-image-2500" width&equals;"394" height&equals;"700"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;

Exit mobile version