本文介绍了从最初获得高达一些X字节的AudioInputStream(切割音频文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能读取的AudioInputStream 高达字节/微秒位置一个特定的数字?
例如:

How can i read an AudioInputStream upto a particular number of bytes/microsecond position ?For example :

AudioInputStream ais = AudioSystem.getAudioInputStream( new File("file.wav") );
// let the file.wav be of y bytes

现在我想获得的AudioInputStream 有高达某些数据x 字节,其中 X - LT;是字节。

Now i want to obtain an AudioInputStream that has data upto some x bytes where x < y bytes.

我怎样才能做到这一点?

How can i do that ?

我一直在想努力,但没有得到任何的方法来做到这一点?

I have been thinking hard but haven't got any method to do that ?

推荐答案

在code以下告诉您如何复制音频流的一部分,从一个文件中读取和写入到另一个。

The code below shows you how to copy a part of an audio stream, reading from one file and writing to another.

import java.io.*;
import javax.sound.sampled.*;

class AudioFileProcessor {

  public static void main(String[] args) {
    copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", 2, 1);
  }

  public static void copyAudio(String sourceFileName, String destinationFileName, int startSecond, int secondsToCopy) {
    AudioInputStream inputStream = null;
    AudioInputStream shortenedStream = null;
    try {
      File file = new File(sourceFileName);
      AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
      AudioFormat format = fileFormat.getFormat();
      inputStream = AudioSystem.getAudioInputStream(file);
      int bytesPerSecond = format.getFrameSize() * (int)format.getFrameRate();
      inputStream.skip(startSecond * bytesPerSecond);
      long framesOfAudioToCopy = secondsToCopy * (int)format.getFrameRate();
      shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
      File destinationFile = new File(destinationFileName);
      AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile);
    } catch (Exception e) {
      println(e);
    } finally {
      if (inputStream != null) try { inputStream.close(); } catch (Exception e) { println(e); }
      if (shortenedStream != null) try { shortenedStream.close(); } catch (Exception e) { println(e); }
    }
  }

  public static void println(Object o) {
    System.out.println(o);
  }

  public static void print(Object o) {
    System.out.print(o);
  }

}

这篇关于从最初获得高达一些X字节的AudioInputStream(切割音频文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 00:26