This article shares the specific code for javax.sound to realize simple audio playback for your reference. The specific content is as follows
/** * @see * @author Al_assad [email protected] * @date November 17, 2016 at 6:27:59 pm * @version V1.0 * Description: Simple audio player (only supports AU, RA, WAV) * Quickly implement audio playback without using JMF* */ import javax.sound.sampled.*; import java.io.*; public class MusicPlayer { private String musicPath; //Audio file private volatile boolean run = true; //Record whether audio plays private Thread mainThread; //The task thread for playing audio private AudioInputStream audioStream; private AudioFormat audioFormat; private SourceDataLine sourceDataLine; public MusicPlayer(String musicPath) { this.musicPath = musicPath; prefetch(); } //Data preparation private void prefetch(){ try{ //Get audio input stream audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); //Get the encoded object of audioFormat = audioStream.getFormat(); //Packaging audio information DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED); //Create source data rows using the Info class after wrapping audio information, and act as the source sourceDataLine of the mixer = (SourceDataLine)AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); }catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(LineUnavailableException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); } } //Destructor: Close audio read stream and data lines protected void finalize() throws Throwable{ super.finalize(); sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } //Play audio: Set whether to loop private void playMusic(boolean loop)throws InterruptedException { try{ if(loop){ while(true){ playMusic(); } }else{ playMusic(); //Clear the data line and close sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } } catch(IOException ex){ ex.printStackTrace(); } } private void playMusic(){ try{ synchronized(this){ run = true; } //Read the audio data stream through the data line and send it to the mixer; //Data stream transmission process: AudioInputStream -> SourceDataLine; audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); int count; byte tempBuff[] = new byte[1024]; while((count = audioStream.read(tempBuff,0,tempBuff.length)) != -1){ synchronized(this){ while(!run) wait(); } sourceDataLine.write(tempBuff,0,count); } } catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }catch(InterruptedException ex){ ex.printStackTrace(); } } //Pause audio playback private void stopMusic(){ synchronized(this){ run = false; notifyAll(); } } //Continue to play music private void continueMusic(){ synchronized(this){ run = true; notifyAll(); } } //External call control method: generate audio main thread; public void start(boolean loop){ mainThread = new Thread(new Runnable(){ public void run(){ try { playMusic(loop); } catch (InterruptedException e) { e.printStackTrace(); } } }); mainThread.start(); } //External call control method: pause the audio thread public void stop(){ new Thread(new Runnable(){ public void run(){ stopMusic(); } }).start(); } //External call control method: continue the audio thread public void continues(){ new Thread(new Runnable(){ public void run(){ continueMusic(); } }).start(); } //Test public static void main(String[] args) throws InterruptedException{ MusicPlayer player = new MusicPlayer("bgm/1.wav"); //Create music player player player.start(true); //Start playback in a loop form, player(false) does not play TimeUnit.SECONDS.sleep(5); player.stop(); //Pause audio playback TimeUnit.SECONDS.sleep(4); player.continues(); //Continue to start playing audio} }
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.