今天学到了如何用java播放一首音乐,具体代码如下,不过里面需要用到 jl1.0.1.jar这个jar包,具体代码如下
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class PlayMusic {
Player player;
private String path;
public PlayMusic(String path){
this.path = path;
}
public void play() throws JavaLayerException{
try{
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(path));
player = new Player(buf);
player.play();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws JavaLayerException{
new PlayMusic("./music/3.mp3").play();
}
}
如果上面的方法不行,就用下面这个简单的
InputStream inputStream = new FileInputStream(new File("D:\\test\\松尾早人 - 決戦~重なり合う運命~.wav"));
AudioPlayer.player.start(inputStream);