本文实例为大家分享了java音乐播放器的具体代码,供大家参考,具体内容如下
这个是源码结构介绍
这个是界面,有点简陋,见笑了,但是基本上的东西都有了,没办法,没有美工的程序写的界面
直接上源代码Player.java
package com.service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.sound.sampled.*;
import javax.swing.JSlider;
import javax.swing.JTable;
import com.list.MusicList;
import com.list.ThreadList;
import com.list.ViewList;
import com.model.Model;
import com.model.Music;
import com.view.View;
/*"duration"
"author"
"title"
"copyright"
"date"
"comment"*/
public class Player extends Thread{
private Player p;
private long time = 0;
Object lock = new Object();//一个空的对象,没什么意义
private boolean paused = false;// 暂停 继续
public boolean isPaused() {
return paused;
}
public void setPaused(boolean paused) {
this.paused = paused;
}
private JSlider jSliderPlayProgress;//播放进度条
private boolean over = false;//开始 结束
//是否自动播放下一曲
private boolean isNext=true;
private Music music;//音乐
AudioInputStream din = null;
SourceDataLine line=null;
private FloatControl volume = null;
private JSlider jSliderVolume;
public JSlider getjSliderVolume() {
return jSliderVolume;
}
public void setjSliderVolume(JSlider jSliderVolume) {
this.jSliderVolume = jSliderVolume;
}
public Player(JSlider jSliderVolume,JSlider jSliderPlayProgress) {
super();
this.jSliderVolume = jSliderVolume;
this.jSliderPlayProgress=jSliderPlayProgress;
}
public Music getMusic() {
return music;
}
public void setMusic(Music music) {
this.music = music;
}
public FloatControl getVolume(){
return volume;
}
//播放音乐
public void run(){
AudioInputStream in=null;
try {
File file = new File(music.getPath());
//播放不了的歌曲,直接下一首,并且在音乐列表中删除
try {
in = AudioSystem.getAudioInputStream(file);
} catch (Exception e) {
//删除有点小问题
MusicList.getList().remove(music.getId());
ViewList.getList().get(0).getJt().setModel(new Model());
nextmusic();
}
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
false);
if(baseFormat.getEncoding()==AudioFormat.Encoding.PCM_UNSIGNED || baseFormat.getEncoding()==AudioFormat.Encoding.ULAW ||
baseFormat.getEncoding()==AudioFormat.Encoding.ALAW || baseFormat.getEncoding()==AudioFormat.Encoding.PCM_SIGNED){
time=(file.length()*8000000)/((int)(decodedFormat.getSampleRate()*baseFormat.getSampleSizeInBits()));
}else{
int bitrate=0;
if(baseFormat.properties().get("bitrate")!=null){
//取得播放速度(单位位每秒)
bitrate=(int)((Integer)(baseFormat.properties().get("bitrate")));
if(bitrate!=0)
time=(file.length()*8000000)/bitrate;
}
}
din = AudioSystem.getAudioInputStream(decodedFormat, in);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open();
setVolume();
jSliderPlayProgress.setMaximum((int)time);
jSliderPlayProgress.setValue(0);
if(line!=null){
line.open(decodedFormat);
byte[] data = new byte[4096];
int nBytesRead;
synchronized (lock) {
while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
while (paused) {
if(line.isRunning()) {
line.stop();
System.out.println("暂停");
}
try {
lock.wait();
System.out.println("等待");
}
catch(InterruptedException e) {
}
}
if(!line.isRunning()&&!over) {
System.out.println("开始播放");
line.start();
}
if (over&&line.isRunning()) {
System.out.println("停止播放");
jSliderPlayProgress.setValue(0);
isNext=false;
line.drain();
line.stop();
line.close();
}
jSliderPlayProgress.setVa