3

My friend asked me to create an app that he can use in conjunction with the game Plague Inc, and he wants the soundtrack from the game to play in the application. Upon doing web research I have tried everything and nothing works. Is it possible to call the soundtrack from a java package (like with an image) instead of specifying folder directories and URLs? There was some promising information I found online but when I ran the code after trying it, the AudioInputStream keeps on giving me errors. I have tried using the clauses exceptions but that severely conflicted with the main method and the application would not even run. I have tried putting the coding in the constructor, a new method and even in the main method itself but all of them just throw out errors when I run the application (I don't even know where to put it so that it will work). Please help as this is getting seriously frustrating.

My package is called Sound and the file is called plague.wav And although the game is an Android game, my application runs off Windows PC

Here is the coding I have so far:

File sound = new File("/Sound/plague.wav");
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(sound);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (LineUnavailableException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        }

1 Answers1

1

You can get it as a resource stream Check this:

       InputStream input = getClass().getResourceAsStream("/Sound/plague.wav");
       AudioInputStream audioIn = AudioSystem.getAudioInputStream(input);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.loop(Clip.LOOP_CONTINUOUSLY);
        } catch (LineUnavailableException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(knownDiseases.class.getName()).log(Level.SEVERE, null, ex);
        }

Here's a sample class:

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Snippet {

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

            InputStream input = Snippet.class.getResource("/Sound/sound.wav")
                    .openStream();
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(input);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.loop(Clip.LOOP_CONTINUOUSLY);

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // A GUI element to prevent the Clip's daemon Thread
                    // from terminating at the end of the main()
                    JOptionPane.showMessageDialog(null, "Close to exit!");
                }
            });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Muhammad Hamed
  • 1,229
  • 9
  • 19
  • I tried this coding you gave me and the 'AudioInputStream' is still giving me problems. When I run the application I get the following error message: "javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1119)" and the audio file does not play. Is there any way to overcome this error? – Stefano OsirizẐz De Mattia Nov 15 '14 at 21:14
  • The wav may include some encoding or compression so it might be not able to be played with Java .. As Java supports PCM wav .. Try to download this wav file and test it in your code http://download.wavetlan.com/SVV/Media/HTTP/WAV/Media-Convert/Media-Convert_test1_Alaw_Mono_VBR_8SS_16000Hz.wav – Muhammad Hamed Nov 16 '14 at 00:24
  • I tried the file you suggested and the exception error said that file was not supported. The plague file I'm trying to play does not give that error message, it simply says it cannot find the stream and it says that error lies on the line where the AudioInputStream object is created. That is what is constantly giving me a problem is that AudioInputStream. Is there anything else I can use besides the AudioInputStream to see if the soundtrack will work? – Stefano OsirizẐz De Mattia Nov 17 '14 at 12:36
  • I GOT IT WORKING! :D...The initial coding you suggested to me was 100% correct, the reason why I kept getting the error was because the file itself was corrupted when I converted it. You see it was originally a .mp3 and when I converted it to a .wav I did so in the file properties cause it to become corrupt. I downloaded the file again and used FormatFactory to convert it to a .wav and it works. Thanx for your help, much appreciated :) – Stefano OsirizẐz De Mattia Nov 17 '14 at 15:01