0

This is my playSound method:

public static void playSound(FileInputStream f,boolean repeat)
{
    AudioPlayer Audio=AudioPlayer.player;
    AudioData MD;
    AudioDataStream sound=null;
    ContinuousAudioDataStream loop=null;
    try{
        MD=new AudioStream(f).getData();
        loop=new ContinuousAudioDataStream(MD);
        sound=new AudioDataStream(MD);
    } 
    catch (Exception e){}
    if(!repeat)
      Audio.start(sound);  
    else
      Audio.start(loop);
    //Audio.stop(loop);
}

If I use a .wav music file that is short -like a gunshot sound-it works. I am trying to add some background music which is 2 minutes long. The gunshot sound works perfectly fine and the background music complies without error but it does not play. Is there a limit to how long the audio file can be and are there any solutions?

Also, not related to the question but would appreciate an answer, I am getting a message that AudioPlayer is an internal proprietary API and may be removed in the future. Could someone please explain this to me.

  • 1
    With respect to the warning, see e.g. https://stackoverflow.com/a/21313088/2891664. We shouldn't use the old `sun.audio` stuff. We should use `javax.sound.sampled` instead. https://stackoverflow.com/tags/javasound/info – Radiodef May 30 '18 at 01:25
  • **Never** throw exceptions away like that - you are probably getting an exception telling you what is wrong. Add an 'e.printStackTrace()` call to your `catch` block. – greg-449 May 30 '18 at 07:32
  • Thanks, I added e.printStackTrace() to it and I got this error message java.io.IOException: could not create AudioData object at sun.audio.AudioStream.getData(AudioStream.java:125) at Game.playSound(Game.java:615) at Game.main(Game.java:634) Does this mean the file was too long? – tshetrim lhendup May 30 '18 at 18:58

0 Answers0