综合利用Swing界面和自己写音乐播放类,设计音乐播放器如下:(1)纯Swing给定的组件可以设计出一个普通的音乐播放器界面。(2)音乐播放方面,不能调用其他第三方jar包,只能用JDK底层自带的API类,例如JavaSound。使用JavaSound API,可以实现各种基于声音的应用,例如声音录制、音乐播放、网络电话、音乐编辑等。JavaSound API又以各种解码和合成器SPI(服务提供者接口, Service Provider Interface)为基础,实现各种音乐格式的解码与转码。 给出java代码
时间: 2024-03-20 08:38:45 浏览: 57
基于java的音乐播放器的设计
3星 · 编辑精心推荐
好的,下面是一个基于JavaSound API的音乐播放器的示例代码。代码中包含了一个基本的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.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MusicPlayer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton btnOpen;
private JButton btnPlay;
private JButton btnPause;
private JButton btnStop;
private JButton btnPrev;
private JButton btnNext;
private AudioInputStream audioInputStream;
private SourceDataLine line;
private boolean playing;
private boolean paused;
private int currentFrame;
private File[] playlist;
private int currentIndex;
public MusicPlayer() throws LineUnavailableException {
super("Music Player");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 100);
setResizable(false);
JPanel panel = new JPanel(new BorderLayout());
btnOpen = new JButton("Open");
btnOpen.addActionListener(this);
panel.add(btnOpen, BorderLayout.WEST);
btnPlay = new JButton("Play");
btnPlay.addActionListener(this);
panel.add(btnPlay, BorderLayout.CENTER);
btnPause = new JButton("Pause");
btnPause.addActionListener(this);
panel.add(btnPause, BorderLayout.EAST);
btnStop = new JButton("Stop");
btnStop.addActionListener(this);
panel.add(btnStop, BorderLayout.SOUTH);
btnPrev = new JButton("Prev");
btnPrev.addActionListener(this);
panel.add(btnPrev, BorderLayout.NORTH);
btnNext = new JButton("Next");
btnNext.addActionListener(this);
panel.add(btnNext, BorderLayout.SOUTH);
add(panel);
playing = false;
paused = false;
currentFrame = 0;
currentIndex = 0;
}
public static void main(String[] args) throws LineUnavailableException {
MusicPlayer player = new MusicPlayer();
player.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnOpen) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
playlist = fileChooser.getSelectedFiles();
currentIndex = 0;
playFile(playlist[currentIndex]);
}
} else if (e.getSource() == btnPlay) {
if (paused) {
paused = false;
} else if (!playing) {
playFile(playlist[currentIndex]);
}
} else if (e.getSource() == btnPause) {
paused = true;
} else if (e.getSource() == btnStop) {
stopPlaying();
} else if (e.getSource() == btnPrev) {
if (currentIndex > 0) {
currentIndex--;
stopPlaying();
playFile(playlist[currentIndex]);
}
} else if (e.getSource() == btnNext) {
if (currentIndex < playlist.length - 1) {
currentIndex++;
stopPlaying();
playFile(playlist[currentIndex]);
}
}
}
private void playFile(File file) {
try {
audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
byte[] buffer = new byte[4096];
int n = 0;
line.start();
playing = true;
while (n != -1 && playing) {
n = audioInputStream.read(buffer, 0, buffer.length);
if (n > 0) {
currentFrame += n;
line.write(buffer, 0, n);
if (paused) {
line.stop();
line.flush();
}
}
}
line.stop();
line.flush();
line.close();
audioInputStream.close();
playing = false;
paused = false;
} catch (IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
private void stopPlaying() {
if (playing) {
playing = false;
paused = false;
line.stop();
line.flush();
line.close();
currentFrame = 0;
}
}
}
```
这段代码中,我们定义了一个名为MusicPlayer的类,继承自JFrame,实现了ActionListener接口。在构造函数中,我们创建了一个Swing界面,并初始化了播放器的状态。在actionPerformed方法中,我们实现了各个按钮的响应事件。
在playFile方法中,我们使用JavaSound API读取音乐文件并创建SourceDataLine,然后通过循环读取音乐数据并写入到SourceDataLine中,实现了音乐的播放功能。在stopPlaying方法中,我们停止了当前正在播放的音乐。
希望这个示例代码能够帮助你完成音乐播放器的设计。
阅读全文