This is inspired by the profgustin(You Tube)
The first file will be AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.saroj.andridrecording" > <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> <uses-permission android:name="android.permission.write_external_storage"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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>
The Second File is MainActivity.java
import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import java.io.File; public class MainActivity extends Activity { //MediaRecorder class is for Recording and Media Player is for Playing the Recorded Media private MediaRecorder mediaRecorder; private MediaPlayer mediaPlayer; // The String OUTPUT_FILE will hold the path of the OUTPUT_FILE private String OUTPUT_FILE; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); OUTPUT_FILE= Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp"; } public void buttonTapped(View v) { switch (v.getId()) { case R.id.StartButton: try { beginRecording(); } catch (Exception ex) { ex.printStackTrace(); } break; case R.id.FinishButton: try { stopRecording(); } catch (Exception ex) { ex.printStackTrace(); } break; case R.id.PlayRecording: try { playRecording(); } catch (Exception ex) { ex.printStackTrace(); } break; case R.id.StopPlayBack: try { stopPlayBack(); } catch (Exception ex) { ex.printStackTrace(); } break; } } private void beginRecording() { ditchMediaRecorder(); File outFile=new File(OUTPUT_FILE); if(outFile.exists()) outFile.delete(); mediaRecorder=new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(OUTPUT_FILE); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (Exception ex) { } } private void stopRecording() { if(mediaRecorder!=null) { mediaRecorder.stop(); } } private void playRecording() { ditchMediaPlayer(); mediaPlayer=new MediaPlayer(); try { mediaPlayer.setDataSource(OUTPUT_FILE); mediaPlayer.prepare(); mediaPlayer.start(); } catch (Exception e) { e.printStackTrace(); } } private void ditchMediaPlayer() { if(mediaPlayer!=null) { try{ mediaPlayer.release(); } catch (Exception ex) { ex.getMessage(); } } } private void stopPlayBack() { if (mediaPlayer!=null) { mediaPlayer.stop(); } } private void ditchMediaRecorder() { if( mediaRecorder !=null) { mediaRecorder.release(); } } }The Third and final layout file is activity_main.xml,<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="0dp" android:text="Start Recording" android:id="@+id/StartButton" android:layout_weight="1" android:onClick="buttonTapped" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:text="Finish Recording" android:id="@+id/FinishButton" android:layout_weight="1" android:onClick="buttonTapped" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:text="Play Recording" android:id="@+id/PlayRecording" android:layout_weight="1" android:onClick="buttonTapped" /> <Button android:layout_width="match_parent" android:layout_height="0dp" android:text="Stop PlayBack" android:id="@+id/StopPlayBack" android:layout_weight="1" android:onClick="buttonTapped" /> </LinearLayout> </LinearLayout>
Great post and tutorial. I was recently recommended to use one voice and call recording application Total Recall which facilitates recording of calls with good quality sound recording and manifold features like auto deletion, auto recording, password security and much more. Check this app once.
ReplyDelete