3

我从服务器收到了一个字节数组,我知道它可以完美地连接和发送。这是我尝试从字节数组播放声音的时候。

这是我必须播放的声音。

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);
    }
}

获取音频格式:

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[]

4

3 回答 3

2

我看不到您在 while 循环中从声音字节数组中读取的位置。他们按照您的设置方式,可能应该有以下几点:

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

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

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

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

于 2013-02-19T21:15:28.037 回答
0

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

去那里,它的播放方式,你只需转到指定的链接,即:www.localhost:8080/test 在我的例子中,浏览器会播放音乐。

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

实际上,无论链接返回字节数组,都将由浏览器根据数据类型等进行流式传输。

于 2013-02-18T22:34:19.700 回答
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();
    }
  }

}
于 2021-02-08T04:11:34.340 回答