我在使用AudioInputStream播放示例时遇到了一个奇怪的问题。三种情况:
我为测试创建的代码是直接的。样品是干净的(开始和结束时零交叉)。第一个pop已经发生在soundLine.start()上,它完全独立于示例。最后一次pop发生在soundLine.close()上,该示例在几秒钟后就完成了。我在代码中添加了一些延迟,因此很容易识别代码的位置。
我制作了演示视频,在那里你可以听到发生了什么:
如您所见(听说),唯一的问题是macOS下的Java1.8。我还用相同的结果测试了java1.7。只有1.6的回放是正确的。流行音乐的声音听起来很奇怪,因为这个声音是用我的MBP的内部微音波记录下来的。这是一个典型的流行声音,你可以听到开关电源时,一个糟糕的设计放大器。
增编:问题似乎只发生在8位wav文件上.16位工作正常。
增编:同时被列为bug: http://bugs.java.com/bugdatabase/view
有人能重现这个问题吗?知道原因是什么吗?我目前假设Java中有一个bug。
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class Soundtest
{
protected File sample=new File("user1.wav");
public Soundtest()
{
SourceDataLine soundLine = null;
int BUFFER_SIZE = 1024;
System.out.println("AudioInputStream test with "+System.getProperty("java.version")+" on "+System.getProperty("os.name"));
// Set up an audio input stream piped from the sound file.
try
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(sample);
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
//Soundline ist noch nicht angelegt -> angelegen.
if(soundLine==null)
{
soundLine = (SourceDataLine) AudioSystem.getLine(info);
soundLine.open(audioFormat);
soundLine.start();
}
System.out.println("Started");
Thread.sleep(2000);
System.out.println("Playing");
int nBytesRead = 0;
byte[] sampledData = new byte[BUFFER_SIZE];
while (nBytesRead != -1)
{
nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
if (nBytesRead >= 0)
{
soundLine.write(sampledData, 0, nBytesRead);
}
}
Thread.sleep(2000);
System.out.println("Draining");
soundLine.drain();
Thread.sleep(2000);
System.out.println("Closing");
soundLine.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Soundtest();
}
}发布于 2016-12-30 17:08:39
我看不出你的代码有什么问题。我自己用Java 1.8.0_60在macOS塞拉利昂10.12.2上运行过,我的耳机里没有弹出的声音,当我从内部扬声器出来时也没有弹出。我现在能想到的唯一原因是,由于某种原因,Java 1.8不喜欢您正在使用的音频接口或媒体,如果它与Mac上的通用扬声器有任何不同的话。甚至在1.8中,音频缓冲的方式也可能有所不同。尝试提高缓冲区大小以防止未运行(这可能是弹出的原因)。
https://stackoverflow.com/questions/41399694
复制相似问题