Color Picker

A Java Program To Play A MP3 File Using JLayer

Watch video tutorial below :


*********************************************************************
COMPLETE SOURCE CODE FOR : TestPlay.java
*********************************************************************

import javazoom.jl.player.Player;
import java.io.FileInputStream;

public class TestPlay
{
    public static void main(String[]args)
    {
        try
        {
            FileInputStream fis=new FileInputStream("C:\\Documents and Settings\\UserXP\\Desktop\\01-flo_rida_(feat_david_guetta)-clab_cant_handle_me.mp3");
            Player playMp3=new Player(fis);

            playMp3.play();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

11 comments:

  1. Thank you very much for the code, works like a charm :)

    ReplyDelete
  2. but i want so have some method with which i can play my music files in mp3 format in the background of my jframe swing application....whereas your method is concerned, it doesn't allow the programme to proceed furthur till the song gets complete..

    ReplyDelete
  3. Thank you very much..... helped a lot to my project....

    Is there any way, so that i can play any audio format file ??

    ReplyDelete
  4. hi..what about if i want to make a converter of avi to mp3. Is it possible? tnx

    hopin for the reply.. :)

    ReplyDelete
  5. Hi

    Your code is very excellent. It works great. Just a question:

    How can I abilities like stop/pause/.... ?

    Thanks

    ReplyDelete
  6. one more problem:
    when the audio is playing, every thing else stops working. I can't stop it any way. Is there a piece of code I can use to prevent that.

    ReplyDelete
    Replies
    1. Try running it on a thread like this:

      new Thread(new Runnable()
      {

      @Override
      public void run()
      {
      try
      {
      Player player = new Player(new FileInputStream("media/alert.mp3"));
      player.play();
      }catch(FileNotFoundException fileNotFoundException)
      {
      fileNotFoundException.printStackTrace();
      }//end catch
      catch(JavaLayerException javaLayerException)
      {
      javaLayerException.printStackTrace();
      }//end catch
      }//end method run

      }).start();

      This will make it run on the background.

      Delete
  7. it's working.
    thank you...

    ReplyDelete
  8. Try running it on a thread like this:

    new Thread(new Runnable()
    {

    @Override
    public void run()
    {
    try
    {
    Player player = new Player(new FileInputStream("media/alert.mp3"));
    player.play();
    }catch(FileNotFoundException fileNotFoundException)
    {
    fileNotFoundException.printStackTrace();
    }//end catch
    catch(JavaLayerException javaLayerException)
    {
    javaLayerException.printStackTrace();
    }//end catch
    }//end method run

    }).start();

    This will make it run on the background.

    ReplyDelete
  9. anyone here knows how to code music streaming via bluetooth? this is a mobile application. if anyone knows please help me.

    ReplyDelete
  10. How can I use this with a GUI in java? I have the graphical design, but I don't know how to use this code to match my GUI... Do I have make an instance of "TestPlay" in my GUI-class?

    ReplyDelete

Leave a comment