From 78b3bc1acd8b19b76527754d8ffb94acf936dc1e Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 12:31:56 +0800 Subject: [PATCH 01/10] modify audio Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 4 + .../ohos/samples/audio/utils/ShellUtils.java | 141 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index 8454c7bbfa..ef158f03a2 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -33,6 +33,7 @@ import ohos.media.audio.AudioCapturerInfo; import ohos.media.audio.AudioDeviceDescriptor; import ohos.media.audio.AudioManager; import ohos.media.audio.AudioStreamInfo; +import ohos.samples.audio.utils.ShellUtils; import java.io.File; import java.io.FileOutputStream; @@ -127,6 +128,9 @@ public class AudioRecorderSlice extends AbilitySlice { recordButton.setText("Start"); showTips("Stop record"); pathText.setText("Path:" + getFilesDir() + File.separator + "record.pcm"); + String wavPath = getFilesDir() + File.separator + "record.wav"; + ShellUtils.tranPcmToWavFile(file, wavPath); + pathText.setText("Path:" + getFilesDir() + File.separator + "record.wav"); } } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java new file mode 100644 index 0000000000..a9daf4f5f6 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -0,0 +1,141 @@ +package ohos.samples.audio.utils; + +import java.io.*; +/** + * ShellUtils + * + * @since 2021-08-27 + */ +public class ShellUtils { + /** + * tranPcmToWavFile + * @param pcmFile File + * @param wavPath String + */ + public static void tranPcmToWavFile(File pcmFile, String wavPath) { + FileInputStream fis = null; + FileOutputStream fos = null; + try { + fis = new FileInputStream(pcmFile); + fos = new FileOutputStream(wavPath); + + int PCMSize = 0; + byte[] buf = new byte[1024 * 4]; + int size = fis.read(buf); + while (size != -1) { + PCMSize += size; + size = fis.read(buf); + } + fis.close(); + + //填入参数,比特率等等。这里用的是16位双声道 44100 hz + WaveHeader header = new WaveHeader(); + //长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节) + header.fileLength = PCMSize + (44 - 8); + header.FmtHdrLeth = 16; + header.BitsPerSample = 16; + header.Channels = 2; + header.FormatTag = 0x0001; + header.SamplesPerSec = 44100; + header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8); + header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec; + header.DataHdrLeth = PCMSize; + + byte[] h = header.getHeader(); + + assert h.length == 44; //WAV标准,头部应该是44字节 + //write header + fos.write(h, 0, h.length); + //write data stream + fis = new FileInputStream(pcmFile); + size = fis.read(buf); + while (size != -1) { + fos.write(buf, 0, size); + size = fis.read(buf); + } + fis.close(); + fos.close(); + pcmFile.delete(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * WaveHeader + */ + static class WaveHeader { + public final char[] fileID = {'R', 'I', 'F', 'F'}; + public int fileLength; + public char[] wavTag = {'W', 'A', 'V', 'E'}; + public char[] FmtHdrID = {'f', 'm', 't', ' '}; + public int FmtHdrLeth; + public short FormatTag; + public short Channels; + public int SamplesPerSec; + public int AvgBytesPerSec; + public short BlockAlign; + public short BitsPerSample; + public char[] DataHdrID = {'d', 'a', 't', 'a'}; + public int DataHdrLeth; + + public byte[] getHeader() throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + WriteChar(bos, fileID); + WriteInt(bos, fileLength); + WriteChar(bos, wavTag); + WriteChar(bos, FmtHdrID); + WriteInt(bos, FmtHdrLeth); + WriteShort(bos, FormatTag); + WriteShort(bos, Channels); + WriteInt(bos, SamplesPerSec); + WriteInt(bos, AvgBytesPerSec); + WriteShort(bos, BlockAlign); + WriteShort(bos, BitsPerSample); + WriteChar(bos, DataHdrID); + WriteInt(bos, DataHdrLeth); + bos.flush(); + byte[] r = bos.toByteArray(); + bos.close(); + return r; + } + + private void WriteShort(ByteArrayOutputStream bos, int s) throws IOException { + byte[] mybyte = new byte[2]; + mybyte[1] = (byte) ((s << 16) >> 24); + mybyte[0] = (byte) ((s << 24) >> 24); + bos.write(mybyte); + } + + + private void WriteInt(ByteArrayOutputStream bos, int n) throws IOException { + byte[] buf = new byte[4]; + buf[3] = (byte) (n >> 24); + buf[2] = (byte) ((n << 8) >> 24); + buf[1] = (byte) ((n << 16) >> 24); + buf[0] = (byte) ((n << 24) >> 24); + bos.write(buf); + } + + private void WriteChar(ByteArrayOutputStream bos, char[] id) { + for (char c : id) { + bos.write(c); + } + } + } +} -- Gitee From bc4af2b0c5792f32c38eaaebe4ef972ba70a33ba Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 12:41:03 +0800 Subject: [PATCH 02/10] Audio Signed-off-by: guojin26 --- .../ohos/samples/audio/utils/ShellUtils.java | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index a9daf4f5f6..2bb76966a2 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -1,6 +1,26 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package ohos.samples.audio.utils; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + /** * ShellUtils * @@ -28,9 +48,10 @@ public class ShellUtils { } fis.close(); - //填入参数,比特率等等。这里用的是16位双声道 44100 hz + // 填入参数,比特率等等。这里用的是16位双声道 44100 hz WaveHeader header = new WaveHeader(); - //长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节) + + // 长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节) header.fileLength = PCMSize + (44 - 8); header.FmtHdrLeth = 16; header.BitsPerSample = 16; @@ -40,12 +61,10 @@ public class ShellUtils { header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8); header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec; header.DataHdrLeth = PCMSize; - byte[] h = header.getHeader(); + assert h.length == 44; // WAV标准,头部应该是44字节 + fos.write(h, 0, h.length);// write header - assert h.length == 44; //WAV标准,头部应该是44字节 - //write header - fos.write(h, 0, h.length); //write data stream fis = new FileInputStream(pcmFile); size = fis.read(buf); @@ -96,26 +115,26 @@ public class ShellUtils { public byte[] getHeader() throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - WriteChar(bos, fileID); - WriteInt(bos, fileLength); - WriteChar(bos, wavTag); - WriteChar(bos, FmtHdrID); - WriteInt(bos, FmtHdrLeth); - WriteShort(bos, FormatTag); - WriteShort(bos, Channels); - WriteInt(bos, SamplesPerSec); - WriteInt(bos, AvgBytesPerSec); - WriteShort(bos, BlockAlign); - WriteShort(bos, BitsPerSample); - WriteChar(bos, DataHdrID); - WriteInt(bos, DataHdrLeth); + writeChar(bos, fileID); + writeInt(bos, fileLength); + writeChar(bos, wavTag); + writeChar(bos, FmtHdrID); + writeInt(bos, FmtHdrLeth); + writeShort(bos, FormatTag); + writeShort(bos, Channels); + writeInt(bos, SamplesPerSec); + writeInt(bos, AvgBytesPerSec); + writeShort(bos, BlockAlign); + writeShort(bos, BitsPerSample); + writeChar(bos, DataHdrID); + writeInt(bos, DataHdrLeth); bos.flush(); byte[] r = bos.toByteArray(); bos.close(); return r; } - private void WriteShort(ByteArrayOutputStream bos, int s) throws IOException { + private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { byte[] mybyte = new byte[2]; mybyte[1] = (byte) ((s << 16) >> 24); mybyte[0] = (byte) ((s << 24) >> 24); @@ -123,7 +142,7 @@ public class ShellUtils { } - private void WriteInt(ByteArrayOutputStream bos, int n) throws IOException { + private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { byte[] buf = new byte[4]; buf[3] = (byte) (n >> 24); buf[2] = (byte) ((n << 8) >> 24); @@ -132,7 +151,7 @@ public class ShellUtils { bos.write(buf); } - private void WriteChar(ByteArrayOutputStream bos, char[] id) { + private void writeChar(ByteArrayOutputStream bos, char[] id) { for (char c : id) { bos.write(c); } -- Gitee From 61976f656941af2ed8dc0cc6217de9ae6e041a44 Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 12:44:35 +0800 Subject: [PATCH 03/10] Audio Signed-off-by: guojin26 --- .../src/main/java/ohos/samples/audio/utils/ShellUtils.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index 2bb76966a2..8a17ab0786 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -15,10 +15,10 @@ package ohos.samples.audio.utils; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; /** @@ -63,9 +63,9 @@ public class ShellUtils { header.DataHdrLeth = PCMSize; byte[] h = header.getHeader(); assert h.length == 44; // WAV标准,头部应该是44字节 - fos.write(h, 0, h.length);// write header + fos.write(h, 0, h.length); // write header - //write data stream + // write data stream fis = new FileInputStream(pcmFile); size = fis.read(buf); while (size != -1) { -- Gitee From af3d7dae597493cf17fd162d76ebab7c414323e8 Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 13:06:22 +0800 Subject: [PATCH 04/10] update Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 9 ++--- .../ohos/samples/audio/utils/HiLogUtils.java | 35 +++++++++++++++++++ .../ohos/samples/audio/utils/ShellUtils.java | 34 ++++++++++++------ 3 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index ef158f03a2..e2ead9c384 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -24,8 +24,6 @@ import ohos.agp.components.Component; import ohos.agp.components.Text; import ohos.agp.window.dialog.ToastDialog; import ohos.app.dispatcher.task.TaskPriority; -import ohos.hiviewdfx.HiLog; -import ohos.hiviewdfx.HiLogLabel; import ohos.media.audio.AudioCapturer; import ohos.media.audio.AudioCapturerCallback; import ohos.media.audio.AudioCapturerConfig; @@ -33,6 +31,7 @@ import ohos.media.audio.AudioCapturerInfo; import ohos.media.audio.AudioDeviceDescriptor; import ohos.media.audio.AudioManager; import ohos.media.audio.AudioStreamInfo; +import ohos.samples.audio.utils.HiLogUtils; import ohos.samples.audio.utils.ShellUtils; import java.io.File; @@ -46,8 +45,6 @@ import java.util.List; public class AudioRecorderSlice extends AbilitySlice { private static final String TAG = AudioRecorderSlice.class.getName(); - private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); - private static final int SAMPLE_RATE = 44100; private static final int BUFFER_SIZE = 1024; @@ -67,7 +64,7 @@ public class AudioRecorderSlice extends AbilitySlice { private final AudioCapturerCallback callback = new AudioCapturerCallback() { @Override public void onCapturerConfigChanged(List configs) { - HiLog.info(LABEL_LOG, "%{public}s", "on capturer config changed"); + HiLogUtils.info(TAG,"on capturer config changed"); } }; @@ -154,7 +151,7 @@ public class AudioRecorderSlice extends AbilitySlice { outputStream.flush(); } } catch (IOException exception) { - HiLog.error(LABEL_LOG, "%{public}s", "record exception"); + HiLogUtils.error(TAG,"record exception," + exception.getMessage()); } }); } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java new file mode 100644 index 0000000000..44a359fef0 --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java @@ -0,0 +1,35 @@ +package ohos.samples.audio.utils; + +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +import java.util.Objects; + +public class HiLogUtils { + private static final String APP_NAME = "Audio"; + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, APP_NAME); + public static final String LOG_FORMAT = "%{public}s:%{public}s"; + + public static void info(String className, String msg){ + HiLog.info(LABEL_LOG, LOG_FORMAT,getLogPrefix(className),msg); + } + + public static void error(String className, String msg){ + HiLog.error(LABEL_LOG, LOG_FORMAT,getLogPrefix(className),msg); + } + + private static String getLogPrefix(String tag){ + Thread currentThread = Thread.currentThread(); + String threadName = currentThread.getName(); + StackTraceElement[] stackTraceElements = currentThread.getStackTrace(); + int lineNum = 0; + for(int index = 0;index < stackTraceElements.length; index++) { + if(Objects.equals(stackTraceElements[index].getClassName(),HiLogUtils.class.getName()) + && stackTraceElements.length>index+1 + && !Objects.equals(stackTraceElements[index+1].getClassName(),HiLogUtils.class.getName())) { + lineNum = stackTraceElements[index+1].getLineNumber(); + } + } + return "[" + threadName + "]" + "(" + tag + ".java" + lineNum + ")"; + } +} diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index 8a17ab0786..54479398e7 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -27,6 +27,18 @@ import java.io.IOException; * @since 2021-08-27 */ public class ShellUtils { + private static final String TAG = ShellUtils.class.getName(); + + private static final int SAMPLE_RATE = 44100; + + private static final int BUFFER_SIZE = 1024*4; + + private static final int CHANNEL = 2; + + private static final int BITS_PRE_SAMPLE = 16; + + private static final int BIT = 8; + /** * tranPcmToWavFile * @param pcmFile File @@ -40,7 +52,7 @@ public class ShellUtils { fos = new FileOutputStream(wavPath); int PCMSize = 0; - byte[] buf = new byte[1024 * 4]; + byte[] buf = new byte[BUFFER_SIZE]; int size = fis.read(buf); while (size != -1) { PCMSize += size; @@ -53,12 +65,12 @@ public class ShellUtils { // 长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节) header.fileLength = PCMSize + (44 - 8); - header.FmtHdrLeth = 16; - header.BitsPerSample = 16; - header.Channels = 2; + header.FmtHdrLeth = BITS_PRE_SAMPLE; + header.BitsPerSample = BITS_PRE_SAMPLE; + header.Channels = CHANNEL; header.FormatTag = 0x0001; - header.SamplesPerSec = 44100; - header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8); + header.SamplesPerSec = SAMPLE_RATE; + header.BlockAlign = (short) (header.Channels * header.BitsPerSample / BIT); header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec; header.DataHdrLeth = PCMSize; byte[] h = header.getHeader(); @@ -74,22 +86,24 @@ public class ShellUtils { } fis.close(); fos.close(); - pcmFile.delete(); + if (pcmFile.delete()) { + HiLogUtils.error(TAG,"record exception"); + } } catch (Exception e) { - e.printStackTrace(); + HiLogUtils.error(TAG,e.getMessage()); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { - e.printStackTrace(); + HiLogUtils.error(TAG,e.getMessage()); } } if (fos != null) { try { fos.close(); } catch (IOException e) { - e.printStackTrace(); + HiLogUtils.error(TAG,e.getMessage()); } } } -- Gitee From 2b5267e54b6b3d5872896d6df600dee1b1f7965d Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 13:07:52 +0800 Subject: [PATCH 05/10] Audio Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 6 +++--- .../ohos/samples/audio/utils/HiLogUtils.java | 20 +++++++++---------- .../ohos/samples/audio/utils/ShellUtils.java | 11 +++++----- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index e2ead9c384..f49c33c2be 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -64,7 +64,7 @@ public class AudioRecorderSlice extends AbilitySlice { private final AudioCapturerCallback callback = new AudioCapturerCallback() { @Override public void onCapturerConfigChanged(List configs) { - HiLogUtils.info(TAG,"on capturer config changed"); + HiLogUtils.info(TAG, "on capturer config changed"); } }; @@ -150,8 +150,8 @@ public class AudioRecorderSlice extends AbilitySlice { bytes = new byte[BUFFER_SIZE]; outputStream.flush(); } - } catch (IOException exception) { - HiLogUtils.error(TAG,"record exception," + exception.getMessage()); + } catch (IOException exception) { + HiLogUtils.error(TAG, "record exception," + exception.getMessage()); } }); } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java index 44a359fef0..ccb6389c1b 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java @@ -10,24 +10,24 @@ public class HiLogUtils { private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, APP_NAME); public static final String LOG_FORMAT = "%{public}s:%{public}s"; - public static void info(String className, String msg){ - HiLog.info(LABEL_LOG, LOG_FORMAT,getLogPrefix(className),msg); + public static void info(String className, String msg) { + HiLog.info(LABEL_LOG, LOG_FORMAT, getLogPrefix(className), msg); } - public static void error(String className, String msg){ - HiLog.error(LABEL_LOG, LOG_FORMAT,getLogPrefix(className),msg); + public static void error(String className, String msg) { + HiLog.error(LABEL_LOG, LOG_FORMAT, getLogPrefix(className), msg); } - private static String getLogPrefix(String tag){ + private static String getLogPrefix(String tag) { Thread currentThread = Thread.currentThread(); String threadName = currentThread.getName(); StackTraceElement[] stackTraceElements = currentThread.getStackTrace(); int lineNum = 0; - for(int index = 0;index < stackTraceElements.length; index++) { - if(Objects.equals(stackTraceElements[index].getClassName(),HiLogUtils.class.getName()) - && stackTraceElements.length>index+1 - && !Objects.equals(stackTraceElements[index+1].getClassName(),HiLogUtils.class.getName())) { - lineNum = stackTraceElements[index+1].getLineNumber(); + for (int index = 0; index < stackTraceElements.length; index++) { + if (Objects.equals(stackTraceElements[index].getClassName(), HiLogUtils.class.getName()) + && stackTraceElements.length > index + 1 + && !Objects.equals(stackTraceElements[index + 1].getClassName(), HiLogUtils.class.getName())) { + lineNum = stackTraceElements[index + 1].getLineNumber(); } } return "[" + threadName + "]" + "(" + tag + ".java" + lineNum + ")"; diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index 54479398e7..442d4e278d 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -31,7 +31,7 @@ public class ShellUtils { private static final int SAMPLE_RATE = 44100; - private static final int BUFFER_SIZE = 1024*4; + private static final int BUFFER_SIZE = 1024 * 4; private static final int CHANNEL = 2; @@ -41,6 +41,7 @@ public class ShellUtils { /** * tranPcmToWavFile + * * @param pcmFile File * @param wavPath String */ @@ -87,23 +88,23 @@ public class ShellUtils { fis.close(); fos.close(); if (pcmFile.delete()) { - HiLogUtils.error(TAG,"record exception"); + HiLogUtils.error(TAG, "record exception"); } } catch (Exception e) { - HiLogUtils.error(TAG,e.getMessage()); + HiLogUtils.error(TAG, e.getMessage()); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { - HiLogUtils.error(TAG,e.getMessage()); + HiLogUtils.error(TAG, e.getMessage()); } } if (fos != null) { try { fos.close(); } catch (IOException e) { - HiLogUtils.error(TAG,e.getMessage()); + HiLogUtils.error(TAG, e.getMessage()); } } } -- Gitee From eabe3ddf3b434bcc8ca84933899f4d1a492095db Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 14:22:31 +0800 Subject: [PATCH 06/10] audio Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 2 +- .../ohos/samples/audio/utils/HiLogUtils.java | 8 ++++ .../ohos/samples/audio/utils/ShellUtils.java | 48 ++++++++----------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index f49c33c2be..340cbd4434 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -40,7 +40,7 @@ import java.io.IOException; import java.util.List; /** - * AudioCapturer + * AudioRecorderSlice */ public class AudioRecorderSlice extends AbilitySlice { private static final String TAG = AudioRecorderSlice.class.getName(); diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java index ccb6389c1b..d4587ff607 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java @@ -5,11 +5,19 @@ import ohos.hiviewdfx.HiLogLabel; import java.util.Objects; +/** + * HiLogUtils + * + * @since 2021-08-27 + */ public class HiLogUtils { private static final String APP_NAME = "Audio"; private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, APP_NAME); public static final String LOG_FORMAT = "%{public}s:%{public}s"; + private HiLogUtils(){ + } + public static void info(String className, String msg) { HiLog.info(LABEL_LOG, LOG_FORMAT, getLogPrefix(className), msg); } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index 442d4e278d..c4a3737489 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -29,6 +29,8 @@ import java.io.IOException; public class ShellUtils { private static final String TAG = ShellUtils.class.getName(); + private static final int HEADER_SIZE = 44; + private static final int SAMPLE_RATE = 44100; private static final int BUFFER_SIZE = 1024 * 4; @@ -39,6 +41,11 @@ public class ShellUtils { private static final int BIT = 8; + private static final short FORMAT_TAG = 0x0001; + + private ShellUtils(){ + } + /** * tranPcmToWavFile * @@ -46,12 +53,8 @@ public class ShellUtils { * @param wavPath String */ public static void tranPcmToWavFile(File pcmFile, String wavPath) { - FileInputStream fis = null; - FileOutputStream fos = null; - try { - fis = new FileInputStream(pcmFile); - fos = new FileOutputStream(wavPath); - + try (FileInputStream fis = new FileInputStream(pcmFile)){ + FileOutputStream fos = new FileOutputStream(wavPath); int PCMSize = 0; byte[] buf = new byte[BUFFER_SIZE]; int size = fis.read(buf); @@ -65,48 +68,33 @@ public class ShellUtils { WaveHeader header = new WaveHeader(); // 长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节) - header.fileLength = PCMSize + (44 - 8); + header.fileLength = PCMSize + (HEADER_SIZE - BIT); header.FmtHdrLeth = BITS_PRE_SAMPLE; header.BitsPerSample = BITS_PRE_SAMPLE; header.Channels = CHANNEL; - header.FormatTag = 0x0001; + header.FormatTag = FORMAT_TAG; header.SamplesPerSec = SAMPLE_RATE; header.BlockAlign = (short) (header.Channels * header.BitsPerSample / BIT); header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec; header.DataHdrLeth = PCMSize; byte[] h = header.getHeader(); - assert h.length == 44; // WAV标准,头部应该是44字节 + assert h.length == HEADER_SIZE; // WAV标准,头部应该是44字节 fos.write(h, 0, h.length); // write header // write data stream - fis = new FileInputStream(pcmFile); - size = fis.read(buf); + FileInputStream file = new FileInputStream(pcmFile); + size = file.read(buf); while (size != -1) { fos.write(buf, 0, size); - size = fis.read(buf); + size = file.read(buf); } - fis.close(); + file.close(); fos.close(); if (pcmFile.delete()) { HiLogUtils.error(TAG, "record exception"); } - } catch (Exception e) { + } catch (IOException e) { HiLogUtils.error(TAG, e.getMessage()); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - HiLogUtils.error(TAG, e.getMessage()); - } - } - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - HiLogUtils.error(TAG, e.getMessage()); - } - } } } @@ -150,6 +138,7 @@ public class ShellUtils { } private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { + // write short byte[] mybyte = new byte[2]; mybyte[1] = (byte) ((s << 16) >> 24); mybyte[0] = (byte) ((s << 24) >> 24); @@ -158,6 +147,7 @@ public class ShellUtils { private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { + // write int byte[] buf = new byte[4]; buf[3] = (byte) (n >> 24); buf[2] = (byte) ((n << 8) >> 24); -- Gitee From 0d56f9dcc8778d7b1c475257a7952bef027f6242 Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 14:24:06 +0800 Subject: [PATCH 07/10] Audio Signed-off-by: guojin26 --- .../src/main/java/ohos/samples/audio/utils/HiLogUtils.java | 2 +- .../src/main/java/ohos/samples/audio/utils/ShellUtils.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java index d4587ff607..c63a17cadd 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/HiLogUtils.java @@ -15,7 +15,7 @@ public class HiLogUtils { private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, APP_NAME); public static final String LOG_FORMAT = "%{public}s:%{public}s"; - private HiLogUtils(){ + private HiLogUtils() { } public static void info(String className, String msg) { diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index c4a3737489..4dbad3ca33 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -43,7 +43,7 @@ public class ShellUtils { private static final short FORMAT_TAG = 0x0001; - private ShellUtils(){ + private ShellUtils() { } /** @@ -53,7 +53,7 @@ public class ShellUtils { * @param wavPath String */ public static void tranPcmToWavFile(File pcmFile, String wavPath) { - try (FileInputStream fis = new FileInputStream(pcmFile)){ + try (FileInputStream fis = new FileInputStream(pcmFile)) { FileOutputStream fos = new FileOutputStream(wavPath); int PCMSize = 0; byte[] buf = new byte[BUFFER_SIZE]; -- Gitee From c5ba6918022fb4b5d297551e2c74be13414899cb Mon Sep 17 00:00:00 2001 From: guojin26 Date: Fri, 27 Aug 2021 16:04:22 +0800 Subject: [PATCH 08/10] Audio Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 2 +- .../ohos/samples/audio/utils/ShellUtils.java | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index 340cbd4434..d4051dac8b 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -140,7 +140,7 @@ public class AudioRecorderSlice extends AbilitySlice { } } - private void runRecord() { + private void runRecord(){ getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> { file = new File(getFilesDir() + File.separator + "record.pcm"); try (FileOutputStream outputStream = new FileOutputStream(file)) { diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index 4dbad3ca33..c7bd2d22ae 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -43,6 +43,12 @@ public class ShellUtils { private static final short FORMAT_TAG = 0x0001; + private static final short BYTE_BIT = 24; + + private static final short SHORT_SIZE = 2; + + private static final short INT_SIZE = 4; + private ShellUtils() { } @@ -52,8 +58,9 @@ public class ShellUtils { * @param pcmFile File * @param wavPath String */ - public static void tranPcmToWavFile(File pcmFile, String wavPath) { - try (FileInputStream fis = new FileInputStream(pcmFile)) { + public static void tranPcmToWavFile(File pcmFile, String wavPath){ + try { + FileInputStream fis = new FileInputStream(pcmFile); FileOutputStream fos = new FileOutputStream(wavPath); int PCMSize = 0; byte[] buf = new byte[BUFFER_SIZE]; @@ -91,9 +98,9 @@ public class ShellUtils { file.close(); fos.close(); if (pcmFile.delete()) { - HiLogUtils.error(TAG, "record exception"); + HiLogUtils.info(TAG, "record.pcm delete"); } - } catch (IOException e) { + } catch (IOException e) { HiLogUtils.error(TAG, e.getMessage()); } } @@ -139,20 +146,20 @@ public class ShellUtils { private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { // write short - byte[] mybyte = new byte[2]; - mybyte[1] = (byte) ((s << 16) >> 24); - mybyte[0] = (byte) ((s << 24) >> 24); + byte[] mybyte = new byte[SHORT_SIZE]; + mybyte[1] = (byte) ((s << BITS_PRE_SAMPLE) >> BYTE_BIT); + mybyte[0] = (byte) ((s << BYTE_BIT) >> BYTE_BIT); bos.write(mybyte); } private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { // write int - byte[] buf = new byte[4]; - buf[3] = (byte) (n >> 24); - buf[2] = (byte) ((n << 8) >> 24); - buf[1] = (byte) ((n << 16) >> 24); - buf[0] = (byte) ((n << 24) >> 24); + byte[] buf = new byte[INT_SIZE]; + buf[3] = (byte) (n >> BYTE_BIT); + buf[2] = (byte) ((n << BIT) >> BYTE_BIT); + buf[1] = (byte) ((n << BITS_PRE_SAMPLE) >> BYTE_BIT); + buf[0] = (byte) ((n << BYTE_BIT) >> BYTE_BIT); bos.write(buf); } -- Gitee From b2d5482513e1164ac592d8d368cc8dfb46fc340b Mon Sep 17 00:00:00 2001 From: guojin26 Date: Tue, 31 Aug 2021 09:24:48 +0800 Subject: [PATCH 09/10] audio Signed-off-by: guojin26 --- .../audio/slice/AudioRecorderSlice.java | 16 ++-- .../ohos/samples/audio/utils/ShellUtils.java | 75 +-------------- .../ohos/samples/audio/utils/WaveHeader.java | 96 +++++++++++++++++++ 3 files changed, 105 insertions(+), 82 deletions(-) create mode 100644 media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java index d4051dac8b..8293080efd 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/slice/AudioRecorderSlice.java @@ -79,7 +79,13 @@ public class AudioRecorderSlice extends AbilitySlice { private void initComponents() { recordButton = (Button) findComponentById(ResourceTable.Id_main_start_recoding); pathText = (Text) findComponentById(ResourceTable.Id_path_text); - recordButton.setClickedListener(this::record); + recordButton.setClickedListener((Component component)->{ + if (isRecording && audioCapturer != null) { + stopRecord(); + return; + } + startRecord(); + }); } private void initRecord() { @@ -111,14 +117,6 @@ public class AudioRecorderSlice extends AbilitySlice { audioManager = null; } - private void record(Component component) { - if (isRecording && audioCapturer != null) { - stopRecord(); - return; - } - startRecord(); - } - private void stopRecord() { if (audioCapturer.stop()) { isRecording = false; diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index c7bd2d22ae..b925f1519e 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -15,7 +15,6 @@ package ohos.samples.audio.utils; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -43,12 +42,6 @@ public class ShellUtils { private static final short FORMAT_TAG = 0x0001; - private static final short BYTE_BIT = 24; - - private static final short SHORT_SIZE = 2; - - private static final short INT_SIZE = 4; - private ShellUtils() { } @@ -58,7 +51,7 @@ public class ShellUtils { * @param pcmFile File * @param wavPath String */ - public static void tranPcmToWavFile(File pcmFile, String wavPath){ + public static void tranPcmToWavFile(File pcmFile, String wavPath) { try { FileInputStream fis = new FileInputStream(pcmFile); FileOutputStream fos = new FileOutputStream(wavPath); @@ -97,6 +90,7 @@ public class ShellUtils { } file.close(); fos.close(); + if (pcmFile.delete()) { HiLogUtils.info(TAG, "record.pcm delete"); } @@ -104,69 +98,4 @@ public class ShellUtils { HiLogUtils.error(TAG, e.getMessage()); } } - - /** - * WaveHeader - */ - static class WaveHeader { - public final char[] fileID = {'R', 'I', 'F', 'F'}; - public int fileLength; - public char[] wavTag = {'W', 'A', 'V', 'E'}; - public char[] FmtHdrID = {'f', 'm', 't', ' '}; - public int FmtHdrLeth; - public short FormatTag; - public short Channels; - public int SamplesPerSec; - public int AvgBytesPerSec; - public short BlockAlign; - public short BitsPerSample; - public char[] DataHdrID = {'d', 'a', 't', 'a'}; - public int DataHdrLeth; - - public byte[] getHeader() throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - writeChar(bos, fileID); - writeInt(bos, fileLength); - writeChar(bos, wavTag); - writeChar(bos, FmtHdrID); - writeInt(bos, FmtHdrLeth); - writeShort(bos, FormatTag); - writeShort(bos, Channels); - writeInt(bos, SamplesPerSec); - writeInt(bos, AvgBytesPerSec); - writeShort(bos, BlockAlign); - writeShort(bos, BitsPerSample); - writeChar(bos, DataHdrID); - writeInt(bos, DataHdrLeth); - bos.flush(); - byte[] r = bos.toByteArray(); - bos.close(); - return r; - } - - private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { - // write short - byte[] mybyte = new byte[SHORT_SIZE]; - mybyte[1] = (byte) ((s << BITS_PRE_SAMPLE) >> BYTE_BIT); - mybyte[0] = (byte) ((s << BYTE_BIT) >> BYTE_BIT); - bos.write(mybyte); - } - - - private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { - // write int - byte[] buf = new byte[INT_SIZE]; - buf[3] = (byte) (n >> BYTE_BIT); - buf[2] = (byte) ((n << BIT) >> BYTE_BIT); - buf[1] = (byte) ((n << BITS_PRE_SAMPLE) >> BYTE_BIT); - buf[0] = (byte) ((n << BYTE_BIT) >> BYTE_BIT); - bos.write(buf); - } - - private void writeChar(ByteArrayOutputStream bos, char[] id) { - for (char c : id) { - bos.write(c); - } - } - } } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java new file mode 100644 index 0000000000..06b5ce6d1d --- /dev/null +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.audio.utils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * ShellUtils + * + * @since 2021-08-27 + */ +public class WaveHeader { + private static final int BITS_PRE_SAMPLE = 16; + + private static final short BYTE_BIT = 24; + + private static final short SHORT_SIZE = 2; + + private static final short INT_SIZE = 4; + + private static final int BIT = 8; + + public final char[] fileID = {'R', 'I', 'F', 'F'}; + public int fileLength; + public char[] wavTag = {'W', 'A', 'V', 'E'}; + public char[] FmtHdrID = {'f', 'm', 't', ' '}; + public int FmtHdrLeth; + public short FormatTag; + public short Channels; + public int SamplesPerSec; + public int AvgBytesPerSec; + public short BlockAlign; + public short BitsPerSample; + public char[] DataHdrID = {'d', 'a', 't', 'a'}; + public int DataHdrLeth; + + public byte[] getHeader() throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + writeChar(bos, fileID); + writeInt(bos, fileLength); + writeChar(bos, wavTag); + writeChar(bos, FmtHdrID); + writeInt(bos, FmtHdrLeth); + writeShort(bos, FormatTag); + writeShort(bos, Channels); + writeInt(bos, SamplesPerSec); + writeInt(bos, AvgBytesPerSec); + writeShort(bos, BlockAlign); + writeShort(bos, BitsPerSample); + writeChar(bos, DataHdrID); + writeInt(bos, DataHdrLeth); + bos.flush(); + byte[] r = bos.toByteArray(); + bos.close(); + return r; + } + + private void writeShort(ByteArrayOutputStream bos, int s) throws IOException { + // write short + byte[] mybyte = new byte[SHORT_SIZE]; + mybyte[1] = (byte) ((s << BITS_PRE_SAMPLE) >> BYTE_BIT); + mybyte[0] = (byte) ((s << BYTE_BIT) >> BYTE_BIT); + bos.write(mybyte); + } + + + private void writeInt(ByteArrayOutputStream bos, int n) throws IOException { + // write int + byte[] buf = new byte[INT_SIZE]; + buf[3] = (byte) (n >> BYTE_BIT); + buf[2] = (byte) ((n << BIT) >> BYTE_BIT); + buf[1] = (byte) ((n << BITS_PRE_SAMPLE) >> BYTE_BIT); + buf[0] = (byte) ((n << BYTE_BIT) >> BYTE_BIT); + bos.write(buf); + } + + private void writeChar(ByteArrayOutputStream bos, char[] id) { + for (char c : id) { + bos.write(c); + } + } +} -- Gitee From 053d9389503dab77104469b9cdd014a37314fcdf Mon Sep 17 00:00:00 2001 From: guojin26 Date: Tue, 31 Aug 2021 09:31:08 +0800 Subject: [PATCH 10/10] Audio Signed-off-by: guojin26 --- .../src/main/java/ohos/samples/audio/utils/ShellUtils.java | 6 ++---- .../src/main/java/ohos/samples/audio/utils/WaveHeader.java | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java index b925f1519e..e0d4661ab1 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/ShellUtils.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; /** * ShellUtils @@ -90,10 +91,7 @@ public class ShellUtils { } file.close(); fos.close(); - - if (pcmFile.delete()) { - HiLogUtils.info(TAG, "record.pcm delete"); - } + Files.deleteIfExists(pcmFile.toPath()); } catch (IOException e) { HiLogUtils.error(TAG, e.getMessage()); } diff --git a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java index 06b5ce6d1d..629afb29eb 100644 --- a/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java +++ b/media/Audio/entry/src/main/java/ohos/samples/audio/utils/WaveHeader.java @@ -19,7 +19,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; /** - * ShellUtils + * WaveHeader * * @since 2021-08-27 */ -- Gitee