Java写的播放音乐文件

import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;


import javax.swing.*;






public class SoundPlayer extends JFrame implements ActionListener{


AudioClip adc;//声音音频剪辑对象

JLabel la=new JLabel();
JPanel pn1=new JPanel();
File file;//声明文件对象
String fileName;
JFileChooser chooser=new JFileChooser();//创建一个选择器对象
boolean loop=false;//判定是否循环操作

public SoundPlayer()
{
super("音乐播放器");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
if(adc!=null)
{
adc.stop();
}
}
});

JButton[] bn=new JButton[3];

JPanel pn=new JPanel(new GridLayout());//创建面板

Icon[] ic=new Icon[3];
for(int i=0;i<ic.length;i++)
{
ic[i]=new ImageIcon("D:\\Java_Tanzhou\\SoundPlayer\\image\\"+(i+1)+".gif");
}

bn[0]=new JButton("开始",ic[0]);
bn[0].addActionListener(this);//添加时间的侦听
bn[1]=new JButton("停止",ic[1]);
bn[1].addActionListener(this);//添加时间的侦听
bn[2]=new JButton("循环",ic[2]);
bn[2].addActionListener(this);//添加时间的侦听

pn.add(bn[0],0);//将开始按钮添加到面板pn上的第一个位置
pn.add(bn[1],1);
pn.add(bn[2],2);
this.add(pn,BorderLayout.SOUTH);

//创建菜单
JMenu fileMenu=new JMenu("文件");
JMenuItem openMenuItem=new JMenuItem("打开");
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
fileMenu.addSeparator();//分隔线

JMenuItem exitMenuItem=new JMenuItem("退出");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);

JMenuBar menuBar=new JMenuBar();
menuBar.add(fileMenu);

String flag="欢迎使用播放器!";

this.setFrame(flag);
this.setJMenuBar(menuBar);
this.setSize(300, 360);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
//退出按钮
if(e.getActionCommand().equals("退出"))
{
//退出按钮
dispose();
return;
}

//打开按钮
if(e.getActionCommand().equals("打开"))
{
//打开按钮
//对音乐文件的处理
//打开一个文件选择器
int val=chooser.showOpenDialog(this);//接受文件选择器的状态
if(val==chooser.APPROVE_OPTION)
{
file=chooser.getSelectedFile();//返回选中的文件
fileName=file.getName();
String flag="您正在欣赏:"+fileName;
this.setFrame(flag);

try {
//创建资源定位
URL url=new URL("file:"+file.getPath());
adc=JApplet.newAudioClip(url);//为音频剪辑对象adc赋值
adc.play();
} catch (MalformedURLException e1) {
e1.printStackTrace();
System.out.println("不能播放此文件!");
}
}
}

//开始按钮
if(e.getActionCommand().equals("开始"))
{
String flag="您正在欣赏:"+fileName;
if(adc==null)
{
flag="请选择播放文件!";
this.setFrame(flag);
return;
}
adc.play();
this.setFrame(flag);
}

//停止按钮
if(e.getActionCommand().equals("停止"))
{
adc.stop();
String flag="停止播放:"+fileName;
this.setFrame(flag);
}

//循环
if(e.getActionCommand().equals("循环"))
{
loop=!loop;//true
String flag="";
if(loop){
adc.play();//
adc.loop();//循环播放
flag="循环播放:"+fileName;
}else{
adc.play();//
flag="顺序播放:"+fileName;
}
this.setFrame(flag);
}

}

/*
* 抽离出来的公共方法
*/
public void setFrame(String flag)
{
la.setText(flag);
pn1.add(la,0);
this.add(pn1,FlowLayout.CENTER);
}

public static void main(String[] args)
{
new SoundPlayer();
}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值