I created an application that records audio, saves a sample to an SD card, then plays it using the record and play buttons. I need to cancel this sample. I can do all this, and the reverse sample is saved on the SD card under a different name. The original sample is test.wav , and the same sample that was canceled is saved as revFile.wav . when I try to play revFile.wav , the android says it cannot play this format.
I put the sample in an array and then changed the contents, something tells me that there may be header information at the beginning of the sample, which first requires a strip, any ideas. thanks.
Here is what I still have.
public class recorder extends Activity { MediaRecorder myRecorder = null; DataInputStream dis = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClickPlay(View v){ Log.v("onClickplay", "play clicked"); try{ MediaPlayer mp = new MediaPlayer(); mp.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/test.wav"); mp.prepare(); mp.start(); } catch(Exception e3) { e3.printStackTrace(); } TextView text = (TextView)findViewById(R.id.TextView01); text.setText("playing"); } public void onClickRecord(View v){ Log.v("onClickRecord", "record clicked"); File path = Environment.getExternalStorageDirectory(); Log.v("file path", ""+path.getAbsolutePath()); File file = new File(path, "test.wav"); if(file.exists()){ file.delete(); } path.mkdirs(); Log.v("file path", ""+file.getAbsolutePath()); myRecorder = new MediaRecorder(); myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); myRecorder.setOutputFile(file.getAbsolutePath()); Log.i("myrecorder", "about to prepare recording"); try{ myRecorder.prepare(); } catch(Exception e) { e.printStackTrace(); } Log.i("myrecorder", "prepared"); myRecorder.start(); // Recording is now started Log.i("myrecorder", "recording"); TextView text = (TextView)findViewById(R.id.TextView01); text.setText("recording"); } public void onClickStop(View v){ Log.v("onClickStop", "stop clicked"); try{ myRecorder.stop(); myRecorder.reset(); // You can reuse the object by going back to setAudioSource() step myRecorder.release(); // Now the object cannot be reused }catch(Exception e){} TextView text = (TextView)findViewById(R.id.TextView01); text.setText("recording stopped"); } public void onClickReverse(View v){ Log.v("onClickReverse", "reverse clicked"); File f = Environment.getExternalStorageDirectory(); String path = f.getAbsolutePath(); path = path + "/test.wav"; Log.v("path = ", ""+path); Log.v("dir = ", ""+f.getAbsolutePath()); Log.v("test file exists? = ", ""+f.getAbsolutePath()+"/test.wav"); File f2 = new File(path); Log.v("f2 = ", ""+f2.getAbsolutePath()); try { InputStream is = new FileInputStream(f2); BufferedInputStream bis = new BufferedInputStream(is); dis = new DataInputStream(bis); } catch (Exception e) { e.printStackTrace(); } int fileLength = (int)f2.length(); byte[] buffer = new byte[fileLength]; /*File reversedFile = Environment.getExternalStorageDirectory(); File revFile = new File(reversedFile, "reversedFile.wav"); Log.v("reversedfile path", ""+ revFile.getAbsolutePath()); if(revFile.exists()){ revFile.delete(); } reversedFile.mkdirs(); */ byte[] byteArray = new byte[fileLength +1]; Log.v("bytearray size = ", ""+byteArray.length); try { while(dis.read(buffer) != -1 ) { dis.read(buffer); Log.v("about to read buffer", "buffer"); byteArray = buffer; } Log.v(" buffer size = ", ""+ buffer.length); } catch (IOException e) { e.printStackTrace(); } byte[] tempArray = new byte[fileLength]; int j=0; for (int i=byteArray.length-1; i >=0; i--) { tempArray[ j++ ] = byteArray[i]; } File revPath = Environment.getExternalStorageDirectory(); Log.v("revpath path", ""+revPath.getAbsolutePath()); File revFile = new File(revPath, "revFile.wav"); Log.v("revfile path ", ""+revFile.getAbsolutePath()); if(revFile.exists()){ revFile.delete(); } revPath.mkdirs(); try { OutputStream os = new FileOutputStream(revFile); BufferedOutputStream bos = new BufferedOutputStream(os); DataOutputStream dos = new DataOutputStream(bos); Log.v("temparray size = ", ""+ tempArray.length); dos.write(tempArray); dos.flush(); dos.close(); } catch (Exception e) { e.printStackTrace(); } try{ MediaPlayer mp = new MediaPlayer(); mp.setDataSource(Environment.getExternalStorageDirectory().getPath() +"/revFile.wav"); mp.prepare(); mp.start(); } catch(Exception e3) { e3.printStackTrace(); } TextView text = (TextView)findViewById(R.id.TextView01); text.setText("playing reversed file"); } }// end of onclickrev
source share