一个简单的音乐播放器的java代码.docx

上传人:b****6 文档编号:6003693 上传时间:2023-01-02 格式:DOCX 页数:13 大小:16.62KB
下载 相关 举报
一个简单的音乐播放器的java代码.docx_第1页
第1页 / 共13页
一个简单的音乐播放器的java代码.docx_第2页
第2页 / 共13页
一个简单的音乐播放器的java代码.docx_第3页
第3页 / 共13页
一个简单的音乐播放器的java代码.docx_第4页
第4页 / 共13页
一个简单的音乐播放器的java代码.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

一个简单的音乐播放器的java代码.docx

《一个简单的音乐播放器的java代码.docx》由会员分享,可在线阅读,更多相关《一个简单的音乐播放器的java代码.docx(13页珍藏版)》请在冰豆网上搜索。

一个简单的音乐播放器的java代码.docx

一个简单的音乐播放器的java代码

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

importjava.util.*;

importjavax.swing.*;

importjavax.media.bean.playerbean.*;

importjava.io.IOException;

importjavax.swing.JOptionPane;

importjava.awt.*;

publicclasslytextendsJFrameimplementsRunnable{

publicMediaPlayersoumd1;

privateJButtonplaySound,stopsound,tjian,loopSound;

privateJMenuItemjmiOpen,jmiSave,jmiExit,jmiAbout;

publicJPaneljp1,jp2,jp3,jp4;

publicJListjl;

privateJButtonlast,next,stop;

publicVectorvector,mingcheng;

booleanfo=false,geshi=false;

JLabeljl1,jl2,sj1,sj2;

JTextFieldjt1,jt2;

inta=0,b=0,you=1,mm=0;

publiclyt(){

super("200842018java音乐播放器");

soumd1=newMediaPlayer();

Containerc=getContentPane();

c.setLayout(newFlowLayout());

mingcheng=newVector();

jp1=newJPanel();

c.add(jp1);

JMenuBarmb=newJMenuBar();

setJMenuBar(mb);

JMenufileMenu=newJMenu("文件");

mb.add(fileMenu);

JMenuhelpMenu=newJMenu("帮助");

mb.add(helpMenu);

fileMenu.add(jmiOpen=newJMenuItem("打开"));

fileMenu.add(jmiSave=newJMenuItem("播放"));

helpMenu.add(jmiAbout=newJMenuItem("关于"));

JPaneljp2=newJPanel();

jp2.setLayout(newFlowLayout());

jp2.add(last=newJButton("后退"));

jp2.add(playSound=newJButton("播放"));

jp2.add(tjian=newJButton("添加"));

jp2.add(next=newJButton("前进"));

jp2.add(stopsound=newJButton("停止"));

jp2.add(loopSound=newJButton("循环"));

getContentPane().add(jp2,BorderLayout.SOUTH);

jp2=newJPanel();

jp2.add(playSound);

jp2.add(stopsound);

c.add(jp2);

jp4=newJPanel();

sj1=newJLabel();

sj2=newJLabel();

jp4.add(sj1);

jp4.add(sj2);

c.add(jp4);

vector=newVector();

jl=newJList(mingcheng);

jl.setVisibleRowCount(4);

jl.setFixedCellHeight(30);

jl.setFixedCellWidth(265);

jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)

;

c.add(newJScrollPane(jl));

jp3=newJPanel();

jp3.add(tjian);

c.add(jp3);

try{

Strings="d:

\\music.txt";

ObjectInputStreaminput=newObjectInputStream(

newFileInputStream(s));

lujina1=(lujin)input.readObject();

mingcheng=a1.b;

vector=a1.a;

jl.setListData(mingcheng);

}

catch(Exceptione){

System.out.println("D盘没有保存音乐文件");

}

jmiOpen.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventevent){

JFileChooserfileChooser=newJFileChooser();

fileChooser

.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES

);

fileChooser.setCurrentDirectory(newFile("."));

fileChooser

.setFileFilter(new

javax.swing.filechooser.FileFilter(){

publicbooleanaccept(Filefile){

Stringname=file.getName().toLowerCase();

returnname.endsWith(".mp3")

||file.isDirectory();

}

publicStringgetDescription(){

return"音乐文件(*.mp3)";

}

});

if(fileChooser.showOpenDialog(lyt.this)==

JFileChooser.APPROVE_OPTION){

StringfileName=

fileChooser.getSelectedFile()

.getAbsolutePath();

vector.add(fileName);

StringBufferbuffer=daoxu(fileName);

mingcheng.add(buffer);

}

}

});

