|
10Java金币
本人小白,近几日学习java编程,在编写一个播放音频的小程序时遇到了问题,
音频无法播放且报异常,以下为详细代码
求大神们解答,谢谢!
/**********************
* 此程序实现mid、wav格式音频文件的播放
* Version 1.0
* @author Zha_yongchun
* Email:1729465178@qq.com
*/
package com.music;
import java.applet.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
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;
import javax.swing.*;
import java.awt.*;
public class playAudio extends JFrame implements ActionListener
{
JButton btExit,btOpen,btPlay,btLoop,btStop;
JPanel panel;
public static void main(String[] args)
{
playAudio pa=new playAudio();
}
//构造方法
public playAudio()
{
//定义按钮
btExit=new JButton("EXIT");
btOpen=new JButton("OPEN");
btPlay=new JButton("PLAY");
btLoop=new JButton("LOOP");
btStop=new JButton("STOP");
panel=new JPanel();
//流式布局
this.setLayout(new GridLayout(1,1));
//添加按钮
panel.add(btExit);
panel.add(btOpen);
panel.add(btPlay);
panel.add(btLoop);
panel.add(btStop);
//添加事件监听
btExit.addActionListener(this);
btExit.setActionCommand("exit");
btOpen.addActionListener(this);
btOpen.setActionCommand("open");
btPlay.addActionListener(this);
btPlay.setActionCommand("play");
btLoop.addActionListener(this);
btLoop.setActionCommand("loop");
btStop.addActionListener(this);
btStop.setActionCommand("stop");
this.add(panel);
//显示窗口
this.setIconImage(new ImageIcon("image/music.JPG").getImage());
this.setTitle("MusicPlayer");//设置用户界面
this.setSize(400,100);//设置窗口大小,数字代表像素
this.setLocation(300,260);//设置窗体位置
this.setResizable(false);//设置窗体大小是否可调整
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭进程
this.setVisible(true);//true 显示,false不显示
}
//事件监听
public void actionPerformed(ActionEvent e)
{
Audio player=new Audio("G:/1.mid");
//退出
if(e.getActionCommand().equals("exit"))
{
System.out.println("Exit!");
System.exit(0);
}
//打开
else if(e.getActionCommand().equals("open"))
{
}
//播放
else if(e.getActionCommand().equals("play"))
{
player.start();
}
//循环
else if(e.getActionCommand().equals("loop"))
{
}
//暂停
else if(e.getActionCommand().equals("stop"))
{
}
}
}
/*****************************
* Audio类实现声音流播放
* 变量名与用途:
* audioPath<音频输入路径>作为文件路径
* hczj<缓冲字节>定义了缓冲大小
****************************/
class Audio extends Thread {
private String audioPath;
public Audio(String audioPath)
{
this.audioPath=audioPath;
}
public void run()
{
File filestream = new File(audioPath);
FileInputStream fis=null;
AudioInputStream audioinputstream = null;
try
{
fis=new FileInputStream(filestream);
audioinputstream = AudioSystem.getAudioInputStream(fis);
}
catch (Exception e)
{
System.out.println("Error01!");
}
AudioFormat format = audioinputstream.getFormat();
SourceDataLine sourcedataline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
sourcedataline = (SourceDataLine) AudioSystem.getLine(info);
sourcedataline.open(format);
} catch (Exception e)
{
System.out.println("Error02!");
}
sourcedataline.start();
int ss = 0;
byte[] hczj = new byte[1024];
try
{
while (ss != -1)
{
ss = audioinputstream.read(hczj, 0, hczj.length);
if (ss >= 0)
sourcedataline.write(hczj, 0, ss );
}
}
catch (Exception e)
{
System.out.println("Error03!");
}
finally
{
sourcedataline.drain();//将残留部分处理干净
sourcedataline.close();
}
}
}
|
|