从byte []数组播放声音

时间:2013-02-18 21:02:37

标签: java client-server javasound audio

我从服务器收到一个字节数组,我知道连接和发送完美。当我尝试播放字节数组中的声音时。

这是我播放声音的内容。

SourceDataLine speaker = null;
try {
    DataLine.Info speakerInfo = new DataLine.Info(SourceDataLine.class, getAudioFormat(samplerate));
    speaker = (SourceDataLine) AudioSystem.getLine(speakerInfo);
} catch (LineUnavailableException e) {
    e.printStackTrace();
}
int nBytesRead = 0;
    while (nBytesRead != -1) {
    if (nBytesRead >= 0) {
         speaker.write(bytes, 0, nBytesRead);
    }
}

getAudioFormat:

private AudioFormat getAudioFormat(float sample) {
    int sampleSizeBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = false;
    return new AudioFormat(sample, sampleSizeBits, channels, signed, bigEndian);
}

如何播放byte[]的音乐?

3 个答案:

答案 0 :(得分:2)

我没有看到你在while循环中读取声音字节数组的位置。你设置的方式,应该有这样的东西:

while (nBytesRead = soundDataArray.read(bytes) != 1)

...假设您已经设置了read方法,以便名为'bytes'的缓冲区接收来自read命令的数据。然后write()方法将重复填充“bytes”以发送。

当然,'bytes'只是一个仅在while循环中使用的缓冲区,而不是具有源声音的字节数组。

有时read方法有两个输入,如:.read(bufferArray, bytesToRead); 其中k或几k范围内的值是常见的。 (bufferArray.length == bytesToRead)

答案 1 :(得分:0)

前段时间我写了一个小型服务器来通过http:Stream music in loop over http using java

传输音乐

去那边,以及它播放的方式,你只需转到指定的链接,即:www.localhost:8080 / test在我的情况下,浏览器会播放音乐。

也许你可以找到将我的一些结果与你的结果相结合的解决方案。

实际上,无论链接返回bytearray,都将由浏览器根据数据类型等进行流式处理。

答案 2 :(得分:0)

这样你就可以从 Byte[] 播放声音。

希望能帮到别人。

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

public class PlaySoundFromByteArr {

  public static void main(String[] args) throws IOException, UnsupportedAudioFileException {
    String FILE_PATH = "resources/wav-1.wav";

    byte[] byteArr = getByte(FILE_PATH);
    AudioFormat format = getFormat(FILE_PATH);
    playAudioUsingByteArray(byteArr, format);
  }

  private static byte[] getByte(String FILE_PATH) {
    byte[] byteArr = new byte[0];

    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      AudioInputStream in =  AudioSystem.getAudioInputStream(new File(FILE_PATH));

      int numOfBytes;
      byte[] buffer = new byte[1024];

      while( (numOfBytes = in.read(buffer)) > 0 ){
        out.write(buffer, 0,numOfBytes);
      }
      out.flush();
      byteArr = out.toByteArray();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
      e.printStackTrace();
    }

    return byteArr;
  }

  private static AudioFormat getFormat(String FILE_PATH) throws IOException, UnsupportedAudioFileException {
    AudioInputStream in =  AudioSystem.getAudioInputStream(new File(FILE_PATH));
    return in.getFormat();
  }

  private static void playAudioUsingByteArray(byte[] byteArr, AudioFormat format) {
    try (Clip clip = AudioSystem.getClip()) {
      clip.open(format, byteArr, 0, byteArr.length);
      clip.start();
      clip.drain();
      Thread.sleep(clip.getMicrosecondLength());
    }
    catch (LineUnavailableException | InterruptedException e) {
      e.printStackTrace();
    }
  }

}