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

    科技2025-08-14  6

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

    之前发过一篇关于播放音频的文章,但是占用CPU过高,这里是改进版 改进内容

    修复部分bugCPU占用率减少安全性提高

    Wav类

    package player; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; public class wav extends Thread { private String path="audio.wav"; private AudioFormat format=null; private AudioInputStream aistream=null; private float sampleRate=0; private float framelength=0; private DataLine.Info datalineinfo=null; private SourceDataLine dataline=null; private boolean pause=false; private boolean stop=false; private int played=0; private int play_from=0; boolean pass=false; private boolean playing=false; public wav() { } public void setPath(String p) { path=p; } public String getPath() { return path; } public void stopPlaying() { stop=true; } public boolean isStopped() { return stop; } public int getPlayed() { return played; } public void playFrom(int t) { play_from=t; } public float getSecLength() { return framelength/sampleRate; } public void setPause(boolean p) { pause=p; } public boolean isPaused() { return pause; } public boolean isPlaying() { return playing; } public void set() { try { aistream=AudioSystem.getAudioInputStream(new File(path)); format=aistream.getFormat(); sampleRate=format.getSampleRate(); framelength=aistream.getFrameLength(); 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; playing=true; stop=false; while(stop==false) { if(pause==true) { Thread.sleep(1500); continue; } if(pass==true) { if((length=aistream.read(bytes))<=0) { break; } played+=1; continue; } if(played<play_from) { if((length=aistream.read(bytes))>0) { played+=1; continue; } else { break; } } if((length=aistream.read(bytes))>0) { dataline.write(bytes, 0, length); played+=1; } else { break; } } stop=true; aistream.close(); dataline.drain(); dataline.close(); aistream=null; format=null; datalineinfo=null; dataline=null; } catch(Exception err) { System.out.println("Error"); err.printStackTrace(); } catch(Error err) { System.out.println("Error: can not play the audio"); err.printStackTrace(); } } }

    Audio

    package player; import java.io.File; import java.io.IOException; public class audio extends wav { public audio(String path) { try { if(new File(path+".wav").exists()) { new File(path+".wav").delete(); } 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.setPath(path+".wav"); } public audio(String path, boolean del) { try { if(new File(path+".wav").exists()) { } else { 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.setPath(path+".wav"); } }

    getlength

    package player; public class getlength { public getlength() { } public static int get(String path) { audio au=new audio(path); au.pass=true; au.set(); au.start(); while(au.isStopped()!=true) { try { Thread.sleep(1000); } catch(InterruptedException err) { } } return au.getPlayed(); } public static int getWavLength(String path) { wav au=new wav(); au.setPath(path); au.pass=true; au.set(); au.start(); while(au.isStopped()!=true) { try { Thread.sleep(1000); } catch(InterruptedException err) { } } return au.getPlayed(); } } //Audio类使用方法 audio Audio=new Audio("path"); //如果碰到相同文件名的文件删除重新编译 //时间使用久,安全 audio Audio=new Audio("path", false); //如果碰到相同文件名的文件不要删掉重新编译 //时间使用短,相对不安全 //推荐使用 //注意:Audio类使用时需要在根目录放置ffmpeg.exe供编译转码,下载方式见之前的文章 //Wav类使用方法(Audio) wav Wav=new wav(); Wav.setPath("path");//设置路径 //注意:使用Wav类不需要ffmpeg.exe //Wav/Audio类方法 String getPath(); //获取路径 void stopPlaying(); //停止播放 boolean isStopped(); //是否停止播放 intPlayed(); //播放进度 void playFrom(int t); //设置播放位置 float setSecLength(); //获取长度(秒) void setPause(boolean pause); //设置是否暂停 boolean isPaused(); //是否暂停 boolean isPlaying(); //是否正在播放 Wav.pass=true; //跳过 Wav.pass=false; //停止跳过 //getlength类基本用法 int getWavLength(String path); //获取Wav文件长度 int get(String path); //获取音频长度,需要ffmpeg.exe
    Processed: 0.039, SQL: 8