OK, then working on my program which will play a song after clicking on the button, I have the code , And my audio is ringing when the button is clicked, but the only problem is that my program is completely collapsing and clicking on button is simply playing audio !!!! The program just freezes the audio rings, but the program will not have it of other functions! What am I doing wrong? And how do I fix it?
Code for action listeners button
myframe.java
sound sound = new sound (); Private file pokemonBattle = new file (". \\ audio \\" + "sound.wav"); Private Zero dualButtonActionPerformed (java.awt.event.ActionEvent evt) {// TODO Add your handling code here: for (int i = 0; i sound.java where I have play audio () import java .io.file; Import javax.sound.sampled *; Public class sound {public zero play audio (file sf) {audio format audio format; Audio indstream audio inputstream; Source-source source-line; Try {audioInputStream = AudioSystem.getAudioInstreamStream (sf); Audioformat = audioInputStream.getformat (); Println (audioFormat); DataLine.Info dataLineInfo = New DataLine.Info (SourceDataLine.class, audioFormat); SourceDataLine = (SourceDataLine) audio system.gateline (dataLineInfo); Byte temp buffer [] = new byte [100000]; Int cnt; SourceDataLine.open (audioFormat); SourceDataLine.start (); Whereas ((cnt = audioInputStream.read (tempBuffer, 0, tempBuffer.length))! = - 1) {if (cnt> 0) {sourceDataLine.write (tempBuffer, 0, cnt); }}} Hold (exception e) {e.printStackTrace (); System.exit (0); }}}
You need to play audio in a separate thread. This program becomes unresponsive because it is playing your audio instead of answering the UI drawing and user interaction.
sound.playAudio (pokemonBattle); Do:
threads t = new thread (new sound player (sound, pokemonbatter); t. Start (); And also:
Public class SoundPlayer implements Runnable {Private Ultimate Sound Sound; Private Last File SoundFile; Public SoundPlayer (Sound Sound, File SoundFile) {This sound = sound ; .soundFile = soundFile;} Public Zero Run () {sound.playAudio (soundFile);}}
Comments
Post a Comment