You are on page 1of 10

Imtiaz

Saifullah

Android Guide
with
Imtiaz Saifullah
Email: Imtiazsaifullah.m@gmail.com
FB: facebook.com/ImtiazSaif.Khan
Ping/Text: +92-331-7370872
Imtiaz
Saifullah

Android Audio Recorder


Imtiaz
Saifullah Android Audio Recorder
The android multimedia framework provides a built in support for capturing and encoding a variety of common
audio and video formats. We have a multiple ways to record audio or video but by using MediaRecorder class we
can easily implement audio or video recording.

to record an audio we need to use device’s microphone along with MediaRecorder class. In case, if we want to
record video, we need to use device’s camera along with MediaRecorder class.
use MediaRecorder to record audio in android applications.

MediaRecorder recorder = new MediaRecorder();


recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start();
recorder.stop();
recorder.reset();   
recorder.release(); 

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah Android Audio Recorder
Android Set Permissions to Record Audio

<manifest ... >
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE"/>
</manifest>

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah Android Audio Recorder
activity_main.xml file from \res\layout folder path and write the code

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp" <Button
android:paddingRight="10dp"> android:id="@+id/btnStop"
<TextView android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="100dp"
android:textStyle="bold" android:text="Stop Recording" />
android:textSize="30dp" <Button
android:gravity="center" android:id="@+id/btnPlay"
android:text="Innovative Sun \n Software android:layout_width="wrap_content"
Development Academy" android:layout_height="wrap_content"
/> android:layout_marginLeft="100dp"
<LinearLayout android:text="Play Recording" />
android:orientation="vertical" <Button
android:layout_width="match_parent" android:id="@+id/btnStopPlay"
android:layout_height="match_parent"> android:layout_width="wrap_content"
<Button android:layout_height="wrap_content"
android:id="@+id/btnRecord" android:layout_marginLeft="100dp"
android:layout_width="wrap_content" android:text="Stop Playing" />
android:layout_height="wrap_content" </LinearLayout>
android:layout_marginLeft="100dp" </LinearLayout>
android:layout_marginTop="120dp"
android:text="Start Recording" />

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah
main activity file MainActivity.java
Android Audio Recorder
startbtn.setOnClickListener(new View.OnClickListener() {
//Written by: Imtiaz Saifullah @Override
//Innovative Sun | +92-331-7370872 public void onClick(View v) {
public class MainActivity extends AppCompatActivity { if(CheckPermissions()) {
private Button startbtn, stopbtn, playbtn, stopplay; stopbtn.setEnabled(true);
private MediaRecorder mRecorder; startbtn.setEnabled(false);
private MediaPlayer mPlayer; playbtn.setEnabled(false);
private static final String LOG_TAG = "AudioRecording"; stopplay.setEnabled(false);
private static String mFileName = null; mRecorder = new MediaRecorder();
public static final int REQUEST_AUDIO_PERMISSION_CODE = 1;
@Override mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setContentView(R.layout.activity_main); ;
startbtn = (Button)findViewById(R.id.btnRecord);
stopbtn = (Button)findViewById(R.id.btnStop); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
playbtn = (Button)findViewById(R.id.btnPlay); mRecorder.setOutputFile(mFileName);
stopplay = (Button)findViewById(R.id.btnStopPlay); try {
stopbtn.setEnabled(false); mRecorder.prepare();
playbtn.setEnabled(false); } catch (IOException e) {
stopplay.setEnabled(false); Log.e(LOG_TAG, "prepare() failed");
mFileName = }
Environment.getExternalStorageDirectory().getAbsolutePath(); mRecorder.start();
mFileName += "/AudioRecording.3gp"; Toast.makeText(getApplicationContext(), "Recording Started",
Toast.LENGTH_LONG).show();
}
else
{
RequestPermissions();
}
}
});

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah
main activity file MainActivity.java
Android Audio Recorder

stopbtn.setOnClickListener(new playbtn.setOnClickListener(new View.OnClickListener() {


View.OnClickListener() { @Override
public void onClick(View v) {
@Override
stopbtn.setEnabled(false);
public void onClick(View v) { startbtn.setEnabled(true);
stopbtn.setEnabled(false); playbtn.setEnabled(false);
startbtn.setEnabled(true); stopplay.setEnabled(true);
playbtn.setEnabled(true); mPlayer = new MediaPlayer();
stopplay.setEnabled(true); try {
mPlayer.setDataSource(mFileName);
mRecorder.stop();
mPlayer.prepare();
mRecorder.release(); mPlayer.start();
mRecorder = null; Toast.makeText(getApplicationContext(),
Toast.makeText(getApplicationContext(), "Recording Started Playing", Toast.LENGTH_LONG).show();
"Recording Stopped", Toast.LENGTH_LONG).show(); } catch (IOException e) {
} Log.e(LOG_TAG, "prepare() failed");
}
});
}
});

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah
main activity file MainActivity.java
Android Audio Recorder
stopplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.release();
mPlayer = null;
stopbtn.setEnabled(false);
startbtn.setEnabled(true);
playbtn.setEnabled(true);
stopplay.setEnabled(false);
Toast.makeText(getApplicationContext(),"Playing Audio Stopped", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_AUDIO_PERMISSION_CODE:
if (grantResults.length> 0) {
boolean permissionToRecord = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean permissionToStore = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (permissionToRecord && permissionToStore) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Permission Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah
main activity file MainActivity.java
Android Audio Recorder

public boolean CheckPermissions() {


int result = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
private void RequestPermissions() {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{RECORD_AUDIO,
WRITE_EXTERNAL_STORAGE}, REQUEST_AUDIO_PERMISSION_CODE);
}
}

Innovative Sun (Software Development Academy) | Imtiaz Saifullah | +92-331-7370872


Imtiaz
Saifullah

You might also like