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

再写完Java播放wav后,我又对代码进行了优化,支持了各种种类音频
基础功能

  1. 开始
  2. 暂停
  3. 继续
  4. 停止

本次添加功能:

  1. 获取音频进度
  2. 跳过某一段
  3. 从某一段开始播放
  4. 各种音频格式支持
/*直接copy以下代码,就当是个开源的jar
使用时请注明出处,并再运行时输出*/
import java.io.*;
import javax.sound.sampled.*;

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;
			play_from=0;
			pass=false;
		}
		catch(Exception err)
		{
			System.out.println("Error");
			err.printStackTrace();
		}
		catch(Error err)
		{
			System.out.println("Error: can not play the audio");
			err.printStackTrace();
		}
	}
}

class audio extends wav
{
	public audio(String path)
	{
		try
		{
			if(!(new File(path+".wav").exists()))
			{
				Process p=Runtime.getRuntime().exec("ffmpeg.exe -i "+path+" -f wav "+path+".wav");
				try
				{
					p.waitFor();
				}
				catch(Exception err)
				{
				}
			}
		}
		catch(IOException err)
		{
			err.printStackTrace();
		}
		this.path=path+".wav";
	}
}

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

audio au=new audio("X:/xxx/xxx.xxx");
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();//重置
//重新播放前必须重置

需要用到ffmpeg来转换音频格式
ffmpeg下载https://blog.csdn.net/twxwjh/article/details/107576426

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