Advertisement
Guest User

Untitled

a guest
Sep 24th, 2010
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package in.omt;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import android.media.MediaRecorder;
  7. import android.os.Environment;
  8.  
  9. import android.app.Activity;
  10. import android.content.Intent;
  11. import android.graphics.Color;
  12. //import android.media.MediaPlayer;
  13. import android.os.Bundle;
  14. //import android.util.Log;
  15. import android.view.View;
  16. //import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.Toast;
  19. public class Record extends Activity {
  20.  
  21. //private String path;
  22.  
  23. private Button send;
  24.  
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.voice);
  29. send = (Button) findViewById(R.id.send);
  30. send.setTextColor(Color.RED);
  31. send.setOnClickListener(new View.OnClickListener() {
  32. public void onClick(View v) {
  33.  
  34. Toast.makeText(getBaseContext(),"Message Sent successfully",
  35. Toast.LENGTH_SHORT).show();
  36. Intent i=new Intent(getBaseContext(),bravo.class);
  37. startActivity(i);
  38.  
  39. }
  40. });
  41. final AudioRecorder recorder = new AudioRecorder("/sdcard/test.amr");
  42. try {
  43. recorder.start();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47.  
  48. //….wait a while
  49. try {
  50. recorder.stop();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54.  
  55. }
  56.  
  57. public class AudioRecorder {
  58.  
  59. final MediaRecorder recorder = new MediaRecorder();
  60. final String path;
  61.  
  62. /**
  63. * Creates a new audio recording at the given path (relative to root of SD card).
  64. */
  65. public AudioRecorder(String path) {
  66. this.path = sanitizePath(path);
  67. }
  68.  
  69.  
  70.  
  71. private String sanitizePath(String path) {
  72. if (!path.startsWith("/")) {
  73. path = "/" + path;
  74. }
  75. if (!path.contains(".")) {
  76. path += ".3gp";
  77. }
  78. return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  79. }
  80.  
  81. /**
  82. * Starts a new recording.
  83. */
  84. public void start() throws IOException {
  85. // Toast.makeText(this, "Press PTT Button To Record Voice",Toast.LENGTH_LONG).show();
  86. String state = android.os.Environment.getExternalStorageState();
  87. if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
  88. throw new IOException("SD Card is not mounted. It is " + state + ".");
  89. }
  90.  
  91. // make sure the directory we plan to store the recording in exists
  92. File directory = new File(path).getParentFile();
  93. if (!directory.exists() && !directory.mkdirs()) {
  94. throw new IOException("Path to file could not be created.");
  95. }
  96.  
  97. recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  98. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  99. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  100. recorder.setOutputFile("/sdcard/test.mp4");
  101. recorder.prepare();
  102. recorder.start();
  103. }
  104.  
  105. /**
  106. * Stops a recording that has been previously started.
  107. */
  108. public void stop() throws IOException {
  109. recorder.stop();
  110. recorder.release();
  111. }
  112.  
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement