10 Replies Latest reply: Jun 24, 2012 9:27 PM by 943033 RSS

    Playing music in browser

    colin-java
      Hi,

      I have an mp3 file in my netbeans project:

      ABC/src/music/abc.mp3

      When I export the project into a jar, I am able to play music outside of the jar, such as the file C://def.mpg
      But I don't know what path to use for the ABC/src/music/abc.mp3 file, when double clicking the jar to run it.

      I also need it to play the music when in browser, but I should probably get it to work when running the jar file first.

      As it happens, this music inside the jar file plays when running directly from netbeans using the path:
      src/music/abc.mp3
      But this path doesn't work for jar file, or jar file in browser.

      Can anyone help?
        • 1. Re: Playing music in browser
          colin-java
          Ok, almost got it to work, part of the problem was that I was seeing if the file existed first.

          I should just hard code it in instead..

          Will message back soon
          • 2. Re: Playing music in browser
            jsmith
            1. Load the media with new Media(YourApplication.class.getResource("abc.mp3").toExternalForm()).
            2. Place the mp3 in the same src directory as your application java src.
            3. Have your build system copy the mp3 over to your class path and package it inside your jar file.
            4. You can debug the location by System.out.println(YourApplication.class.getResource("abc.mp3").toExternalForm()) and check that your mp3 is actually located at the path printed out.
            If you do the above, then the media should load anywhere regardless of how you deploy your app.
            • 3. Re: Playing music in browser
              colin-java
              Thanks very much for your help...

              Unfortunately its still not working..
              My music file is at src/def/def0/_4.wav

              When I do...
              Media media = new Media(MusicPlayer.class.getResource("/def/def0/_4.wav").toExternalForm());

              Running the jar file allows the music to play,
              But running it locally in desktop via the html page that comes with the jar and jnlp files does not give any sound.

              I tried a few variations, like "def/def0/_4.wav" and "src/def/def0/_4.wav", but none of them work
              Please let me know if you have any ideas to fix this, thanks
              • 4. Re: Playing music in browser
                jsmith
                Assuming your application name is MusicPlayer:
                1. Place your wav file in the same directory as MusicPlayer.java
                2. Don't access it via "/def/def0/_4.wav", just access it as "_4.wav"
                3. Make sure that your wav file is copied into the jar under the same directory as MusicPlayer.class.
                • 5. Re: Playing music in browser
                  colin-java
                  Thanks, I got it to work with Xylophone class in my browser, using a MediaPlayer instead of Audio thing.

                  I figured it was something to do with packages or locations, I will try soon, thanks
                  • 6. Re: Playing music in browser
                    colin-java
                    Its strange, I can create a new project and the music plays fine in browser after using your 3 hints.
                    But when I try to apply it to my current project, theres no sound.

                    Its really bugging me, gonna have to rebuild it step by step to try and isolate the cause.
                    • 7. Re: Playing music in browser
                      colin-java
                      Ok, I have it working, not sure what I did exactly, thanks for your help
                      • 8. Re: Playing music in browser
                        943033
                        colin-java,

                        The issue you describe exactly matches my situation. I can play MP3s from the jar, but not when I load the jar in a browser. Would you please tell me what your solution was?

                        Thanks!
                        • 9. Re: Playing music in browser
                          colin-java
                          Yes.. its as jsmith explained...

                          Put the mp3/wav files in same directory as the java file that wants to play them
                          Then in that file (Eg MyMusicPlayer) you have:

                          String mediaString = MyMusicPlayer.class.getResource("abc.mp3").toExternalForm();
                          Media media = new Media(mediaString);
                          mediaPlayer = new MediaPlayer(media);
                          mediaPlayer.play();

                          Make sure that inside the jar that is exported, that abc.mp3 lies in same directory as MusicPlayer.class file.

                          I think possibly that the code below might cause file not to play, as its calling getResource twice:
                          System.out.println("string=" + MyMusicPlayer.class.getResource("abc.mp3").toExternalForm());
                          String mediaString = MyMusicPlayer.class.getResource("abc.mp3").toExternalForm();
                          Media media = new Media(mediaString);
                          mediaPlayer = new MediaPlayer(media);
                          mediaPlayer.play();

                          Also, if you're running it in browser, try just running it locally first
                          Cause when you put on a server, sometimes you don't see the updated jar file cause of browser cache (I think).
                          A simple way to get round this, is to just put the 4 files..."webfiles folder, html page, jar file, jnlp file" into a folder of a new name.
                          Then when you enter that url in address bar, its a fresh address, so you can be sure your seeing the project you just uploaded.

                          Hope that helps
                          • 10. Re: Playing music in browser
                            943033
                            colin-java, sorry for not responding sooner. Thank you for the information. It helped me figure it out.