上學(xué)期老師布置了一個(gè)音樂(lè)播放器的作業(yè),自己獨(dú)立寫(xiě)的界面感覺(jué)還行就傳上來(lái)了。
package Music;
import javax.sound.sampled.*;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Musicrun {
JFrame frame = new JFrame("音樂(lè)播放器");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
Border etched = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etched, "");
JButton jButton1 = new JButton("Start");
JButton jButton2 = new JButton("Stop");
JButton jButton3 = new JButton("Continue");
boolean bofang = false;
JLabel yinliang = new JLabel();
JLabel musicImage = new JLabel();
float ylforce = 0;
double atime = 0;
double bftime = 0;
public String musicPath = "D:\\zuoye\\java\\resource\\test.wav";
public AudioPlayer music = null;
JSlider slider1 = new JSlider();
JProgressBar progressBar = new JProgressBar();
ImageIcon icon[] = { new ImageIcon("D:\\zuoye\\java\\resource\\b1.png"),
new ImageIcon("D:\\zuoye\\java\\resource\\b2.png"),
new ImageIcon("D:\\zuoye\\java\\resource\\b3.png"),
new ImageIcon("D:\\zuoye\\java\\resource\\b4.png"),
new ImageIcon("D:\\zuoye\\java\\resource\\b5.png") };
public Musicrun() {
frame.setSize(800, 800);
frame.setLayout(new GridLayout(3, 1));
border = BorderFactory.createTitledBorder("播放進(jìn)度");
p2.setBorder(border);
border = BorderFactory.createTitledBorder("音量控制");
p3.setBorder(border);
init();
frame.getContentPane().add(p1);
frame.getContentPane().add(p2);
frame.getContentPane().add(p3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void init() {
// p1部分
musicImage.setIcon(icon[0]);
p1.add(musicImage);
Timer timer = new Timer();
// timer.schedule(new MusicTask(), 500,1000);
timer.schedule(new MusicTask(), 500, 2000);
// p2部分
music = new AudioPlayer(musicPath);
progressBar.setMaximum(100);
progressBar.setMinimum(0);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(false);
progressBar.setPreferredSize(new Dimension(780, 30));
;
;
p2.add(progressBar);
Timer timer2 = new Timer();
timer2.schedule(new MusicTask2(), 0, 100);
jButton1.setSize(80, 40);
jButton2.setSize(80, 40);
jButton3.setSize(80, 40);
p2.add(jButton1);
p2.add(jButton2);
p2.add(jButton3);
jButton1.addActionListener(new ButtonHandler());
jButton2.addActionListener(new ButtonHandler1());
jButton3.addActionListener(new ButtonHandler2());
// p3部分
yinliang.setText("當(dāng)前音量為:" + slider1.getValue());
// yinliang.setBounds(0,630,100,20);
p3.setLayout(new BorderLayout());
p3.add(yinliang, BorderLayout.WEST);
slider1.setValue(100);
slider1.setPaintTicks(true);// setPaintTicks()方法是設(shè)置是否在JSlider加上刻度,若為true則下面兩行才有作用。
slider1.setMajorTickSpacing(20);
slider1.setMinorTickSpacing(5);
slider1.setPaintLabels(true);// setPaintLabels()方法為設(shè)置是否數(shù)字標(biāo)記,若設(shè)為true,則JSlider刻度上就會(huì)有數(shù)值出現(xiàn)。
slider1.setPaintTrack(true);// setPaintTrack()方法表示是否出現(xiàn)滑動(dòng)桿的橫桿。默認(rèn)值為true.
slider1.setSnapToTicks(true);// setSnapToTicks()方法表示一次移動(dòng)一個(gè)小刻度,而不再是一次移動(dòng)一個(gè)單位刻度。
// JLabel label1 = new JLabel("目前刻度:" + );
slider1.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
yinliang.setText("當(dāng)前音量:" + slider1.getValue());
ylforce = (float) ((0.86) * slider1.getValue() - 80);
music.setVol(ylforce);
}
});
p3.add(slider1, BorderLayout.SOUTH);
}
public class MusicTask extends TimerTask {
// TODO 自動(dòng)生成的方法存根
int i = 0;
@Override
public void run() {
musicImage.setIcon(icon[i]);
i++;
if (i == 5) {
i = 0;
}
}
}
public class MusicTask2 extends TimerTask {
// TODO 自動(dòng)生成的方法存根
// double progressValues;
int progressBarValues;
@Override
public void run() {
if (bofang == true) {
bftime += 0.1;
progressBarValues = (int) ((bftime / atime) * 100);
progressBar.setValue(progressBarValues);
}
}
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (bofang == false) {
bofang = true;
bftime = 0;
music.start(bofang);
} else {
music.start(bofang);
}
}
}
private class ButtonHandler1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (bofang == true) {
bofang = false;
music.stop();
} else {
music.stop();
}
}
}
private class ButtonHandler2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (bofang == false) {
bofang = true;
music.continues();
} else {
music.continues();
}
}
}
public class AudioPlayer {
private String musicPath; // 音頻文件
private volatile boolean run = true; // 記錄音頻是否播放
private Thread mainThread; // 播放音頻的任務(wù)線程
private float newVolumn = 7;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceDataLine;
public AudioPlayer(String musicPath) {
this.musicPath = musicPath;
prefetch();
}
// 數(shù)據(jù)準(zhǔn)備
private void prefetch() {
try {
// 獲取音頻輸入流
audioStream = AudioSystem.getAudioInputStream(new File(musicPath));
// 獲取音頻的編碼對(duì)象
audioFormat = audioStream.getFormat();
// 包裝音頻信息
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,
AudioSystem.NOT_SPECIFIED);
// 使用包裝音頻信息后的Info類創(chuàng)建源數(shù)據(jù)行,充當(dāng)混頻器的源
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
// 獲得音頻的總時(shí)長(zhǎng)
atime = audioStream.getFrameLength() / audioFormat.getSampleRate();
sourceDataLine.open(audioFormat);
sourceDataLine.start();
} catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 析構(gòu)函數(shù):關(guān)閉音頻讀取流和數(shù)據(jù)行
protected void finalize() throws Throwable {
super.finalize();
sourceDataLine.drain();
sourceDataLine.close();
audioStream.close();
}
// 播放音頻:通過(guò)loop參數(shù)設(shè)置是否循環(huán)播放
private void playMusic(boolean loop) throws InterruptedException {
try {
if (loop) {
while (true) {
playMusic();
bftime = 0;
}
} else {
playMusic();
// 清空數(shù)據(jù)行并關(guān)閉
sourceDataLine.drain();
sourceDataLine.close();
audioStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void playMusic() {
try {
synchronized (this) {
run = true;
}
// 通過(guò)數(shù)據(jù)行讀取音頻數(shù)據(jù)流,發(fā)送到混音器;
// 數(shù)據(jù)流傳輸過(guò)程:AudioInputStream -> SourceDataLine;
audioStream = AudioSystem.getAudioInputStream(new File(musicPath));
int count;
byte tempBuff[] = new byte[1024];
while ((count = audioStream.read(tempBuff, 0, tempBuff.length)) != -1) {
synchronized (this) {
while (!run)
wait();
}
sourceDataLine.write(tempBuff, 0, count);
}
} catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// 暫停播放音頻
private void stopMusic() {
synchronized (this) {
run = false;
notifyAll();
}
}
// 繼續(xù)播放音樂(lè)
private void continueMusic() {
synchronized (this) {
run = true;
notifyAll();
}
}
// 外部調(diào)用控制方法:生成音頻主線程;
public void start(boolean loop) {
mainThread = new Thread(new Runnable() {
public void run() {
try {
playMusic(loop);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
mainThread.start();
}
// 外部調(diào)用控制方法:暫停音頻線程
public void stop() {
new Thread(new Runnable() {
public void run() {
stopMusic();
}
}).start();
}
// 外部調(diào)用控制方法:繼續(xù)音頻線程
public void continues() {
new Thread(new Runnable() {
public void run() {
continueMusic();
}
}).start();
}
// 播放器的狀態(tài)
public boolean isPlaying() {
return run;
}
// 設(shè)置音頻音量
// https://zhidao.baidu.com/question/269020584.html
public void setVol(float value) {
newVolumn = value;
// 必須open之后
if (newVolumn != 7) {
FloatControl control = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
// System.out.println(control.getMaximum());
// System.out.println(control.getMinimum());
control.setValue(newVolumn);
}
}
// 銷毀
public void destroy() {
try {
finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
然后底下是主函數(shù)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-517718.html
package Music;
public class JavaMain {
public static void main(String[] args) {
// TODO 自動(dòng)生成的方法存根
new Musicrun();
}
}
關(guān)于進(jìn)度條進(jìn)度的問(wèn)題,可以利用計(jì)時(shí)器統(tǒng)計(jì)當(dāng)前已經(jīng)播放的時(shí)間。然后利用函數(shù)計(jì)算音樂(lè)播放的總時(shí)間。兩者相除就能得出當(dāng)前的進(jìn)度了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-517718.html
到了這里,關(guān)于java實(shí)現(xiàn)音樂(lè)播放器(調(diào)整顯示音量大小、調(diào)整進(jìn)度、圖片切換)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!