简体   繁体   English

使用JLayer循环使用Java的背景音乐

[英]Loop background music in Java using JLayer

I'm trying to add background music to my game using JLayer. 我正在尝试使用JLayer为我的游戏添加背景音乐。 How can I to set it to play in a loop? 如何将其设置为循环播放?

http://www.javazoom.net/javalayer/javalayer.html 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 我有这个方法来播放我的mp3文件

 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 你需要的是使用线程和jlayer.jar和mp3plugin.jar加上jaudiotagger.jar然后你可以播放mp3文件作为你的背景音乐

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 我有另一种方法在背景播放音频文件,但这种方法是播放.wav声音很容易你可以看看一个想法,你可以使用更好的线程,以获得出色的背景播放性能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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