在背景java中播放音乐

时间:2015-03-23 13:34:51

标签: java audio background

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

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

1 个答案:

答案 0 :(得分:0)

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

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