user2287319
user2287319

Reputation: 45

Loop background music in Java using JLayer

I'm trying to add background music to my game using JLayer. How can I to set it to play in a loop?

http://www.javazoom.net/javalayer/javalayer.html

    BackgroundMusic bm = new BackgroundMusic("music.mp3");
    bm.start();

Upvotes: 1

Views: 3661

Answers (2)

Tech Nerd
Tech Nerd

Reputation: 832

I have another method to play the audio files while at background but this method is for to play .wav sound easily you can take a look for an idea and further you can use better thread for brilliant background playing performance

 private void playAudioForMenuDropDown(){
    String gongFile = "E://Net Beans Work Space//My Computing Fellow//src    //computingsounds//menu drop down.wav";
        InputStream in = null;
    try {
        in = new FileInputStream(gongFile);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ComputingFellowJobs.class.getName()).log(Level.SEVERE, null, ex);
    }

        // create an audiostream from the inputstream
        AudioStream audioStream = null;
    try {
        audioStream = new AudioStream(in);
    } catch (IOException ex) {
        Logger.getLogger(ComputingFellowJobs.class.getName()).log(Level.SEVERE, null, ex);
    }

        // play the audio clip with the audioplayer class
        AudioPlayer.player.start(audioStream);
}

Upvotes: 2

Tech Nerd
Tech Nerd

Reputation: 832

I have this method to play my mp3 file using

 jlayer.jar 
 mp3plugin.jar
 jaudiotagger-2.0.1.jar

  private void playMe(){
  try {

      File file=new File("F:\\Net Beans Work Space\\mp3\\a.mp3");
      FileInputStream fis     = new FileInputStream(file);

      BufferedInputStream bis = new BufferedInputStream(fis);
      player = new Player(bis);

      int d=0;
      AudioFile audioFile = AudioFileIO.read(file);
      d = audioFile.getAudioHeader().getTrackLength();

      System.out.print("ddd=   "+d)  ;

      player.play();
 } catch(Exception e){
      System.out.print("ERROR "+e);

 }

   }

what you need is to use thread and jlayer.jar and mp3plugin.jar plus jaudiotagger.jar then you can play mp3 files as your background music

Upvotes: 2

Related Questions