Tuesday, August 9, 2011

Play an Audio File

Play an audio file from java program.


import java.io.FileInputStream;
import java.io.InputStream;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class PlaySoundFile {

/** Plays audio from given file names. */

public static void main(String[] args) throws Exception {

String fileName = "D:/songs/laila.wav";

InputStream in = new FileInputStream(fileName);

// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);

// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);

// Similarly, to stop the audio.
// AudioPlayer.player.stop(as);

} // playAudioStream
}

No comments:

Post a Comment