Java播放.wav

本文介绍了如何使用Java实现.wav音频的播放,并且支持开始、暂停、继续和停止操作。此外,新添加的功能包括获取音频播放进度以及从指定位置开始播放和跳过某段音频。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java播放.wav音频,支持开始暂停继续停止

之前发布过这篇文章,这次更新了代码,添加了一些功能,重新写一篇文章
本次添加功能:

  1. 获取音频进度
  2. 跳过某一段
  3. 从某一段开始播放
/*直接copy以下代码,当作一个开源的jar包
使用时请注明出处,并在运行时输出*/
class wav extends Thread
{
	public String path="audio.wav";
	public AudioFormat format=null;
	public AudioInputStream aistream=null;
	public float sampleRate=0;
	public float framelength=0;
	public float seclen=0;
	public DataLine.Info datalineinfo=null;
	public SourceDataLine dataline=null;
	public boolean pause=false;
	public boolean stop=false;
	public int played=0;
	public int play_from=0;
	public boolean pass=false;
	public wav()
	{
	}
	public void set()
	{
		try
		{
			aistream=AudioSystem.getAudioInputStream(new File(path));
			format=aistream.getFormat();
			sampleRate=format.getSampleRate();
			framelength=aistream.getFrameLength();
			seclen=framelength/sampleRate;
			datalineinfo=new DataLine.Info(SourceDataLine.class, format);
			dataline=(SourceDataLine)AudioSystem.getLine(datalineinfo);
		}
		catch(LineUnavailableException err)
		{
			System.out.println("LineUnavailableException");
		}
		catch(UnsupportedAudioFileException err)
		{
			System.out.println("UnsupportedAudioFileException");
		}
		catch(IOException err)
		{
			System.out.println("IOException");
		}
	}
	public void run()
	{
		try
		{
			byte[] bytes=new byte[512];
			int length=0;
			dataline.open(format);
			dataline.start();
			played=0;
			while(stop==false)
			{
				if(pause==false)
				{
					if((length=aistream.read(bytes))>0)
					{
						if(played>=play_from)
						{
							if(pass==false)
							{
								dataline.write(bytes, 0, length);
							}
							else
							{
								System.out.print("");
							}
							played+=1;
						}
						else
						{
							played+=1;
							System.out.print("");
						}
					}
					else
					{
						break;
					}
				}
				else
				{
					System.out.print("");
				}
			}
			stop=true;
			aistream.close();
			dataline.drain();
			dataline.close();
			
			aistream=null;
			format=null;
			sampleRate=0;
			framelength=0;
			seclen=0;
			datalineinfo=null;
			dataline=null;
			pause=false;
			stop=false;
			//played=0;
			play_from=0;
			pass=false;
		}
		catch(Exception err)
		{
			System.out.println("Error");
			err.printStackTrace();
		}
	}
}

class getlength
{
	public getlength()
	{
	}
	public static int get(String path)
	{
		wav au=new wav();
		au.path=path;
		au.set();
		au.start();
		au.pass=true;
		while(au.stop!=true)
		{
			System.out.print("");
		}
		return au.played;
	}
}

wav au=new wav();
au.path="C:/audio.wav";//设置新路径
au.play_from=100;//从某处开始播放
au.set();//刷新
au.start();//开始播放,不阻塞
au.run();//开始播放,阻塞
au.pause=true;//暂停
au.pause=false;//继续
au.stop=true;//停止
au.played;//获取已经播放的进度
getlength.get(path);//获取音频总长度
au.seclen;//音频时长
au=new wav();//重置
//重新播放前必须重置

Java播放wav音频功能的实现代码,播放wav音频,压缩包中带有测试音频,是否能播放 MP3,未知。   boolean looping = false; //是否循环播放   String[] choics = { "chimes.wav", "start.wav" }; //声音文件名数组   URL file1 = getClass().getResource(choics[0]); //声音文件1   URL file2 = getClass().getResource(choics[1]); //声音文件2   AudioClip sound1 = java.applet.Applet.newAudioClip(file1); //声音剪辑对象1   AudioClip sound2 = java.applet.Applet.newAudioClip(file2); //声音剪辑对象2   AudioClip chosenClip = sound1; //选择的声音剪辑对象   JComboBox jcbFiles = new JComboBox(choics); //文件选择组合框   JButton playButton = new JButton("播放"); //播放按钮   JButton loopButton = new JButton("循环播放"); //循环播放按钮   JButton stopButton = new JButton("停止"); //停止播放按钮   JLabel status = new JLabel("选择播放文件"); //状态栏标签   JPanel controlPanel = new JPanel(); //控制面板用于包容按钮   Container container = getContentPane(); //获得窗口内容窗格   public AudioPlayDemo() { //构造器    super("声音播放程序"); //调用父类构造器设置窗口标题栏    jcbFiles.setSelectedIndex(0); //设置组合框选择项    jcbFiles.addItemListener(this); //为播放按钮添加项目监听器    //为播放按钮、循环播放按钮、停止播放按钮添加动作监听器    playButton.addActionListener(this);    loopButton.addActionListener(this);    stopButton.addActionListener(this);    stopButton.setEnabled(false); //设置停止播放按钮不可用    //把播放按钮、循环播放按钮、停止播放按钮加入控制面板    controlPanel.add(playButton);    controlPanel.add(loopButton);    controlPanel.add(stopButton);    //把文件选择组合框、控制面板、状态栏标签加入到窗口内容窗格    container.add(jcbFiles, BorderLayout.NORTH);    container.add(controlPanel, BorderLayout.CENTER);    container.add(status, BorderLayout.SOUTH);    setSize(300, 130); //设置窗口大小    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值