简体   繁体   中英

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();

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM