Advertisement
saurabhmesh17

SO ANswer - AudioRecord Issue

Sep 22nd, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. package com.example.soquestion;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import android.support.v7.app.ActionBarActivity;
  12. import android.support.v7.app.ActionBar;
  13. import android.support.v4.app.Fragment;
  14. import android.media.AudioFormat;
  15. import android.media.AudioManager;
  16. import android.media.AudioRecord;
  17. import android.media.AudioTrack;
  18. import android.media.MediaRecorder;
  19. import android.media.MediaRecorder.AudioSource;
  20. import android.os.Bundle;
  21. import android.os.Environment;
  22. import android.util.Log;
  23. import android.view.LayoutInflater;
  24. import android.view.Menu;
  25. import android.view.MenuItem;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28. import android.widget.Button;
  29. import android.os.Build;
  30.  
  31. public class MainActivity extends ActionBarActivity {
  32.  
  33. protected static final String TAG = "SO_QUESTION";
  34.  
  35. private int BufferSize;
  36. byte[] buffer = new byte[BufferSize];
  37.  
  38. private AudioRecord recorder = null;
  39. private AudioTrack track = null;
  40.  
  41. private int sampleRate = 8000;
  42. private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
  43. private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
  44.  
  45. private boolean isRecording = true;
  46. private Thread recordingThread = null;
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_main);
  52.  
  53.  
  54. setContentView(R.layout.activity_main);
  55.  
  56. setButtonHandlers();
  57.  
  58. enableButton(R.id.btnStartRec,true);
  59. enableButton(R.id.btnStopRec,false);
  60.  
  61. enableButton(R.id.btnStartPlay,false);
  62. enableButton(R.id.btnStopPlay,false);
  63.  
  64.  
  65. BufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
  66. /*if (savedInstanceState == null) {
  67. getSupportFragmentManager().beginTransaction()
  68. .add(R.id.container, new PlaceholderFragment()).commit();
  69. }*/
  70. }
  71.  
  72. private void writeAudioDataToFile()
  73. {
  74. byte data[] = new byte[BufferSize];
  75. String filename = "/sdcard/audiofile.raw";
  76. FileOutputStream os = null;
  77.  
  78. try {
  79. os = new FileOutputStream(filename);
  80. } catch (FileNotFoundException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84.  
  85. int read = 0;
  86.  
  87. if(null != os){
  88. while(isRecording)
  89. {
  90. read = recorder.read(data, 0, BufferSize);
  91.  
  92. if(AudioRecord.ERROR_INVALID_OPERATION != read){
  93. try {
  94. os.write(data);
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. }
  100.  
  101. try {
  102. os.close();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108.  
  109. private void startRecording()
  110. {
  111. recorder = new AudioRecord(AudioSource.DEFAULT, 8000, channelConfig, audioFormat, BufferSize);
  112. if(1 == recorder.getState())
  113. recorder.startRecording();
  114.  
  115. isRecording = true;
  116. recordingThread = new Thread(new Runnable() {
  117. @Override
  118. public void run() {
  119. writeAudioDataToFile();
  120. }
  121. },"AudioRecorder Thread");
  122. recordingThread.start();
  123. }
  124.  
  125. private void stopRecording(){
  126. if(null != recorder){
  127. isRecording = false;
  128.  
  129. int i = recorder.getState();
  130. if(i==1)
  131. recorder.stop();
  132. recorder.release();
  133. Log.d(TAG, "===== Recording Audio Completed ===== ");
  134.  
  135. recorder = null;
  136. recordingThread = null;
  137. }
  138. }
  139.  
  140. public void StartPlaying()
  141. {
  142. int minBufferSize = AudioTrack.getMinBufferSize(8000,
  143. AudioFormat.CHANNEL_OUT_MONO,
  144. AudioFormat.ENCODING_PCM_16BIT); /* Return 640 */
  145.  
  146.  
  147. int bufferSize = minBufferSize;
  148. AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
  149. AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
  150.  
  151. int i = 0;
  152. byte[] temp = new byte[bufferSize];
  153. try {
  154. FileInputStream fin = new FileInputStream("/sdcard/audiofile.raw");
  155. Log.d(TAG, "===== opening file : /sdcard/audiofile.raw ===== ");
  156.  
  157. DataInputStream dis = new DataInputStream(fin);
  158.  
  159. at.play();
  160. while((i = dis.read(temp, 0, bufferSize)) > -1)
  161. {
  162. at.write(temp, 0, i);
  163. }
  164.  
  165. Log.d(TAG, "===== Playing Audio Completed ===== ");
  166. at.stop();
  167. at.release();
  168. dis.close();
  169. fin.close();
  170.  
  171. } catch (FileNotFoundException e) {
  172. // TODO
  173. e.printStackTrace();
  174. } catch (IOException e) {
  175. // TODO
  176. e.printStackTrace();
  177. }
  178. }
  179.  
  180.  
  181. private View.OnClickListener btnClick = new View.OnClickListener() {
  182. @Override
  183. public void onClick(View v) {
  184. switch(v.getId()){
  185. case R.id.btnStartRec:{
  186. Log.d(TAG, "Start Recording");
  187. enableButton(R.id.btnStartRec,false);
  188. enableButton(R.id.btnStopRec,true);
  189. startRecording();
  190. enableButton(R.id.btnStopPlay,false);
  191. break;
  192. }
  193. case R.id.btnStopRec:{
  194. Log.d(TAG, "Stop Recording");
  195. enableButton(R.id.btnStartRec,true);
  196. enableButton(R.id.btnStopRec,false);
  197. stopRecording();
  198. enableButton(R.id.btnStartPlay,true);
  199. break;
  200. }
  201. case R.id.btnStartPlay:{
  202. Log.d(TAG, "Play Recording");
  203. enableButton(R.id.btnStartRec,false);
  204. enableButton(R.id.btnStopRec,false);
  205. StartPlaying();
  206. enableButton(R.id.btnStartPlay,false);
  207. enableButton(R.id.btnStopPlay,true);
  208. break;
  209. }
  210.  
  211. case R.id.btnStopPlay:{
  212. Log.d(TAG, "Stop Playing");
  213. //StopPlaying();
  214. enableButton(R.id.btnStartPlay,true);
  215. enableButton(R.id.btnStopPlay,false);
  216. enableButton(R.id.btnStartRec,true);
  217. break;
  218. }
  219. }
  220. }
  221. };
  222.  
  223. /* Assign OnClickListener to Buttons */
  224. private void setButtonHandlers() {
  225. ((Button)findViewById(R.id.btnStartRec)).setOnClickListener(btnClick);
  226. ((Button)findViewById(R.id.btnStopRec)).setOnClickListener(btnClick);
  227. ((Button)findViewById(R.id.btnStartPlay)).setOnClickListener(btnClick);
  228. ((Button)findViewById(R.id.btnStopPlay)).setOnClickListener(btnClick);
  229. }
  230.  
  231. /* Function to Enable/Disable Buttons */
  232. private void enableButton(int id,boolean isEnable){
  233. ((Button)findViewById(id)).setEnabled(isEnable);
  234. }
  235.  
  236.  
  237.  
  238. /*private String getTempFilename(){
  239. String filepath = Environment.getExternalStorageDirectory().getPath();
  240. File file = new File(filepath,AUDIO_RECORDER_FOLDER);
  241.  
  242. if(!file.exists()){
  243. file.mkdirs();
  244. }
  245.  
  246. File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
  247.  
  248. //if(tempFile.exists())
  249. // tempFile.delete();
  250.  
  251. raw_filename = file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE;
  252. Log.d(TAG, "================= Temp File Name : "+file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE+"=================");
  253. return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
  254. }
  255. */
  256. @Override
  257. public boolean onCreateOptionsMenu(Menu menu) {
  258.  
  259. // Inflate the menu; this adds items to the action bar if it is present.
  260. getMenuInflater().inflate(R.menu.main, menu);
  261. return true;
  262. }
  263.  
  264. @Override
  265. public boolean onOptionsItemSelected(MenuItem item) {
  266. // Handle action bar item clicks here. The action bar will
  267. // automatically handle clicks on the Home/Up button, so long
  268. // as you specify a parent activity in AndroidManifest.xml.
  269. int id = item.getItemId();
  270. if (id == R.id.action_settings) {
  271. return true;
  272. }
  273. return super.onOptionsItemSelected(item);
  274. }
  275.  
  276. /**
  277. * A placeholder fragment containing a simple view.
  278. */
  279. public static class PlaceholderFragment extends Fragment {
  280.  
  281. public PlaceholderFragment() {
  282. }
  283.  
  284. @Override
  285. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  286. Bundle savedInstanceState) {
  287. View rootView = inflater.inflate(R.layout.fragment_main, container,
  288. false);
  289. return rootView;
  290. }
  291. }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement