用java写一个音乐播放器
时间: 2023-12-22 19:03:24 浏览: 52
好的,以下是一个简单的Java音乐播放器实现:
```java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class MusicPlayer {
private Player player;
private FileInputStream fileInputStream;
public void play(String filename) throws FileNotFoundException, JavaLayerException {
fileInputStream = new FileInputStream(filename);
player = new Player(fileInputStream);
player.play();
}
public void stop() {
if (player != null) {
player.close();
}
}
public static void main(String[] args) {
MusicPlayer musicPlayer = new MusicPlayer();
try {
musicPlayer.play("music.mp3"); // 替换成你要播放的音乐文件名
} catch (FileNotFoundException | JavaLayerException e) {
e.printStackTrace();
}
}
}
```
这个播放器使用了JavaZoom的jl库来播放mp3文件,需要先将库文件引入项目中。你也可以使用其他的音频库来实现播放器。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)