0

I found this online and manipulated it to the best of my ability but I cannot get it to work.

import java.io.*;
import sun.audio.*;

/**
 * A simple Java sound file example (i.e., Java code to play a sound file).
 * AudioStream and AudioPlayer code comes from a javaworld.com example.
 * @author alvin alexander, devdaily.com.
 */



public class SoundTest
{
  public static void main(String[] args) 
  throws IOException

  {
    // open the sound file as a Java input stream
    String gongFile = "C:/Users/jd186856/Desktop/SoundTest/IMALEMON.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);

  }
}

Here are the error codes:

Exception in thread "main" java.io.IOException: could not create audio stream from input stream at sun.audio.AudioStream.(AudioStream.java:80) at SoundTest.main(SoundTest.java:23)

Thanks for any help in advance!!

Simon Bosley
  • 1,114
  • 3
  • 18
  • 41
  • Just to clarify: [jGrasp](https://en.wikipedia.org/wiki/JGRASP) is your [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment), not your [programming language](https://en.wikipedia.org/wiki/Programming_language). – robotlos May 10 '16 at 17:00
  • This link might help: http://stackoverflow.com/questions/6870816/why-this-code-doesnt-play-the-sound-file?rq=1 – robotlos May 10 '16 at 17:04
  • I know that jGrasp is the IDE. I already tried that earlier but no success. – NeglectedNoodles May 10 '16 at 17:31

1 Answers1

1

I'm not able to find references to the Java class AudioStream except in a listing from 1997!

The current practice is to use an AudioInputStream. This can be found in the API for Java 7 & 8.

The Java Sound Tutorials are a difficult read but cover the current practices. See the section on "playing back audio". When importing a sound, I always use the URL, and avoid creating an InputStream as an intermediate step. The code which converts InputStreams to AudioInputStreams may apply "Mark/Reset" tests to the InputStream which can fail. Making an AudioInputStream directly from a URL avoids this test.

In fact, if AudioStream allows you to use a URL as a source, changing it to use the URL of your source file might fix your code, but I wouldn't count on it working on every Java system out there, given that many sun libraries have been deprecated. (I don't know for sure if this one has been deprecated or not, but not finding it in the current API is telling.)

Another dodge that sometimes works is to wrap the InputStream in a BufferedInputStream, as this class implements the mark and reset methods that sometimes cause errors when attempting to use an InputStream.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41