播放音频时,音频波形的显示问题(如图)
WINAMP播放器的一个截图...
我使用的是QuartzTypeLib实现MP3的播放..请问有什么方法可以实现上图的波形效果呢?有现成的控件吗?
------解决方案--------------------UP,我也在找!
------解决方案--------------------帮顶...
------解决方案--------------------//绘制音乐曲线图
class WaveGraphPane extends JPanel implements Runnable{
Thread graphThread;
private Font font12 = new Font("serif", Font.PLAIN, 12);
//背景黑色
public WaveGraphPane() {
setBackground(new Color(15, 15, 15));
}
//按字节数据绘制音乐曲线图
public void drawWaveLine(byte[] audioBytes){
lines.removeAllElements(); //清除向量元素
if(audioBytes==null){
try{ //帧数*一帧字节数,并将long型强制转换为int,不支持太大文件的播放.
audioFormat=audioInputStream.getFormat();
audioBytes=new byte[(int)(audioInputStream.getFrameLength()*audioFormat.getFrameSize())];
audioInputStream.read(audioBytes); //读入到audioBytes字节数组
}catch(Exception e){
System.out.println(e.toString());
repaint();
return;
}
}
int w=getSize().width;
int h=getSize().height-20;
int[] audioData=null;//将byte数组数据转换为int数组数据,便于绘制.
if(audioFormat.getSampleSizeInBits()==16){
int nlengthInSamples=audioBytes.length/2;
audioData=new int[nlengthInSamples];
if(audioFormat.isBigEndian()){
for(int i=0; i<nlengthInSamples;i++){
//audioBytes偶字节HAB作高位字节处理
int HAB = (int) audioBytes[2*i];
System.out.println("hello");
//System.out.println(HAB);
//audioBytes奇字节LAB作低位字节处理
int LAB = (int) audioBytes[2*i+1];
//移位形成一个新整数
audioData[i] = HAB ><< 8 | (255 & LAB);
}
}
else{
for (int i = 0; i < nlengthInSamples; i++) {
//audioBytes偶字节作低位字节处理
int LAB = (int) audioBytes[2*i];
//audioBytes奇字节作高位字节处理
int HAB = (int) audioBytes[2*i+1];
audioData[i] = HAB << 8 | (255 & LAB);
}
}
}
//一个象素点对应多少个帧
int frames_per_pixel=audioBytes.length/audioFormat.getFrameSize()/w;
byte my_byte=0;
double y_last = 0;
int numChannels=audioFormat.getChannels();
for (double x = 0; x < w && audioData != null; x++){
int idx = (int)(frames_per_pixel * numChannels * x);
//双字节采样,32768是2的15次方
my_byte=(byte)(128 * audioData[idx]/32768 );
double y_new = (double) (h * (128 - my_byte) / 256);
lines.add(new Line2D.Double(x, y_last, x, y_new));
y_last = y_new;
}
repaint();
}
------解决方案--------------------codeproject上有很多这样的DEMO的.可以看看啊.
------解决方案--------------------