0

My code:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        try{
            File snd = new File("C:/music.mp3");
            AudioPlayer MGP = AudioPlayer.player;
            AudioStream BGM;
            AudioData MD;
            ContinuousAudioDataStream loop = null;
            try{
                BGM = new AudioStream(new FileInputStream(snd));
                MD = BGM.getData();
                loop = new ContinuousAudioDataStream(MD);

            }
            catch(IOException err){    
            }

            MGP.start(loop);
        }
        catch(Exception e){
        }

    }  

I am getting no errors. Its just that I'm not hearing the sound. My speakers are properly connected and volume is set to max. Please help.

Thanks.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
yashmathur
  • 65
  • 1
  • 3
  • 8
  • Have a look at http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java – Amit Mar 20 '12 at 07:17
  • But I think that will only play .wav files. I want to play MP3 files. – yashmathur Mar 20 '12 at 07:19
  • 1
    1) *"I want to play MP3 files."* Read the [JavaSound info. page](http://stackoverflow.com/tags/javasound/info) for how to play MP3s using the JavaSound API. 2) *"I am getting no errors"* No, you are ***ignoring all*** errors! In the `catch` call `printStackTrace()` on the throwable. 3) When using 3rd party APIs, it pays to link to the JavaDocs for them so people can check what exceptions the methods declare etc. – Andrew Thompson Mar 20 '12 at 07:30
  • 4) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. – Andrew Thompson Mar 20 '12 at 07:30
  • I edited my code according to what you said, @AndrewThompson It prints `java.io.IOException: could not create audio stream from input stream` – yashmathur Mar 20 '12 at 07:35
  • Good. ..I'll jump back in when you have addressed all 4 points I made. – Andrew Thompson Mar 20 '12 at 07:37
  • AFAIK you can't play MP3 with JavaSE alone. You need 3rd party and your code doesn't seem to use any. I could run your code without 3rd party extension... and got errors (I added error output ) – JScoobyCed Mar 20 '12 at 07:44

1 Answers1

0

You need to download the Java Media Framework (JMF)and the MP3 plugin (and optionally but recommended Performance Pack for your platform).
Then a sample code can be found here.
The JMF javadocs can be found here

JScoobyCed
  • 10,203
  • 6
  • 34
  • 58
  • Im getting an error - `Exception in thread "JMF thread: com.sun.media.PlaybackEngine@53a9d5[ com.sun.media.PlaybackEngine@53a9d5 ] ( configureThread)" java.lang.NullPointerException` – yashmathur Mar 20 '12 at 08:56
  • EDIT: More errors- Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 24000.0 frame rate, FrameSize=32768 bits Failed to realize: com.sun.media.PlaybackEngine@83e2ef Error: Unable to realize com.sun.media.PlaybackEngine@83e2ef` – yashmathur Mar 20 '12 at 09:03
  • Did you check the JMF sample applications? Try to run one of them to make sure your installation of JMF is correct, including the support for MP3 – JScoobyCed Mar 20 '12 at 09:07