音乐播放器

音乐播放器主程序:

package musicPlayer;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.lang.reflect.Field;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;


import decorator.Source;


public class MusicPlayer extends Frame {

boolean isStop=true;
boolean hasStop=true;
String filepath;
String filename;
AudioInputStream audioInputStream;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
List list;
Label labelfilepath;
Label labelfilename;

public MusicPlayer(){
// 设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3音乐播放器");
setSize(350,370);
// 建立菜单栏
MenuBar menuBar=new MenuBar();
Menu menfile=new Menu("文件");
MenuItem menuopen=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O));
menfile.add(menuopen);
menfile.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
open();
}
});
menuBar.add(menfile);
setMenuBar(menuBar);
list=new List(10);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getClickCount()==2){
filename=list.getSelectedItem();
play();
}
}
});
add(list,"Center");
// 信息显示
Panel panel=new Panel(new GridLayout(2, 1));
labelfilepath=new Label("播放目录:");
labelfilename=new Label("播放文件:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel,"North");

// 注册窗体关闭事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
setVisible(true);


}

// 打开
private void open(){
FileDialog dialog=new FileDialog(this, "open", 0);
dialog.setVisible(true);
filepath=dialog.getDirectory();
if(filepath!=null){
labelfilepath.setText("播放目录:"+filepath);
// 显示文件列表
list.removeAll();
File filedir=new File(filepath);
File[] filelist=filedir.listFiles();
for(File file:filelist){
String filename=file.getName().toLowerCase();
if(filename.endsWith(".mp3")||filename.endsWith(".wav")){
list.add(filename);
}
}
}
}

// 播放
private void play(){
try {
isStop=true;
System.out.println("开始播放:"+filename);
while(!hasStop){
System.out.println(".");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


File file=new File(filepath+filename);
labelfilename.setText("播放文件:"+filename);
// 获取文件输入流
audioInputStream=AudioSystem.getAudioInputStream(file);
if(audioInputStream!=null){
audioFormat=audioInputStream.getFormat();
}
// 转换MP3文件编码
if(audioFormat.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED){
audioFormat=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(),16,
audioFormat.getChannels(),audioFormat.getChannels()*2,audioFormat.getSampleRate(),false);
audioInputStream=AudioSystem.getAudioInputStream(audioFormat, audioInputStream);

}
// 打开输出设备
DataLine.Info dateLineInfo=new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dateLineInfo);
if(sourceDataLine==null){
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}else{
sourceDataLine.stop();
sourceDataLine.drain();
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}


// 创建独立线程进行播放
isStop=false;
// PlayThread playThread=new  PlayThread(audioInputStream,sourceDataLine);
Thread playThread=new Thread(new PlayThread(audioInputStream,sourceDataLine));
// Thread playThread=new Thread(new PlayThread());
playThread.start();

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
new MusicPlayer();
}
}


播放线程

package musicPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.SourceDataLine;


//播放线程
class PlayThread extends Thread{
byte tempBuffer[]=new byte[320];
boolean isStop;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
public PlayThread(AudioInputStream audioInputStream, SourceDataLine sourceDataLine) {
super();
this.audioInputStream = audioInputStream;
this.sourceDataLine = sourceDataLine;
}
public void run(){
try {
int cnt=0;
boolean hasStop=false;
// 读取数据到缓存数据
while((cnt=audioInputStream.read(tempBuffer,0,tempBuffer.length))!=-1){
if(isStop) 
break;
if(cnt>0) 
sourceDataLine.write(tempBuffer,0,cnt);
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
hasStop=true;

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.exit(0);
}

}

}

出现问题:

在选择.mp3文件的时候,运行出现 问题:

开始播放:follow the light.mp3
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
at musicPlayer.MusicPlayer.play(MusicPlayer.java:136)
at musicPlayer.MusicPlayer.access$1(MusicPlayer.java:119)
at musicPlayer.MusicPlayer$2.mouseClicked(MusicPlayer.java:72)
at java.awt.Component.processMouseEvent(Component.java:6519)
at java.awt.Component.processEvent(Component.java:6281)

。。。。。。。

运行.wav文件时能够正常运行,在网上百度找不出.mp3不能运行的解决办法。

代码参考博主(http://blog.csdn.net/huahuaxingjing/article/details/48859033)

结果:


















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值