tjian.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventevent){

JFileChooserfileChooser=newJFileChooser();

fileChooser

.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES

);

fileChooser.setCurrentDirectory(new

File("."));

fileChooser

.setFileFilter(new

javax.swing.filechooser.FileFilter(){

publicbooleanaccept(Filefile){

Stringname=

file.getName().toLowerCase();

returnname.endsWith(".mp3")

||file.isDirectory();

}

publicStringgetDescription(){

return"音乐文件(*.mp3)";

}

});

if(fileChooser.showOpenDialog(lyt.this)==

JFileChooser.APPROVE_OPTION){

StringfileName=

fileChooser.getSelectedFile()

.getAbsolutePath();

vector.add(fileName);

StringBufferbuffer=daoxu(fileName);

mingcheng.add(buffer);

}

}

});

jmiSave.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(jl.getSelectedIndex()>=0){

Stringyy=(String)

vector.get(jl.getSelectedIndex());

Fileff=newFile(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(InterruptedExceptioneee){

}

}

else

JOptionPane.showMessageDialog(null,

"无法播放");

}

else

JOptionPane.showMessageDialog(null,

"此歌曲已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件");

}

});

loopSound.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(jl.getSelectedIndex()>=0){

Stringyy=(String)vector.get(jl.getSelectedIndex());

Fileff=newFile(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(InterruptedExceptionwe){

}

}else

JOptionPane.showMessageDialog(null,

"不支持此格式,无法播放");

}else

JOptionPane.showMessageDialog(null,

"此歌曲文件已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件");

}

});

playSound.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(jl.getSelectedIndex()>=0){

Stringyy=(String)

vector.get(jl.getSelectedIndex());

Fileff=newFile(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(InterruptedExceptioneee){

}

}

else

JOptionPane.showMessageDialog(null,

"无法播放");

}

else

JOptionPane.showMessageDialog(null,

"此歌曲已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件");

}

});

stopsound.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

a=0;

b=0;

you=0;

mm=0;

geshi=false;

fo=false;

sj1.setText(null);

sj2.setText(null);

if(jl.getSelectedIndex()>=0)

soumd1.stop();

}

});

jl.addMouseListener(newMouseAdapter(){

publicvoidmousePressed(MouseEventevent){

if(event.getClickCount()==2){

if(jl.getSelectedIndex()>=0){

Stringyy=(String)

vector.get(jl.getSelectedIndex());

Fileff=newFile(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(InterruptedExceptione){

}

}

else

JOptionPane.showMessageDialog(null,

"无法播放");

}

else

JOptionPane.showMessageDialog(null,

"此歌曲已经存在");

}

}

if(event.isMetaDown()){

if(jl.getSelectedIndex()>=0){

inta=jl.getSelectedIndex();

jt1.setText(mingcheng.get(jl.getSelectedIndex())+"");

jt2.setText(vector.get(jl.getSelectedIndex())+"");

}

}

}

});

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

Strings="d:

\\music.txt";

mmm(vector,mingcheng,s);

System.exit(0);

}

});

setSize(370,270);

setVisible(true);

}

publicvoidrun(){

while(true){

Datenow=newDate();

try{

Thread.sleep(1000);

}

catch(InterruptedExceptione){

}

if(geshi){

you=(int)soumd1.getMediaTime().getSeconds();

mm=you%60;

if(you!

=0)

if(you%60==0){

b++;

}

sj1.setText(b+":

"+mm);

}

}

}

publicStringBufferdaoxu(StringfileName){

Stringmc="";

for(inti=fileName.length();i>=1;i--){

if(fileName.charAt(i-1)=='\\')

break;

mc+=fileName.charAt(i-1);

}

StringBufferbuffer=newStringBuffer(mc);

StringBuffermm=buffer.reverse();

returnmm;

}

publicvoidmmm(Vectorvector,Vectormingcheng,

Stringlujin){

lujina=newlujin(vector,mingcheng);

try{

ObjectOutputStreamoutput=new

ObjectOutputStream(newFileOutputStream(

lujin));

output.writeObject(a);

output.flush();

output.close();

}

catch(Exceptione){

}

}

publicstaticvoidmain(Stringagrs[]){

lyts=newlyt();

Threadt1=newThread(s);

t1.start();

s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

classlujin

implementsSerializable{

Vectora=newVector();

Vectorb=newVector();

publiclujin(Vectorvector,Vectormingcheng){

a=vector;

b=mingcheng;

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1