Guest User

Untitled

a guest
Oct 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. package com.android.AudioRecord_Player;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import android.app.Activity;
  9. import android.media.AudioFormat;
  10. import android.media.AudioRecord;
  11. import android.media.MediaRecorder;
  12. import android.os.Bundle;
  13. import android.os.Environment;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.ImageButton;
  17. import android.widget.Toast;
  18.  
  19. public class AudioRecord_Player extends Activity implements OnClickListener{
  20.  
  21. private static final int RECORDER_BPP = 16;
  22. private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
  23. private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
  24. private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
  25. private static final int RECORDER_SAMPLERATE = 8000;
  26. private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
  27. private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
  28.  
  29. private static final int AUDIO_SAMPLE_FREQ = 44100;
  30. private static final int AUDIO_BUFFER_SIZE = 200000;
  31.  
  32. private AudioRecord recorder = null;
  33. private int bufferSize = 0;
  34. private Thread recordingThread = null;
  35. private boolean isRecording = false;
  36.  
  37. ImageButton start_record,pause_rec,stop_rec,play_rec,resume_rec;
  38.  
  39. /** Called when the activity is first created. */
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main);
  44.  
  45. start_record=(ImageButton)findViewById(R.id.rec);
  46. pause_rec=(ImageButton)findViewById(R.id.pause);
  47. resume_rec=(ImageButton)findViewById(R.id.resume);
  48. stop_rec=(ImageButton)findViewById(R.id.stop);
  49. play_rec=(ImageButton)findViewById(R.id.play);
  50.  
  51. pause_rec.setEnabled(false);
  52. stop_rec.setEnabled(false);
  53. play_rec.setEnabled(false);
  54. resume_rec.setEnabled(false);
  55.  
  56. bufferSize = AUDIO_BUFFER_SIZE;
  57. // AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
  58.  
  59. start_record.setOnClickListener(this);
  60. pause_rec.setOnClickListener(this);
  61. stop_rec.setOnClickListener(this);
  62. play_rec.setOnClickListener(this);
  63. resume_rec.setOnClickListener(this);
  64. }
  65.  
  66. private String getFilename(){
  67. String filepath = Environment.getExternalStorageDirectory().getPath();
  68. File file = new File(filepath,AUDIO_RECORDER_FOLDER);
  69.  
  70. if(!file.exists()){
  71. file.mkdirs();
  72. }
  73.  
  74. return (file.getAbsolutePath() + "/" + "Ravindra" + AUDIO_RECORDER_FILE_EXT_WAV);
  75. }
  76.  
  77. private String getTempFilename(){
  78. String filepath = Environment.getExternalStorageDirectory().getPath();
  79. File file = new File(filepath,AUDIO_RECORDER_FOLDER);
  80.  
  81. if(!file.exists()){
  82. file.mkdirs();
  83. }
  84.  
  85. File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
  86.  
  87. if(tempFile.exists())
  88. tempFile.delete();
  89.  
  90. return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
  91. }
  92.  
  93. private void startRecording(){
  94. //recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
  95. recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
  96. AUDIO_SAMPLE_FREQ,
  97. AudioFormat.CHANNEL_CONFIGURATION_MONO,
  98. AudioFormat.ENCODING_PCM_16BIT,
  99. AUDIO_BUFFER_SIZE);
  100.  
  101. recorder.startRecording();
  102.  
  103. isRecording = true;
  104.  
  105. recordingThread = new Thread(new Runnable() {
  106.  
  107. @Override
  108. public void run() {
  109. writeAudioDataToFile();
  110. }
  111. },"AudioRecorder Thread");
  112.  
  113. recordingThread.start();
  114. }
  115.  
  116. private void stopRecording(boolean b){
  117. if(recorder != null){
  118. isRecording = false;
  119.  
  120. recorder.stop();
  121. recorder.release();
  122.  
  123. recorder = null;
  124. recordingThread = null;
  125. }
  126.  
  127. copyWaveFile(getTempFilename(),getFilename(),b);
  128. deleteTempFile();
  129. }
  130.  
  131. private void deleteTempFile() {
  132. File file = new File(getTempFilename());
  133.  
  134. file.delete();
  135. }
  136. private void writeAudioDataToFile(){
  137. byte data[] = new byte[bufferSize];
  138. String filename = getTempFilename();
  139. FileOutputStream os = null;
  140.  
  141. try {
  142. os = new FileOutputStream(filename);
  143. } catch (FileNotFoundException e) {
  144. e.printStackTrace();
  145. }
  146.  
  147. int read = 0;
  148.  
  149. if(os != null){
  150. while(isRecording){
  151. read = recorder.read(data, 0, bufferSize);
  152.  
  153. if(AudioRecord.ERROR_INVALID_OPERATION != read){
  154. try {
  155. os.write(data);
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. }
  161.  
  162. try {
  163. os.close();
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169.  
  170. private void copyWaveFile(String inFilename,String outFilename, boolean b){
  171. FileInputStream in = null;
  172. FileOutputStream out = null;
  173. long totalAudioLen = 0;
  174. long totalDataLen = totalAudioLen + 36;
  175. long longSampleRate = RECORDER_SAMPLERATE;
  176. int channels = 2;
  177. long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;
  178.  
  179. byte[] data = new byte[bufferSize];
  180.  
  181. try {
  182. in = new FileInputStream(inFilename);
  183. out = new FileOutputStream(outFilename,b);
  184. totalAudioLen = in.getChannel().size();
  185. totalDataLen = totalAudioLen + 36;
  186.  
  187. //AppLog.logString("File size: " + totalDataLen);
  188.  
  189. WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
  190. longSampleRate, channels, byteRate);
  191.  
  192. while(in.read(data) != -1){
  193. out.write(data);
  194. }
  195.  
  196. in.close();
  197. out.close();
  198. } catch (FileNotFoundException e) {
  199. e.printStackTrace();
  200. } catch (IOException e) {
  201. e.printStackTrace();
  202. }
  203. }
  204.  
  205.  
  206. private void WriteWaveFileHeader(
  207. FileOutputStream out, long totalAudioLen,
  208. long totalDataLen, long longSampleRate, int channels,
  209. long byteRate) throws IOException {
  210.  
  211. byte[] header = new byte[44];
  212.  
  213. header[0] = 'R'; // RIFF/WAVE header
  214. header[1] = 'I';
  215. header[2] = 'F';
  216. header[3] = 'F';
  217. header[4] = (byte) (totalDataLen & 0xff);
  218. header[5] = (byte) ((totalDataLen >> 8) & 0xff);
  219. header[6] = (byte) ((totalDataLen >> 16) & 0xff);
  220. header[7] = (byte) ((totalDataLen >> 24) & 0xff);
  221. header[8] = 'W';
  222. header[9] = 'A';
  223. header[10] = 'V';
  224. header[11] = 'E';
  225. header[12] = 'f'; // 'fmt ' chunk
  226. header[13] = 'm';
  227. header[14] = 't';
  228. header[15] = ' ';
  229. header[16] = 16; // 4 bytes: size of 'fmt ' chunk
  230. header[17] = 0;
  231. header[18] = 0;
  232. header[19] = 0;
  233. header[20] = 1; // format = 1
  234. header[21] = 0;
  235. header[22] = (byte) channels;
  236. header[23] = 0;
  237. header[24] = (byte) (longSampleRate & 0xff);
  238. header[25] = (byte) ((longSampleRate >> 8) & 0xff);
  239. header[26] = (byte) ((longSampleRate >> 16) & 0xff);
  240. header[27] = (byte) ((longSampleRate >> 24) & 0xff);
  241. header[28] = (byte) (byteRate & 0xff);
  242. header[29] = (byte) ((byteRate >> 8) & 0xff);
  243. header[30] = (byte) ((byteRate >> 16) & 0xff);
  244. header[31] = (byte) ((byteRate >> 24) & 0xff);
  245. header[32] = (byte) (2 * 16 / 8); // block align
  246. header[33] = 0;
  247. header[34] = RECORDER_BPP; // bits per sample
  248. header[35] = 0;
  249. header[36] = 'd';
  250. header[37] = 'a';
  251. header[38] = 't';
  252. header[39] = 'a';
  253. header[40] = (byte) (totalAudioLen & 0xff);
  254. header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
  255. header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
  256. header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
  257.  
  258. out.write(header, 0, 44);
  259. }
  260.  
  261. @Override
  262. public void onClick(View v) {
  263. // TODO Auto-generated method stub
  264. if(v==start_record)
  265. {
  266. Toast.makeText(AudioRecord_Player.this, "Now Recording...", Toast.LENGTH_LONG).show();
  267. startRecording();
  268. start_record.setEnabled(false);
  269. pause_rec.setEnabled(true);
  270. stop_rec.setEnabled(true);
  271.  
  272. }
  273. else if(v==stop_rec)
  274. {
  275. Toast.makeText(AudioRecord_Player.this, "Recording Stopped...", Toast.LENGTH_LONG).show();
  276. stopRecording(true);
  277. start_record.setEnabled(true);
  278. stop_rec.setEnabled(false);
  279. pause_rec.setEnabled(false);
  280.  
  281. }
  282. else if(v==pause_rec)
  283. {
  284. Toast.makeText(AudioRecord_Player.this, "Pausing...", Toast.LENGTH_LONG).show();
  285. stopRecording(false);
  286. pause_rec.setEnabled(false);
  287. resume_rec.setEnabled(true);
  288.  
  289. }
  290. else if(v==play_rec)
  291. {
  292. Toast.makeText(AudioRecord_Player.this, "Now Playing...", Toast.LENGTH_LONG).show();
  293.  
  294.  
  295. }
  296. else if(v==resume_rec)
  297. {
  298. Toast.makeText(AudioRecord_Player.this, "Resuming...", Toast.LENGTH_LONG).show();
  299. //recorder.startRecording();
  300. startRecording();
  301. start_record.setEnabled(false);
  302. pause_rec.setEnabled(true);
  303. stop_rec.setEnabled(true);
  304.  
  305.  
  306. }
  307. }
  308. }
Add Comment
Please, Sign In to add comment