Android Audio recorder is use to record the voice and listen to it. In this tutorial you will learn how to create your own audio recording application and you can save your audio in your phone.
Android application user can easily record their voice as audio. Follow the below steps to create your own audio player.
Create new application with any name from file menu option.
File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish
In activity_main.xml layout file design your application.
Add following Controls:-
Import Package and write following coding in MainActivity.java class file.
import android.widget.Button; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; import android.app.Activity; import android.view.View; import android.widget.Toast; import java.io.IOException; public class MainActivity extends ActionBarActivity { private MediaRecorder myAudioRecorder; private String outputFile = null; private Button start,stop,play; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button)findViewById(R.id.button1); stop = (Button)findViewById(R.id.button2); play = (Button)findViewById(R.id.button3); stop.setEnabled(false); play.setEnabled(false); outputFile = Environment.getExternalStorageDirectory(). getAbsolutePath() + "/myrecording.3gp"; myAudioRecorder = new MediaRecorder(); myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); myAudioRecorder.setOutputFile(outputFile); } public void start(View view){ try { myAudioRecorder.prepare(); myAudioRecorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } start.setEnabled(false); stop.setEnabled(true); Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show(); } public void stop(View view){ myAudioRecorder.stop(); myAudioRecorder.release(); myAudioRecorder = null; stop.setEnabled(false); play.setEnabled(true); Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show(); } public void play(View view) throws IllegalArgumentException,SecurityException, IllegalStateException, IOException { MediaPlayer m = new MediaPlayer(); m.setDataSource(outputFile); m.prepare(); m.start(); Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show(); }
Now add some code in AndroidManifest.xml file.
Go to → app → manifest → AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sumit.audioapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sumit.audioapp.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now your task has been complete. Now you have to test this application in your phone. So copy apk file in your phone and install it.