java音乐播放器源码
时间: 2023-08-04 16:02:52 浏览: 57
以下是一个基于Java Swing的简单音乐播放器的源码,仅供参考:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MusicPlayer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton playButton, stopButton, nextButton;
private JLabel songNameLabel;
private File[] playlist;
private int currentSong;
private Clip clip;
public MusicPlayer() {
super("Java Music Player");
panel = new JPanel();
panel.setLayout(new BorderLayout());
playButton = new JButton("Play");
playButton.addActionListener(this);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
songNameLabel = new JLabel();
panel.add(playButton, BorderLayout.WEST);
panel.add(stopButton, BorderLayout.CENTER);
panel.add(nextButton, BorderLayout.EAST);
panel.add(songNameLabel, BorderLayout.SOUTH);
add(panel);
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public void loadPlaylist() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3 & WAV Files", "mp3", "wav");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
playlist = chooser.getSelectedFiles();
currentSong = 0;
songNameLabel.setText("Playing: " + playlist[currentSong].getName());
}
}
public void playSong() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(playlist[currentSong].getAbsoluteFile());
clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, audioInputStream.getFormat()));
clip.open(audioInputStream);
clip.start();
songNameLabel.setText("Playing: " + playlist[currentSong].getName());
setTitle("Java Music Player - " + playlist[currentSong].getName());
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
public void stopSong() {
if (clip != null) {
clip.stop();
songNameLabel.setText("");
setTitle("Java Music Player");
}
}
public void nextSong() {
if (playlist != null && playlist.length > 1) {
currentSong++;
if (currentSong >= playlist.length) {
currentSong = 0;
}
stopSong();
playSong();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == playButton) {
if (playlist == null) {
loadPlaylist();
}
if (playlist != null) {
playSong();
}
} else if (e.getSource() == stopButton) {
stopSong();
} else if (e.getSource() == nextButton) {
nextSong();
}
}
public static void main(String[] args) {
new MusicPlayer();
}
}
```
该程序使用了Java的Swing GUI库来创建用户界面,可以选择多个音频文件作为播放列表,支持播放、停止和切换歌曲等操作。需要注意的是,该程序只支持MP3和WAV格式的音频文件。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)