利用java制作简单的音乐播放器
作者:StrictModel 发布时间:2022-01-21 11:39:32
标签:java,播放器
本文主要是用到java中的swing技术,以及JMFjar中的API,为大家分享了java音乐播放器的具体实现代码,供大家参考,具体内容如下
备注:需要用JDK1.8才能播放音乐MP3
package baidu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.media.bean.playerbean.*; //这个包要用到JMF
public class MP3 extends JFrame implements Runnable {
public JLabel shijian;
private JSlider sldDiameter;
public MediaPlayer soumd1;
public JButton playSound, loopSound, stopsound;
public JButton tjian, shanc, baocun, duqu;
public JPanel jp1, jp2, jp3, jp4;
public JList jl;
int zongmiao=0;
public Vector vector, mingcheng;
boolean fo = false, geshi = false;
JLabel jl1, jl2, sj1, sj2;
JTextField jt1, jt2;
JButton queding, xiugai;
int zong = 0;
int a = 0, b = 0, you = 1,mm=0;
int fenzhong, miaozhong;
public MP3() {
super("java简单音乐播放器--严楷");
soumd1 = new MediaPlayer();
Container c = getContentPane();
c.setLayout(new FlowLayout());
mingcheng = new Vector();
jp1 = new JPanel();
shijian = new JLabel();
jp1.add(shijian);
c.add(jp1);
playSound = new JButton("开始播放");
loopSound = new JButton("循环播放");
stopsound = new JButton("停止播放");
jp2 = new JPanel();
jp2.add(playSound);
jp2.add(loopSound);
jp2.add(stopsound);
c.add(jp2);
jp4 = new JPanel();
sj1 = new JLabel();
sj2 = new JLabel();
sldDiameter = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
sldDiameter.setMajorTickSpacing(1);
sldDiameter.setPaintTicks(true);
jp4.add(sj1);
jp4.add(sldDiameter);
jp4.add(sj2);
c.add(jp4);
vector = new Vector();
jl = new JList(mingcheng);
jl.setVisibleRowCount(5);
jl.setFixedCellHeight(40);
jl.setFixedCellWidth(265);
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.add(new JScrollPane(jl));
tjian = new JButton("添加");
shanc = new JButton("删除");
duqu = new JButton("读取");
baocun = new JButton("保存");
jp3 = new JPanel();
jp3.add(tjian);
jp3.add(shanc);
jp3.add(baocun);
jp3.add(duqu);
c.add(jp3);
try {
String s =" C:/Users/Administrator/Music/卓依婷 - 萍聚.mp3";
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(s));
lujin a1 = (lujin) input.readObject();
mingcheng = a1.b;
vector = a1.a;
jl.setListData(mingcheng);
} catch (Exception e) {
System.out.println("c盘没有保存音乐文件");
//e.printStackTrace();
}
baocun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
if (fileChooser.showSaveDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到选择文件或目录的绝对路径
mmm(vector, mingcheng, fileName);
}
}
});
duqu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
if (fileChooser.showOpenDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到选择文件或目录的绝对路径
try {
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(fileName));
lujin a1 = (lujin) input.readObject();
mingcheng = a1.b;
vector = a1.a;
jl.setListData(mingcheng);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
playSound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException eee) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException ew) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
zongmiao=(int)soumd1.getDuration().getSeconds();
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有错,无法播放 建议删除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已经不存在,建议删除");
}
else
JOptionPane.showMessageDialog(null, "请选择音乐文件");
}
});
loopSound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException we) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException we) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
zongmiao=(int)soumd1.getDuration().getSeconds();
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有错,无法播放 建议删除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已经不存在,建议删除");
}
else
JOptionPane.showMessageDialog(null, "请选择音乐文件");
}
});
stopsound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = 0;
b = 0;
you = 0;
mm=0;
geshi=false;
fo = false;
sldDiameter.setMaximum(100);
sldDiameter.setValue(0);
sj1.setText(null);
sj2.setText(null);
if (jl.getSelectedIndex() >= 0)
soumd1.stop();
}
});
tjian.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) { // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".mp3")
|| file.isDirectory();
}
public String getDescription() { // 文件描述
return "音乐文件(*.mp3)";
}
});
if (fileChooser.showOpenDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到选择文件或目录的绝对路径
vector.add(fileName);
StringBuffer buffer = daoxu(fileName);
mingcheng.add(buffer);
}
}
});
shanc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (jl.getSelectedIndex() >= 0) {
mingcheng.removeElementAt(jl.getSelectedIndex());
vector.removeElementAt(jl.getSelectedIndex());
jl.setListData(mingcheng);
}
}
});
jl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
if (event.getClickCount() == 2) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有错,无法播放 建议删除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已经不存在,建议删除");
}
}
if (event.isMetaDown()) {
if (jl.getSelectedIndex() >= 0) {
int a = jl.getSelectedIndex();
xiugai x = new xiugai();
jt1.setText(mingcheng.get(jl.getSelectedIndex()) + "");
jt2.setText(vector.get(jl.getSelectedIndex()) + "");
}
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String s = "c:\\music.txt";
mmm(vector, mingcheng, s);
System.exit(0);
}
});
setSize(300, 400);
setVisible(true);
}
public void run() {
while (true) {
Date now = new Date();
//shijian.setText("当前时间: " + now.getYear()+"年"+now.getMonth()+"月"+now.getDate()+"日"+now.getTime());
shijian.setText("当前时间:"+(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())+" 严楷");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if(geshi){
you=(int)soumd1.getMediaTime().getSeconds();
mm =you%60;
if(you!=0)
if(you%60==0)
{
b++;
}
sj1.setText(b+":"+mm);
sldDiameter.setMaximum(zongmiao);
if(you!=zongmiao)
sldDiameter.setValue(you);
else {
sldDiameter.setValue(0);
mm=0;
b=0;
}
}
}
}
public StringBuffer daoxu(String fileName) {
String mc = "";
for (int i = fileName.length(); i >= 1; i--) {
if (fileName.charAt(i - 1) == '\\')
break;
mc += fileName.charAt(i - 1);
}
StringBuffer buffer = new StringBuffer(mc);
StringBuffer mm = buffer.reverse();
return mm;
}
public void mmm(Vector vector, Vector mingcheng, String lujin) {
lujin a = new lujin(vector, mingcheng);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(lujin));
output.writeObject(a);
output.flush();
output.close();
} catch (Exception e) {
}
}
public String fen(int yy) {
fenzhong = (int) yy / 60;
miaozhong = (int) yy % 60;
String sss = fenzhong + ":" + miaozhong;
return sss;
}
public static void main(String agrs[]) {
MP3 s = new MP3();
Thread t1 = new Thread(s);
t1.start();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class xiugai extends JFrame {
public xiugai() {
jl1 = new JLabel("文件名");
jt1 = new JTextField(20);
jl2 = new JLabel("文件路径");
jt2 = new JTextField(20);
xiugai = new JButton("修改");
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel j1 = new JPanel();
JPanel j2 = new JPanel();
JPanel j3 = new JPanel();
j1.add(jl1);
j1.add(jt1);
j2.add(jl2);
j2.add(jt2);
j3.add(xiugai);
c.add(j1);
c.add(j2);
c.add(j3);
xiugai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
mingcheng.setElementAt(jt1.getText(), jl.getSelectedIndex());
vector.setElementAt(jt2.getText(), jl.getSelectedIndex());
jl.setListData(mingcheng);
dispose();
}
});
setSize(300, 120);
setVisible(true);
}
}
}
class lujin implements Serializable {
Vector a = new Vector();
Vector b = new Vector();
public lujin(Vector vector, Vector mingcheng) {
a = vector;
b = mingcheng;
}
}
0
投稿
猜你喜欢
- 欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demo
- 前言在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成sw
- 需求:学生输入姓名和语文、数学、英语,编程求出总分和平均分,并在屏幕上显示XX的总分和平均分using System;using Syste
- 从接收输入值说起在日常的开发应用中,有时候需要直接接收外部设备如键盘等的输入值,而对于这种数据的接收方式,我们一般有三种方法:字节流读取,字
- 本文实例讲述了C#中Winform窗体Form的关闭按钮变灰色的方法,对C#程序设计有一定的借鉴价值,分享给大家供大家参考之用。具体方法如下
- 1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个Serv
- 需求:视频通话界面,两个surfaceView一个显示本端的视图,另一个显示对端的视图,由于显示比例的问题总会存在一个覆盖另一个的问题,为保
- 泛型泛型的语法定义class 类名 <泛型标识,泛型标识,…>{ private 泛型标识1,变量名;常用
- thinking in java3中的多态People are often confused by other, non-object-or
- 1、String 是最基本的数据类型吗?不是。Java 中的基本数据类型只有 8 个 :byte、short、int、long、float、
- 1 支持文件类型.txt.html.htm.xml.bytes.json.csv.yaml.fnt2 寻找文件1 //Load textur
- 1.一段简单的代码首先来一段代码,这个是单例模式,可能有的人不知道什么是单例模式,我就简单说一下单例模式是指一个类有且只有一种对象实例。这里
- 什么是栈和队列栈如果用数组模拟的话是类似于一个U形桶状堆栈空间,地下是封口的,只能从顶部一个地方进出,它的进出都是有顺序的,看下图:如果是进
- 一、Android前端实现新建一个login的项目,主要的几个文件在这里1、gradle引入OKhttp3依赖implementation
- 基本操作示例VectorApp.javaimport java.util.Vector; import java.lang.*; impor
- Spring整合Myabtis思路的分析引入相关依赖SpringMyabtismysqlMybatsi-spring…
- 本文实例讲述了Winform中Treeview实现按需加载的方法,非常具有实用价值。分享给大家供大家参考。具体分析如下:最近项目里用到tre
- 本文实例讲述了Android结束进程的方法。分享给大家供大家参考,具体如下:最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这个管
- @JsonInclude(JsonInclude.Include.NON_NULL)不起作用记录一下使用@JsonInclude(JsonI
- 本人小菜一个。目前只见过两种弹出框的实现方式,第一种是最常见的PopupWindow,第二种也就是Activity的方式是前几天才见识过。感