Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package in.omt;
- import java.io.File;
- import java.io.IOException;
- import android.media.MediaRecorder;
- import android.os.Environment;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Color;
- //import android.media.MediaPlayer;
- import android.os.Bundle;
- //import android.util.Log;
- import android.view.View;
- //import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class Record extends Activity {
- //private String path;
- private Button send;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.voice);
- send = (Button) findViewById(R.id.send);
- send.setTextColor(Color.RED);
- send.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- Toast.makeText(getBaseContext(),"Message Sent successfully",
- Toast.LENGTH_SHORT).show();
- Intent i=new Intent(getBaseContext(),bravo.class);
- startActivity(i);
- }
- });
- final AudioRecorder recorder = new AudioRecorder("/sdcard/test.amr");
- try {
- recorder.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- //….wait a while
- try {
- recorder.stop();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public class AudioRecorder {
- final MediaRecorder recorder = new MediaRecorder();
- final String path;
- /**
- * Creates a new audio recording at the given path (relative to root of SD card).
- */
- public AudioRecorder(String path) {
- this.path = sanitizePath(path);
- }
- private String sanitizePath(String path) {
- if (!path.startsWith("/")) {
- path = "/" + path;
- }
- if (!path.contains(".")) {
- path += ".3gp";
- }
- return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
- }
- /**
- * Starts a new recording.
- */
- public void start() throws IOException {
- // Toast.makeText(this, "Press PTT Button To Record Voice",Toast.LENGTH_LONG).show();
- String state = android.os.Environment.getExternalStorageState();
- if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
- throw new IOException("SD Card is not mounted. It is " + state + ".");
- }
- // make sure the directory we plan to store the recording in exists
- File directory = new File(path).getParentFile();
- if (!directory.exists() && !directory.mkdirs()) {
- throw new IOException("Path to file could not be created.");
- }
- recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- recorder.setOutputFile("/sdcard/test.mp4");
- recorder.prepare();
- recorder.start();
- }
- /**
- * Stops a recording that has been previously started.
- */
- public void stop() throws IOException {
- recorder.stop();
- recorder.release();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement