如何在.wav文件中写入bytearrayoutput

8ftvxx2r  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(259)

我正在尝试将音频数据写入bytearray,当单击按钮时,我正在尝试将数据写入.wav文件,但即使我将数据写入bytearray,文件仍无法创建。下面是我使用的代码,我可以直接将文件写入.wav文件

  1. public class Wavrecordernew {
  2. private static final int RECORDER_BPP = 16;
  3. private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
  4. private static final String AUDIO_RECORDER_FOLDER = "Automator";
  5. private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
  6. private static final int RECORDER_SAMPLERATE = 44100;
  7. private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
  8. private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
  9. private static AudioRecord recorder = null;
  10. private static int bufferSize = 0;
  11. //bufferSize *= 2;
  12. private static Thread recordingThread = null;
  13. private static boolean isRecording = false;
  14. private static String filenme, wavpath;
  15. private static ByteArrayOutputStream mainBuffer = new ByteArrayOutputStream();
  16. // bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT);
  17. static String getFilename() {
  18. String filepath = wavpath;
  19. File file = new File(filepath, AUDIO_RECORDER_FOLDER);
  20. if (!file.exists()) {
  21. file.mkdirs();
  22. }
  23. return (file.getAbsolutePath() + "/" + filenme);
  24. }
  25. static String getTempFilename() {
  26. String filepath = wavpath;
  27. File file = new File(filepath, AUDIO_RECORDER_FOLDER);
  28. if (!file.exists()) {
  29. file.mkdirs();
  30. }
  31. File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE);
  32. if (tempFile.exists())
  33. tempFile.delete();
  34. return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
  35. }
  36. // audio recorded file name and path passed from main activity
  37. public static void startRecording(String filepathfrmdis, String filenamefrmdis) {
  38. bufferSize = (AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT));
  39. filenme = filenamefrmdis;
  40. wavpath = filepathfrmdis;
  41. recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
  42. int i = recorder.getState();
  43. if (i == 1)
  44. recorder.startRecording();
  45. isRecording = true;
  46. recordingThread = new Thread(new Runnable() {
  47. @Override
  48. public void run() {
  49. writeAudioDataToFile();
  50. adddatatorecorder();
  51. }
  52. }, "AudioRecorder Thread");
  53. recordingThread.start();
  54. new java.util.Timer().schedule(
  55. new java.util.TimerTask() {
  56. @Override
  57. public void run() {
  58. stopRecording();
  59. }
  60. },
  61. 9000
  62. );
  63. }
  64. private static void writeAudioDataToFile() {
  65. byte data[] = new byte[bufferSize];
  66. Log.e("addbuffer", "" + bufferSize);
  67. String filename = getTempFilename();
  68. FileOutputStream os = null;
  69. try {
  70. os = new FileOutputStream(filename);
  71. } catch (FileNotFoundException e) {
  72. e.printStackTrace();
  73. }
  74. int read = 0;
  75. if (null != os) {
  76. while (isRecording) {
  77. read = recorder.read(data, 0, bufferSize);
  78. if (AudioRecord.ERROR_INVALID_OPERATION != read) {
  79. try {
  80. os.write(data);
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. try {
  87. os.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. public static void adddatatorecorder() {
  94. int i;
  95. byte[] bArr = new byte[bufferSize];
  96. do {
  97. try {
  98. i = recorder.read(bArr, 0, bufferSize);
  99. try {
  100. mainBuffer.write(bArr, 0, i);
  101. Log.e("addbuffer", "" + mainBuffer.size());
  102. } catch (Exception unused) {
  103. }
  104. } catch (Exception unused2) {
  105. i = 0;
  106. }
  107. } while (i >= bufferSize);
  108. }
  109. public static void stopRecording() {
  110. if (null != recorder) {
  111. isRecording = false;
  112. int i = recorder.getState();
  113. if (i == 1)
  114. recorder.stop();
  115. recorder.release();
  116. recorder = null;
  117. recordingThread = null;
  118. }
  119. copyWaveFile(getTempFilename(), getFilename());
  120. deleteTempFile();
  121. }
  122. // if there is an existing file with same path and file name it will delete that temp file
  123. static void deleteTempFile() {
  124. File file = new File(getTempFilename());
  125. file.delete();
  126. }
  127. private static void copyWaveFile(String inFilename, String outFilename) {
  128. FileInputStream in = null;
  129. FileOutputStream out = null;
  130. long totalAudioLen = 0;
  131. long totalDataLen = totalAudioLen + 36;
  132. long longSampleRate = RECORDER_SAMPLERATE;
  133. int channels = 2;
  134. long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
  135. byte[] data = new byte[bufferSize];
  136. try {
  137. in = new FileInputStream(inFilename);
  138. out = new FileOutputStream(outFilename);
  139. totalAudioLen = in.getChannel().size();
  140. totalDataLen = totalAudioLen + 36;
  141. //AppLog.logString("File size: " + totalDataLen);
  142. WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
  143. longSampleRate, channels, byteRate);
  144. while (in.read(data) != -1) {
  145. out.write(data);
  146. }
  147. in.close();
  148. out.close();
  149. } catch (FileNotFoundException e) {
  150. e.printStackTrace();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. // writing data from bytearray to .wav file
  156. public static void copyWaveFilefrombytearr(String inFilename, String outFilename) {
  157. FileInputStream in = null;
  158. FileOutputStream out = null;
  159. long totalAudioLen = 0;
  160. long totalDataLen = totalAudioLen + 36;
  161. long longSampleRate = RECORDER_SAMPLERATE;
  162. int channels = 2;
  163. long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
  164. try {
  165. in = new FileInputStream(inFilename);
  166. out = new FileOutputStream(outFilename);
  167. totalAudioLen = in.getChannel().size();
  168. totalDataLen = totalAudioLen + 36;
  169. //AppLog.logString("File size: " + totalDataLen);
  170. WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
  171. longSampleRate, channels, byteRate);
  172. mainBuffer.writeTo(out);
  173. in.close();
  174. out.close();
  175. } catch (FileNotFoundException e) {
  176. e.printStackTrace();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. //Wav file header required to set the 44byte for wav file
  182. private static void WriteWaveFileHeader(
  183. FileOutputStream out, long totalAudioLen,
  184. long totalDataLen, long longSampleRate, int channels,
  185. long byteRate) throws IOException {
  186. byte[] header = new byte[44];
  187. header[0] = 'R'; // RIFF/WAVE header
  188. header[1] = 'I';
  189. header[2] = 'F';
  190. header[3] = 'F';
  191. header[4] = (byte) (totalDataLen & 0xff);
  192. header[5] = (byte) ((totalDataLen >> 8) & 0xff);
  193. header[6] = (byte) ((totalDataLen >> 16) & 0xff);
  194. header[7] = (byte) ((totalDataLen >> 24) & 0xff);
  195. header[8] = 'W';
  196. header[9] = 'A';
  197. header[10] = 'V';
  198. header[11] = 'E';
  199. header[12] = 'f'; // 'fmt ' chunk
  200. header[13] = 'm';
  201. header[14] = 't';
  202. header[15] = ' ';
  203. header[16] = 16; // 4 bytes: size of 'fmt ' chunk
  204. header[17] = 0;
  205. header[18] = 0;
  206. header[19] = 0;
  207. header[20] = 1; // format = 1
  208. header[21] = 0;
  209. header[22] = (byte) channels;
  210. header[23] = 0;
  211. header[24] = (byte) (longSampleRate & 0xff);
  212. header[25] = (byte) ((longSampleRate >> 8) & 0xff);
  213. header[26] = (byte) ((longSampleRate >> 16) & 0xff);
  214. header[27] = (byte) ((longSampleRate >> 24) & 0xff);
  215. header[28] = (byte) (byteRate & 0xff);
  216. header[29] = (byte) ((byteRate >> 8) & 0xff);
  217. header[30] = (byte) ((byteRate >> 16) & 0xff);
  218. header[31] = (byte) ((byteRate >> 24) & 0xff);
  219. header[32] = (byte) (2 * 16 / 8); // block align
  220. header[33] = 0;
  221. header[34] = RECORDER_BPP; // bits per sample
  222. header[35] = 0;
  223. header[36] = 'd';
  224. header[37] = 'a';
  225. header[38] = 't';
  226. header[39] = 'a';
  227. header[40] = (byte) (totalAudioLen & 0xff);
  228. header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
  229. header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
  230. header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
  231. out.write(header, 0, 44);
  232. }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题