在后台播放音乐 java

play music in background java

我创建了 "SimplePlayer" 播放音乐但它停止了我的应用程序。我想要音频解决方案和我的应用程序(Jframe 类型)同时工作这里是我的 class:

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

public class SimplePlayer {
    public SimplePlayer() {
        try {
            FileInputStream fis = new FileInputStream("C:/Users/Admin/Music/B.B. King - Blues Boys Tune - YouTube.mp3");
            Player playMP3 = new Player(fis);
            playMP3.play();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

将播放器代码放入新线程中然后启动它:

new Thread() {

  @Override
  public void run() {
    //As your stream implements Closeable, it is better to use a "try-with-resources"
    try(FileInputStream fis = new FileInputStream("C:/Users/Admin/Music/B.B. King - Blues Boys Tune - YouTube.mp3")){
      new Player(fis).play();
    }catch(Exception e){System.out.println(e);}
  }
}.start();