Here simple java code segment to play a audio file inside your java application.But limitation is there because it support only WAVE file format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package default; | |
import java.io.InputStream; | |
import sun.audio.AudioPlayer; | |
import sun.audio.AudioStream; | |
/** | |
* | |
* @author Open source developers | |
* | |
*/ | |
/* | |
This class support only wave format sound clips. | |
Assume all the sound clips were copied into Sound folder which in root folder (/Sound/myaudio.wav) | |
root means src folder. | |
*/ | |
public class Playsound { | |
private InputStream inputStream=null; | |
private static AudioStream audioStream=null; | |
public static void stop(){ | |
AudioPlayer.player.stop(audioStream); | |
} | |
public void play(String FILENAME){ | |
try{ | |
inputStream= getClass().getResourceAsStream("/Sound/"+FILENAME); | |
audioStream= new AudioStream(inputStream); | |
AudioPlayer.player.start(audioStream); | |
} | |
catch(Exception error){ | |
System.out.println("Unable to play audio clip"); | |
} | |
} | |
} | |
/////////////////// THIS IS HOW TO USE THIS CLASS ////////////////////////////// | |
Playsound playsound=new Playsound(); | |
//use play button click event | |
playsound.play("audio_file_name.wav"); | |
//wait a while or use stop button click event | |
Playsound.stop(); | |
//////////////ENJOY JAVA SHARE JAVA //////////////////////////////////////// |
0 comments:
Post a Comment
Leave your comment and feedback here for me