- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取已录制的 .wav,然后创建该 wav 部分的新输出流。最终目标是让我能够获取一个 wav,在某个点将其分割并在其中插入新的音频。
我使用 FFMPEG 来执行此操作,但在最新版本的 Android 中,FFMPEG 性能变得非常糟糕。
我认为我最大的问题是缺乏完全理解 .read() 和 .write() 方法。
这是我尝试过的
final int SAMPLE_RATE = 44100; // Hz
final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
final int CHANNEL_MASK = AudioFormat.CHANNEL_IN_MONO;
in1 = new FileInputStream(Environment.getExternalStorageDirectory() + "/recording.wav");
out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/recording_part_1.wav");
// Write out the wav file header
wavHeader.writeWavHeader(out, CHANNEL_MASK, SAMPLE_RATE, ENCODING);
while (in1.read(buffer, 0, buffer.length) != -1) {
out.write(buffer);
}
out.close();
in1.close();
File fileToSave = new File(Environment.getExternalStorageDirectory() + "/GMT/recording_part_1.wav");
try {
// This is not put in the try/catch/finally above since it needs to run
// after we close the FileOutputStream
wavHeader.updateWavHeader(fileToSave);
} catch (IOException ex) {
}
上面的方法有效,但它只是复制了整个内容。录音代码、writeWaveHeader 和 updateWavHeader 均来自此要点,https://gist.github.com/kmark/d8b1b01fb0d2febf5770 .
我尝试过类似的事情
for (int i = 0; i < in1.getChannel().size() / 2; i++) {
out.write(in1.read(buffer, i, 1));
}
但这根本不起作用。我也想也许
byte[] byteInput = new byte[(int)in1.getChannel().size() - 44];
while (in1.read(byteInput, 44, byteInput.length - 45) != -1) {
out.write(byteInput, 44, byteInput.length /2);
}
希望只会创建一个包含一半文件的新文件。我一直在查看文档,但我做错了。
最佳答案
查看 FileInputStream 的文档
你的方法并没有那么糟糕。这个可以完成一些工作:
for (int i = 0; i < in1.getChannel().size() / 2; i++) {
out.write(in1.read(buffer, i, 1));
}
文档说:
read(byte[] b, int off, int len)从此输入流中读取最多 len 个字节的数据到字节数组中。
您将缓冲区作为 byte[] 传递,这是正确的。
然后你将 i 作为偏移量传递。您的偏移量应为0(即从音频文件的开头开始)。
对于您传递的 len 1。这应该是您要复制的长度。因此,将 in1.getChannel().size()/2 传递到那里(直到音频文件的中间)。
在这种情况下,您甚至不需要循环,因为 read 方法会为您完成所有操作。要编辑部分的开头和结尾,您需要更改 2.&3。参数。
所以这应该适合你:
byte[] buffer = new byte[(int)(in1.gerChannel().size() / 2)];
in1.read(buffer, 0, (int)(in1.gerChannel().size() / 2));
out.write(buffer);
关于java - 将部分 FileInputStream 写入 FileOutPutstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958411/
我查看了 FileOutputStream 的 java 文档 http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.ht
在我的 Java 应用程序中,我创建了线程,其中使用包含在 BufferedInputStream 和 BufferedOutputStream 中的 FileOutputStream 和 FileI
我希望这是一个直接的设计问题。 上下文: 我可能正在通过套接字连接下载一个到多个文件。当从套接字读取字节[]时,我正在传递它们。我还知道将这些字节写入哪个文件。我将这些字节附加到文件中 FileOut
我正在尝试编写一个简单的测试方法来将文件从 Asset 文件夹复制到 sd 卡。当我尝试打开 SD 卡上的文件时,它崩溃了。 代码 try { // POp
目标是对硬编码的 log4j Appender 进行功能测试,该 Appender 扩展了 RollingFileAppender,其中我可以使用 MyAppender 重现写入同一文件的 log4j
这里是我如何将字节写入文件。我正在使用 FileOutputStream private final Handler handler = new Handler(){
如何解决这个问题? 我收到以下错误:关闭此“FileOutputStream”。但我已经在finally block 中关闭了它 public void initHistoryForOldFile(F
我过去曾遇到过此错误,但从未完全理解它。关闭 OutputStream 后,无论 java 文件的位置或其调用方式如何,所有顺序运行或写入另一个文件的尝试都会完全搞砸,即使使用不同的写入文件的方法也是
我正在尝试创建跨 JVM 锁。为了做到这一点,我想在远程 Linux 服务器中的某个位置创建一个 java.io.FileOutputStream,如下所示: some_remote_server.m
我正在制作一个包含文件复制的应用程序,但是当我浏览一个大目录(1000+)文件并将它们复制到另一个文件夹时,它使用 290+ MB 的 RAM。 那么,有没有办法在不创建 FileOutoutStre
我在 Java 中使用 FileOutputStream 和 BufferedWriter 时遇到了问题。 如果我的磁盘空间已满并且我正在尝试写入,它将抛出IOException(这是正确的),但是当
我有一个 FileOutputStream 和一个像这样的 cicle: PrintStream output = new PrintStream(new FileOutputStream("XXXX
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有一个FileOutputStream。我想“保存”此文件并为其指定一个保存在资源文件夹中的自己的图标。 代码如下: ObjectOutputStream oout = new Ob
这个问题已经有答案了: How to write data with FileOutputStream without losing old data? (2 个回答) 已关闭 7 年前。 您好,我正
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
目前我的代码中遇到 FileOutputStream 问题,使用 FileOutputStream 在磁盘中创建文件。创建文件后,无法从其位置打开、删除或移动文件,已经收到错误消息被其他用户锁定当网络
我正在尝试编写一个程序,将正弦波的数据保存到 .txt 中,但是当我执行该程序时,它写的内容比应有的少(有时甚至什么也不写)。这是代码: public static void main(String[
我在使用以下代码时遇到问题。我正在尝试写入 .ppm 文件,并且得到 Red.java:6: unreported exception java.io.FileNotFoundException; m
我正在从网络下载数据库,该数据库大小在 100 KB 到 500 KB 之间。这是我的代码(删除了无用的代码): URLConnection uConnection = downloadUrl.ope
我是一名优秀的程序员,十分优秀!