dimanche 14 décembre 2014

Java sound example - how to play a sound file in Java

A simple Java "play sound file" example

Here's the source code for my Java sound file example:
import java.io.*;
import sun.audio.*;

public class JavaAudioPlaySoundExample
{
  public static void main(String[] args)
  throws Exception
  {
    // open the sound file as a Java input stream
    String gongFile = "/Users/al/DevDaily/Projects/MeditationApp/resources/gong.au";
    InputStream in = new FileInputStream(gongFile);
    // create an audiostream from the inputstream
    AudioStream audioStream = new AudioStream(in);
    // play the audio clip with the audioplayer class
    AudioPlayer.player.start(audioStream);
  }
}
As you can see from this source code, it's pretty easy to create a basic Java sound file player.
For more info: http://alvinalexander.com/java/java-audio-example-java-au-play-sound

Aucun commentaire: