class="java" name="code">
package com.xu.musicplayer.player;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
/**
* Java 播放音频
* @ClassName: MusicPlayer
* @Description: TODO
* @author: hyacinth
* @date: 2020年3月5日 上午12:10:53
* @Copyright: hyacinth
*/
public class MusicPlayer {
static Player player = null;
public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
File file = new File("C:\\Users\\hyacinth\\Desktop\\Work\\花涵 - 假行僧.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream stream = new BufferedInputStream(fis);
Player player = new Player(stream);
player.play();
}
/**
* 播放 20 秒并结束播放
*/
public void play() {
new Thread(new Runnable() {
@Override
public void run() {
try {
File file = new File("C:\\Users\\hyacinth\\Desktop\\Work\\花涵 - 假行僧.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream stream = new BufferedInputStream(fis);
player = new Player(stream);
player.play();
} catch (Exception e) {
// TODO: handle exception
}
}
}).start();
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
player.close();
}
}
https://blog.csdn.net/qq_34814092/article/details/80889813