我在从套接字创建AudioInputStream时遇到了问题。以下是重要的部分:
public class SoundStream extends Thread {
private int port;
private String IP;
private Socket socket;
private SoundObject soundObject;
private OpenAL openAL;
private Source source;
private boolean run = true;
public SoundStream(int port, String IP, SoundObject soundObject) {
this.soundObject = soundObject;
this.port = port;
this.IP = IP;
}
public void run() {
try {
this.socket = new Socket(this.IP, this.port);
this.openAL = new OpenAL();
} catch (Exception e) {
e.printStackTrace();
}
this.mainCycleMethod();
}
private void mainCycleMethod() {
while (run) {
this.soundObject.blockAndWait();
switch (this.soundObject.getAndResetEvent()) {
case 0:
this.run = false;
this.close();
break;
case 1:
this.setPitch();
break;
case 2:
this.closeSource();
this.play();
break;
case 3:
this.pause(true);
break;
case 4:
this.pause(false);
break;
}
}
}
private BufferedInputStream getInputStream() throws Exception {
return new BufferedInputStream(socket.getInputStream());
}
private void setPitch() {
if(this.source != null) {
try {
this.source.setPitch(this.soundObject.getPitch());
} catch (ALException e) {
e.printStackTrace();
}
}
}
private void play() {
try {
AudioInputStream audioInputStream = new AudioInputStream(this.getInputStream(), this.soundObject.getAudioFormat(), AudioSystem.NOT_SPECIFIED);
// AudioInputStream audioInputStream_tmp = AudioSystem.getAudioInputStream(this.getInputStream());
// AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.soundObject.getAudioFormat(), audioInputStream_tmp);
this.source = openAL.createSource(audioInputStream);
this.source.setGain(1f);
this.source.play();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void close() {
this.closeSource();
this.openAL.close();
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void closeSource() {
if(this.source!=null) {
this.source.close();
}
}
private void pause(boolean pause) {
if(this.source != null) {
try {
if (pause) {
this.source.pause();
} else {
this.source.play();
}
} catch (ALException ex) {
ex.printStackTrace();
}
}
}
}
public class SoundObject extends AbstractEventObject {
public AudioFormat getAudioFormat() {
boolean signed = false;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat(this.frequency, this.bits, this.channels, signed, bigEndian);
}
.
.
.
.
}这段代码将UnsupportedAudioFileException抛到这一行:
AudioInputStream audioInputStream_tmp = AudioSystem.getAudioInputStream(this.getInputStream());但是,当我使用此代码时:
AudioInputStream audioInputStream = new AudioInputStream(this.getInputStream(), this.soundObject.getAudioFormat(), 100000);它播放声音,但只在它将这100000个样本帧加载到音频输入流之后。在播放完所有的100000帧之后。
我想,如果我能够在第一次AudioFormat初始化过程中将AudioInputStream作为参数直接传递给它,我就会解决这个问题,但这似乎是不可能的。我正在从服务器接收音频格式规范。
我认为一个可能的解决方案是创建一个dataline,我可以将它作为参数传递给AudioInputStream构造函数。但是,我不知道如何从套接字直接获取数据到dataline。我知道一个使用无限循环的解决方案,在这个解决方案中,它读取数据并将它们写入dataline。但这似乎是浪费。是否有更直接的方法?
我希望使用java库解决问题是可能的,因为我需要改变速度,我希望我不必自己去做。
谢谢
发布于 2015-12-16 20:48:32
我终于解决了这个问题。事实证明,java内置了流支持,但它不在GitHub的文档中,所以我一开始没有注意到。源类中有一个createOutputStream方法,它返回OutputStream。您可以直接将字节写入OutputStream。
这是我的代码:
在这个片段中,我初始化了OpenAL:
public void run() {
try {
this.socket = new Socket(this.IP, this.port);
this.openAL = new OpenAL();
} catch (Exception ex) {
Log.severe(ex.toString());
}
this.mainCycleMethod();
}下面是在InputStream可用时调用的play方法:
private void play() {
try {
this.source = openAL.createSource();
this.outputWriter = new OutputWriter(this.socket.getInputStream(), this.source, this.soundObject.getAudioFormat());
this.source.setGain(1f);
this.outputWriter.start();
} catch (Exception ex) {
Log.severe(ex.toString());
}
}您必须使用没有参数的createSource方法,它返回Source的新实例。不要调用源上的play方法,它由SourceOutputStream类处理,createOutputStream方法返回该类实例。手动调用play方法没有什么问题,但是当缓冲区为空时,我的经验很糟糕。基本上,当您开始将数据流到OpenAL时,它不会在稍后开始播放。
下面是我的OutputWriter代码,它负责将字节从InputStream传递给OutputStream:
package cz.speechtech.sound;
import org.urish.openal.ALException;
import org.urish.openal.Source;
import javax.sound.sampled.AudioFormat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by honza on 16.12.15.
*/
public class OutputWriter extends Thread {
private InputStream inputStream;
private OutputStream outputStream;
private int STREAMING_BUFFER_SIZE = 24000;
private int NUMBER_OF_BUFFERS = 4;
private boolean run = true;
public OutputWriter(InputStream inputStream, Source source, AudioFormat audioFormat) {
this.inputStream = inputStream;
try {
this.outputStream = source.createOutputStream(audioFormat, this.NUMBER_OF_BUFFERS, 1024);
} catch (ALException e) {
e.printStackTrace();
}
}
public void run() {
byte[] buffer = new byte[this.STREAMING_BUFFER_SIZE];
int i;
try {
Thread.sleep(1000); // Might cause problems
while (this.run) {
i = this.inputStream.read(buffer);
if (i == -1) break;
outputStream.write(buffer, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void stopRunning() {
this.run = false;
try {
this.outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}祝您今天愉快。
https://stackoverflow.com/questions/33744164
复制相似问题