Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Jlayer, playing mp3!

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Jlayer, playing mp3!

    Hi I downloaded the custom library Jlayer to play a music file, my code has no errors however when I run the program, nothing happens. Why?

    public static void main(String[] args) 
    {       
        Player player = null;
     
        try 
        {
            FileInputStream fis = new FileInputStream("song.mp3");
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        } 
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
     
        try 
        {
            player.play();
        } 
        catch (JavaLayerException e) 
        {
            System.out.println(e.getMessage());
        }   
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by sakonpure6 View Post
    Hi I downloaded the custom library Jlayer to play a music file, my code has no errors however when I run the program, nothing happens. Why?
    The code technically works (tested, changing just only the filename). Do you have an exception at runtime? Keep in mind that "song.mp3" is relative to the "current" directory. What is your current directory depends on how/from where you launch the application.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    sakonpure6 (December 22nd, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by andbin View Post
    What is your current directory depends on how/from where you launch the application.
    Sorry, I did not understand , I tried linking the music.mp3 using this method "C:\\Users\\sakonpure6\\Desktop\\Java Projects\\Sounds\res\\music.mp3" still no sound plays? Btw I only added the jar files of jlayer, do I need to install something extra?

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by sakonpure6 View Post
    Sorry, I did not understand , I tried linking the music.mp3 using this method "C:\\Users\\sakonpure6\\Desktop\\Java Projects\\Sounds\res\\music.mp3" still no sound plays? Btw I only added the jar files of jlayer, do I need to install something extra?
    You need only the jlX.X.X.jar that must be in classpath. And if the absolute path of the mp3 is correct, then it's ok.
    I repeat the question: do you have an exception at runtime?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    sakonpure6 (December 22nd, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by andbin View Post
    You need only the jlX.X.X.jar that must be in classpath. And if the absolute path of the mp3 is correct, then it's ok.
    I repeat the question: do you have an exception at runtime?
    Non at all. The error box in eclipse is empty. Its just that I hear nothing and yes I tested speakers

  8. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by sakonpure6 View Post
    The error box in eclipse is empty.
    Do you mean the "Error Log" / "Problems" view? If there is an exception at runtime, your code prints to standard output, so you must see in the "Console" view.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  9. The Following User Says Thank You to andbin For This Useful Post:

    sakonpure6 (December 22nd, 2013)

  10. #7
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by andbin View Post
    Do you mean the "Error Log" / "Problems" view? If there is an exception at runtime, your code prints to standard output, so you must see in the "Console" view.
    There is nothing in console view

  11. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by sakonpure6 View Post
    There is nothing in console view
    So no compile errors and no exceptions at runtime but ... no sound. At this point I can only think that: a) there's some problem at system level or b) your mp3s are in a format/compression/bit-rate not supported by JLayer. It's more probable the second.

    Try with some free mp3 you can find on internet, for example here: http://www.testsounds.com
    I have tested, with your code, the last "Sinus 440 Hz -10 dB (941 KB)", track90.mp3. And works!
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  12. The Following User Says Thank You to andbin For This Useful Post:

    sakonpure6 (December 22nd, 2013)

  13. #9
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Jlayer, playing mp3!

    Omg I have to be the stupidest guy around. In my volume mixer Java was muted. Thank you so much andbin for your constant feedback, it is highly appreciated.

    Edit: Do you know how to run music at the same time as a JFrame window? Because I am making a game and JFrame won't run at the same time the music is playing. Also, how can you loop , or play another sound file at the same time?

  14. #10
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Jlayer, playing mp3!

    Quote Originally Posted by sakonpure6 View Post
    In my volume mixer Java was muted.
    Well ... I could not help more.

    Quote Originally Posted by sakonpure6 View Post
    Do you know how to run music at the same time as a JFrame window? Because I am making a game and JFrame won't run at the same time the music is playing. Also, how can you loop , or play another sound file at the same time?
    The play() of Player is synchronous, this means that it keeps busy the "current" thread. If the current thread is the Event Dispatch Thread (for example when you are in the context of an actionPerformed for a JButton), it's a very bad thing. The main rule in Swing is to not block the EDT for too much time.

    Solution: start the playing in a new thread. You should have basic knowledge on threads and the implications about the use of multi-threading.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  15. The Following User Says Thank You to andbin For This Useful Post:

    sakonpure6 (December 22nd, 2013)

Similar Threads

  1. Shoutcasting Internet Radio Streaming and playing Mp3 Files in J2me Midlet(Java):
    By pavankumarragi in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 14th, 2012, 02:02 PM
  2. mp3 file cant be resolved
    By aNGaJe in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2012, 07:20 AM
  3. Java mp3 Player
    By aNGaJe in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2011, 09:49 AM
  4. MP3 problems
    By relion65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 15th, 2011, 12:09 PM
  5. Java Mp3 player.
    By alex901 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 18th, 2010, 09:12 AM