Java 数据线声级计不';不显示
我正在尝试从Java 数据线声级计不';不显示,java,audio,javasound,Java,Audio,Javasound,我正在尝试从DataLinedoc示例中获取的这段代码。据我所知,当声音播放时,它应该显示一个振幅水平仪,但它没有。它打开声音文件并正确播放,并在一个小窗口中显示有关该文件的一些数据,但没有仪表 也许我不明白它的工作原理。你能帮我理解吗 DataLineInfoGUIclass: import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.border
DataLine
doc示例中获取的这段代码。据我所知,当声音播放时,它应该显示一个振幅水平仪,但它没有。它打开声音文件并正确播放,并在一个小窗口中显示有关该文件的一些数据,但没有仪表
也许我不明白它的工作原理。你能帮我理解吗
DataLineInfoGUI
class:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.sound.sampled.*;
public class DataLineInfoGUI extends JPanel {
PCMFilePlayer player;
JButton startButton;
public DataLineInfoGUI (File f) {
super();
try {
player = new PCMFilePlayer (f);
} catch (Exception ioe) {
add (new JLabel ("Error: " +
ioe.getMessage()));
return;
}
DataLine line = player.getLine();
// layout
// line 1: name
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (new JLabel ("File: " +
player.getFile().getName()));
// line 2: levels
add (new DataLineLevelMeter (line));
// line 3: format info as textarea
AudioFormat format = line.getFormat();
JTextArea ta = new JTextArea();
ta.setBorder (new TitledBorder ("Format"));
ta.append ("Encoding: " +
format.getEncoding().toString() + "\n");
ta.append ("Bits/sample: " +
format.getSampleSizeInBits() + "\n");
ta.append ("Channels: " +
format.getChannels() + "\n");
ta.append ("Endianness: " +
(format.isBigEndian() ? " big " : "little") + "\n");
ta.append ("Frame size: " +
format.getFrameSize() + "\n");
ta.append ("Frame rate: " +
format.getFrameRate() + "\n");
add (ta);
// now start playing
player.start();
}
public static void main (String[] args) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
DataLineInfoGUI demo =
new DataLineInfoGUI (file);
JFrame f = new JFrame ("JavaSound info");
f.getContentPane().add (demo);
f.pack();
f.setVisible(true);
}
class DataLineLevelMeter extends JPanel {
DataLine line;
float level = 0.0f;
public DataLineLevelMeter (DataLine l) {
line = l;
Timer timer =
new Timer (50,
new ActionListener (){
public void actionPerformed (ActionEvent e)
{
level = line.getLevel();
repaint();
}
});
timer.start();
}
public void paint (Graphics g) {
Dimension d = getSize();
g.setColor (Color.green);
int meterWidth = (int) (level * (float) d.width);
g.fillRect (0, 0, meterWidth, d.height);
}
}
}
import javax.sound.sampled.*;
import java.io.*;
public class PCMFilePlayer implements Runnable {
File file;
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer = new byte [32 * 1024]; // 32k is arbitrary
Thread playThread;
boolean playing;
boolean notYetEOF;
public PCMFilePlayer (File f)
throws IOException,
UnsupportedAudioFileException,
LineUnavailableException {
file = f;
in = AudioSystem.getAudioInputStream (f);
AudioFormat format = in.getFormat();
AudioFormat.Encoding formatEncoding = format.getEncoding();
if (! (formatEncoding.equals (AudioFormat.Encoding.PCM_SIGNED) ||
formatEncoding.equals (AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException (
file.getName() + " is not
PCM audio");
System.out.println ("got PCM format");
frameSize = format.getFrameSize();
DataLine.Info info =
new DataLine.Info (SourceDataLine.class, format);
System.out.println ("got info");
line = (SourceDataLine) AudioSystem.getLine (info);
System.out.println ("got line");
line.open();
System.out.println ("opened line");
playThread = new Thread (this);
playing = false;
notYetEOF = true;
playThread.start();
}
public void run() {
int readPoint = 0;
int bytesRead = 0;
try {
while (notYetEOF) {
if (playing) {
bytesRead = in.read (buffer,
readPoint,
buffer.length - readPoint);
if (bytesRead == -1) {
notYetEOF = false;
break;
}
// how many frames did we get,
// and how many are left over?
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// send to line
line.write (buffer, readPoint, bytesRead-leftover);
// save the leftover bytes
System.arraycopy (buffer, bytesRead,
buffer, 0,
leftover);
readPoint = leftover;
} else {
// if not playing
// Thread.yield();
try { Thread.sleep (10);}
catch (InterruptedException ie) {}
}
} // while notYetEOF
System.out.println ("reached eof");
line.drain();
line.stop();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// line.close();
}
} // run
public void start() {
playing = true;
if (! playThread.isAlive())
playThread.start();
line.start();
}
public void stop() {
playing = false;
line.stop();
}
public SourceDataLine getLine() {
return line;
}
public File getFile() {
return file;
}
}
PCMFilePlayer
class:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.sound.sampled.*;
public class DataLineInfoGUI extends JPanel {
PCMFilePlayer player;
JButton startButton;
public DataLineInfoGUI (File f) {
super();
try {
player = new PCMFilePlayer (f);
} catch (Exception ioe) {
add (new JLabel ("Error: " +
ioe.getMessage()));
return;
}
DataLine line = player.getLine();
// layout
// line 1: name
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (new JLabel ("File: " +
player.getFile().getName()));
// line 2: levels
add (new DataLineLevelMeter (line));
// line 3: format info as textarea
AudioFormat format = line.getFormat();
JTextArea ta = new JTextArea();
ta.setBorder (new TitledBorder ("Format"));
ta.append ("Encoding: " +
format.getEncoding().toString() + "\n");
ta.append ("Bits/sample: " +
format.getSampleSizeInBits() + "\n");
ta.append ("Channels: " +
format.getChannels() + "\n");
ta.append ("Endianness: " +
(format.isBigEndian() ? " big " : "little") + "\n");
ta.append ("Frame size: " +
format.getFrameSize() + "\n");
ta.append ("Frame rate: " +
format.getFrameRate() + "\n");
add (ta);
// now start playing
player.start();
}
public static void main (String[] args) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
DataLineInfoGUI demo =
new DataLineInfoGUI (file);
JFrame f = new JFrame ("JavaSound info");
f.getContentPane().add (demo);
f.pack();
f.setVisible(true);
}
class DataLineLevelMeter extends JPanel {
DataLine line;
float level = 0.0f;
public DataLineLevelMeter (DataLine l) {
line = l;
Timer timer =
new Timer (50,
new ActionListener (){
public void actionPerformed (ActionEvent e)
{
level = line.getLevel();
repaint();
}
});
timer.start();
}
public void paint (Graphics g) {
Dimension d = getSize();
g.setColor (Color.green);
int meterWidth = (int) (level * (float) d.width);
g.fillRect (0, 0, meterWidth, d.height);
}
}
}
import javax.sound.sampled.*;
import java.io.*;
public class PCMFilePlayer implements Runnable {
File file;
AudioInputStream in;
SourceDataLine line;
int frameSize;
byte[] buffer = new byte [32 * 1024]; // 32k is arbitrary
Thread playThread;
boolean playing;
boolean notYetEOF;
public PCMFilePlayer (File f)
throws IOException,
UnsupportedAudioFileException,
LineUnavailableException {
file = f;
in = AudioSystem.getAudioInputStream (f);
AudioFormat format = in.getFormat();
AudioFormat.Encoding formatEncoding = format.getEncoding();
if (! (formatEncoding.equals (AudioFormat.Encoding.PCM_SIGNED) ||
formatEncoding.equals (AudioFormat.Encoding.PCM_UNSIGNED)))
throw new UnsupportedAudioFileException (
file.getName() + " is not
PCM audio");
System.out.println ("got PCM format");
frameSize = format.getFrameSize();
DataLine.Info info =
new DataLine.Info (SourceDataLine.class, format);
System.out.println ("got info");
line = (SourceDataLine) AudioSystem.getLine (info);
System.out.println ("got line");
line.open();
System.out.println ("opened line");
playThread = new Thread (this);
playing = false;
notYetEOF = true;
playThread.start();
}
public void run() {
int readPoint = 0;
int bytesRead = 0;
try {
while (notYetEOF) {
if (playing) {
bytesRead = in.read (buffer,
readPoint,
buffer.length - readPoint);
if (bytesRead == -1) {
notYetEOF = false;
break;
}
// how many frames did we get,
// and how many are left over?
int frames = bytesRead / frameSize;
int leftover = bytesRead % frameSize;
// send to line
line.write (buffer, readPoint, bytesRead-leftover);
// save the leftover bytes
System.arraycopy (buffer, bytesRead,
buffer, 0,
leftover);
readPoint = leftover;
} else {
// if not playing
// Thread.yield();
try { Thread.sleep (10);}
catch (InterruptedException ie) {}
}
} // while notYetEOF
System.out.println ("reached eof");
line.drain();
line.stop();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
// line.close();
}
} // run
public void start() {
playing = true;
if (! playThread.isAlive())
playThread.start();
line.start();
}
public void stop() {
playing = false;
line.stop();
}
public SourceDataLine getLine() {
return line;
}
public File getFile() {
return file;
}
}
我将尝试查看1)DataLineLevelMeter的高度或宽度是否为0,2)是否返回
音响系统。未指定或0。顺便说一句,我在网上找到了该代码的副本,其中包含一条注释,内容是:使用逻辑一致的代码行和代码块缩进形式。缩进的目的是使代码的流程更易于遵循!谢谢你的评论。getLevel()返回-1.0:可能这就是仪表不显示的原因。但为什么它会返回-1.0?它应该返回一个包含在0和1之间的值。有人遇到过类似问题吗?更新:。如果我用int meterWidth=(int)(level*(float)d.width)(0.5*(float)d.width)替换这一行:int meterWidth=(int)(float)d.width),仪表会显示,但它当然固定在0.5的值上。所以问题是这个值-1.0。请帮助我:这个奇怪值的原因是什么?@Dav如果你说它总是-1,那么这是返回AudioSystem.NOT_指定的参数。指定…文件大小、帧大小、缓冲区大小和采样率。。。是错误的原因。