-
-
Save byteshiva/f5d086d97bd18a3a8e755e84f85c09df to your computer and use it in GitHub Desktop.
Main Activity for the Android Audio Recorder Tutorial on the SSaurel's Channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.audiorecorder; | |
import android.media.MediaPlayer; | |
import android.media.MediaRecorder; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import java.io.IOException; | |
public class MainActivity extends AppCompatActivity { | |
private Button play, stop, record; | |
private MediaRecorder myAudioRecorder; | |
private String outputFile; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
play = (Button) findViewById(R.id.play); | |
stop = (Button) findViewById(R.id.stop); | |
record = (Button) findViewById(R.id.record); | |
stop.setEnabled(false); | |
play.setEnabled(false); | |
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp"; | |
myAudioRecorder = new MediaRecorder(); | |
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | |
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); | |
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); | |
myAudioRecorder.setOutputFile(outputFile); | |
record.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
try { | |
myAudioRecorder.prepare(); | |
myAudioRecorder.start(); | |
} catch (IllegalStateException ise) { | |
// make something ... | |
} catch (IOException ioe) { | |
// make something | |
} | |
record.setEnabled(false); | |
stop.setEnabled(true); | |
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
stop.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
myAudioRecorder.stop(); | |
myAudioRecorder.release(); | |
myAudioRecorder = null; | |
record.setEnabled(true); | |
stop.setEnabled(false); | |
play.setEnabled(true); | |
Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
play.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
MediaPlayer mediaPlayer = new MediaPlayer(); | |
try { | |
mediaPlayer.setDataSource(outputFile); | |
mediaPlayer.prepare(); | |
mediaPlayer.start(); | |
Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show(); | |
} catch (Exception e) { | |
// make something | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment