December 24, 2014

How to Play Sound Using Java Source Code. File format support is WAVE.

How to play sound using java source code

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. 
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 ////////////////////////////////////////
view raw Playsound.java hosted with ❤ by GitHub

Related Posts:

0 comments:

Post a Comment

Leave your comment and feedback here for me