import java.applet.Applet; //音乐播放
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;//文件读写
import javax.swing.text.*; //界面设计
import javax.swing.*;
import static javax.swing.JFrame.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.util.Timer; //定时器
import java.util.TimerTask;
//注释掉的代码为音量控制,没时间弄了
/*package com.lct.utils;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
*/
class audioplay{//播放音乐类
AudioClip adc;// 声音音频剪辑对象
URL url;
boolean adcFlag=false;
void SetPlayAudioPath(String path){
try{
url = new URL(path);
// System.out.println(adc.toString());
if(adcFlag==true){adc.stop();}
adc = Applet.newAudioClip(url);
adcFlag=true;
}
catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
void play(){ //开始播放
adc.play();
}
void loop(){ //循环播放
adc.loop();
}
void stop(){ //停止播放
adc.stop();
}
}
class MyExtendsJFrame extends JFrame implements ActionListener,MouseListener{
audioplay audioPlay=new audioplay();
String currentname;//当前歌曲名称
int currentduration;//当前歌曲时长
int currentplaytime=0;//当前歌曲播放进度
JLabel background;
public MyExtendsJFrame(){
setTitle("音乐播放器");
setBounds(350,110,1280,820);//播放器位置和大小
Image icontitle=Toolkit.getDefaultToolkit().getImage(".//logo.jpg");
setIconImage(icontitle);//设置音乐播放器左上角图标
setResizable(false);
setLayout(null);
init(); //添加控件的操作封装成一个函数
setVisible(true);//放在添加组件后面执行
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//下面的函数作用是美化进度条,可以改变进度条颜色为纯色或渐变色
void beautiful(JSlider slider){
slider.setUI(new javax.swing.plaf.metal.MetalSliderUI(){
@Override
public void paintThumb(Graphics g) {
//绘制指示物
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillOval(thumbRect.x, thumbRect.y, thumbRect.width,
thumbRect.height);//修改为圆形
//也可以帖图(利用鼠标事件转换image即可体现不同状态)
//g2d.drawImage(image, thumbRect.x, thumbRect.y, thumbRect.width,thumbRect.height,null);
}
public void paintTrack(Graphics g) {
//绘制刻度的轨迹
int cy,cw;
Rectangle trackBounds = trackRect;
if (slider.getOrientation() == JSlider.HORIZONTAL) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);//将背景设为黑色
cy = (trackBounds.height/2) - 2;
cw = trackBounds.width;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(trackBounds.x, trackBounds.y + cy);
g2.fillRect(0, -cy + 5, cw, cy);
int trackLeft = 0;
int trackRight = 0;
trackRight = trackRect.width - 1;
int middleOfThumb = 0;
int fillLeft = 0;
int fillRight = 0;
//换算坐标
middleOfThumb = thumbRect.x + (thumbRect.width / 2);
middleOfThumb -= trackRect.x;
if (!drawInverted()) {
fillLeft = !slider.isEnabled() ? trackLeft : trackLeft + 1;
fillRight = middleOfThumb;
} else {
fillLeft = middleOfThumb;
fillRight = !slider.isEnabled() ? trackRight - 1
: trackRight - 2;
}
//设定渐变色
g2.setPaint(new GradientPaint(0, 0, Color.red, cw, 0,
Color.blue, true));
g2.fillRect(0, -cy + 5, fillRight - fillLeft, cy);
g2.setPaint(slider.getBackground());
g2.fillRect(10, 10, cw, 5);
g2.setPaint(Color.yellow);
g2.drawLine(0, cy, cw - 1, cy);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.translate(-trackBounds.x, -(trackBounds.y + cy));
}
else {
super.paintTrack(g);
}
}
});
}
int lrc[]= {6,12,19,25,31,37,45,51,56,64,69,76};//存放每句歌词对应的开始时间
//加入lrcflag是为了解决,如果使用playFileName判断当前歌曲,用户打开文件框不选文件直接关闭时会导致进度条失灵的问题
Timer nTimer;//定时器
public void timerFun(){
if(nTimer!=null) nTimer.cancel();
nTimer = new Timer();
nTimer.schedule(new TimerTask() {
public void run() {
//如果当前播放时间大于等于歌曲时长,并且为循环模式和播放状态
if(currentplaytime>=currentduration && buttoncycle.isSelected()==false && buttonPlay.isSelected()==true) {
int index=musiclist.getAnchorSelectionIndex();//获取歌曲列表中当前歌曲索引
musiclist.setSelectedIndex((index+1)%musicnum);//设置下一首被选中
musicName=musiclist.getSelectedValue();//获得当前被选中的歌曲名
playFile=musicDirectory+musicName;//歌曲路径
audioPlay.SetPlayAudioPath("file:"+playFile);//设置播放文件
setplay();//播放前初始化 ,见void seplay()定义
currentplaytime=0;//歌曲当前时间置零
audioPlay.play();//播放歌曲
timerFun();//开始计时
}
//如果当前播放时间大于等于歌曲时长,并且为单曲循环
if(currentplaytime>=currentduration && buttoncycle.isSelected()==true)
currentplaytime=0;//歌曲当前时间置零
//如果停止播放
if(buttonPlay.isSelected()==false)
{
nTimer.cancel();//取消定时器
currentplaytime=currentduration;//歌曲当前时间等于歌曲时长
if(currentduration/60<10 && currentduration%60<10)//显示歌曲时长
maxArea.setText("0"+currentduration/60+":"+"0"+currentduration%60);
if(currentduration/60<10 && currentduration%60>=10)
maxArea.setText("0"+currentduration/60+":"+currentduration%60);
if(currentduration/60>=10 && currentduration%60>=10)
maxArea.setText(currentduration/60+":"+currentduration%60);
if(currentduration/60>=10 && currentduration%60<10)
maxArea.setText(currentduration/60+":"+"10"+currentduration%60);
}
currentplaytime=currentplaytime+1; //计时
//如果当前播放歌曲为“离人愁”
if(musicName.equals("离人愁.wav"))
for(int i=0;i<12;i++)//遍历歌词时间数组
{
if(currentplaytime==lrc[i])//如果当前时间等于数组中的某一个歌词时间
{
lrclist.setSelectedIndex(i);//设置此句歌词为选中状态,即显示不同颜色
}
}
musiclist.setSelectedIndex(vtlist.indexOf(musicName));//持续保持当前播放歌曲为选中状态
playtime.setValue(currentplaytime);//设置进度条当前值
//显示歌曲当前播放时间
if(currentplaytime/60<10 && currentplaytime%60<10)
currenttimeArea.setText("0"+currentplaytime/60+":"+"0"+currentplaytime%60);
if(currentplaytime/60>=10 && currentplaytime%60<10)
currenttimeArea.setText(currentplaytime/60+":"+"0"+currentplaytime%60);
if(currentplaytime/60>=10 && currentplaytime%60>=10)
currenttimeArea.setText(currentplaytime/60+":"+currentplaytime%60);
if(currentplaytime/60<10 && currentplaytime%60>=10)
currenttimeArea.setText("0"+currentplaytime/60+":"+currentplaytime%60);
}
},0,1000);//每1秒执行1次
}
JButton buttonfile;//打开文件按钮
JButton buttonLast;//上一曲按钮
JToggleButton buttonPla
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MisicPlayer.zip (31个子文件)
MisicPlayer
file.jpg 9KB
.project 387B
list.jpg 9KB
play.jpg 13KB
nomute.jpg 9KB
山外小楼夜听雨.wav 16.91MB
飘洋过海.wav 9.04MB
single.jpg 10KB
src
MusicPlayer.java 28KB
player0.gif 1.92MB
nolist.jpg 10KB
player.gif 757KB
.settings
org.eclipse.jdt.core.prefs 598B
background.jpg 91KB
logo.jpg 9KB
stop.jpg 13KB
nolrc.jpg 10KB
mute.jpg 9KB
烟花易冷.wav 12.88MB
三生三世.wav 16.25MB
离人愁.wav 13.79MB
next.jpg 11KB
cyc.jpg 9KB
.classpath 301B
last.jpg 11KB
lrc.jpg 9KB
bin
MyExtendsJFrame.class 13KB
MyExtendsJFrame$1.class 3KB
MyExtendsJFrame$2.class 3KB
audioplay.class 1KB
MusicPlayer.class 452B
共 31 条
- 1
资源评论
SufjanC
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功