java播放音乐
发布时间: 2023-03-14 03:58:10
‘壹’ java,音频流,怎么播放音乐啊
importjava.applet.AudioClip;
importjava.net.MalformedURLException;
importjava.net.URL;
importjavax.swing.JApplet;
publicclassYuGiOh
{
(Stringfilename)
{
URLurl=null;
try
{
url=newURL("file:"+filename);
}
catch(MalformedURLExceptione)
{}
returnJApplet.newAudioClip(url);
}
publicstaticvoidmain(String[]args)
{
AudioClipchristmas=loadSound("d:/yinyue.wav");
christmas.play();
}
}
‘贰’ 如何用java做一个音乐播放器
首先下载播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加这个包。
播放器演示代码如下
packagecom.test.audio;
importjava.io.File;
importjava.awt.BorderLayout;
importjava.awt.FileDialog;
importjava.awt.Frame;
importjava.awt.GridLayout;
importjava.awt.Label;
importjava.awt.List;
importjava.awt.Menu;
importjava.awt.MenuBar;
importjava.awt.MenuItem;
importjava.awt.MenuShortcut;
importjava.awt.Panel;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.sound.sampled.AudioFormat;
importjavax.sound.sampled.AudioInputStream;
importjavax.sound.sampled.AudioSystem;
importjavax.sound.sampled.DataLine;
importjavax.sound.sampled.SourceDataLine;
{
/**
*
*/
=-2605658046194599045L;
booleanisStop=true;//控制播放线程
booleanhasStop=true;//播放线程状态
Stringfilepath;//播放文件目录
Stringfilename;//播放文件名称
;//文件流
AudioFormataudioFormat;//文件格式
SourceDataLinesourceDataLine;//输出设备
Listlist;//文件列表
Labellabelfilepath;//播放目录显示标签
Labellabelfilename;//播放文件显示标签
publicMusicPlayer(){
//设置窗体属性
setLayout(newBorderLayout());
setTitle("MP3MusicPlayer");
setSize(350,370);
//建立菜单栏
MenuBarmenubar=newMenuBar();
Menumenufile=newMenu("File");
MenuItemmenuopen=newMenuItem("Open",newMenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
//文件列表
list=newList(10);
list.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
//双击时处理
if(e.getClickCount()==2){
//播放选中的文件
filename=list.getSelectedItem();
play();
}
}
});
add(list,"Center");
//信息显示
Panelpanel=newPanel(newGridLayout(2,1));
labelfilepath=newLabel("Dir:");
labelfilename=newLabel("File:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel,"North");
//注册窗体关闭事件
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
setVisible(true);
}
//打开
privatevoidopen(){
FileDialogdialog=newFileDialog(this,"Open",0);
dialog.setVisible(true);
filepath=dialog.getDirectory();
if(filepath!=null){
labelfilepath.setText("Dir:"+filepath);
//显示文件列表
list.removeAll();
Filefiledir=newFile(filepath);
File[]filelist=filedir.listFiles();
for(Filefile:filelist){
Stringfilename=file.getName().toLowerCase();
if(filename.endsWith(".mp3")||filename.endsWith(".wav")){
list.add(filename);
}
}
}
}
//播放
privatevoidplay(){
try{
isStop=true;//停止播放线程
//等待播放线程停止
System.out.print("Start:"+filename);
while(!hasStop){
System.out.print(".");
try{
Thread.sleep(10);
}catch(Exceptione){
}
}
System.out.println("");
Filefile=newFile(filepath+filename);
labelfilename.setText("Playing:"+filename);
//取得文件输入流
audioInputStream=AudioSystem.getAudioInputStream(file);
audioFormat=audioInputStream.getFormat();
//转换mp3文件编码
if(audioFormat.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED){
audioFormat=newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(),16,audioFormat
.getChannels(),audioFormat.getChannels()*2,
audioFormat.getSampleRate(),false);
audioInputStream=AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}
//打开输出设备
DataLine.InfodataLineInfo=newDataLine.Info(
SourceDataLine.class,audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
//创建独立线程进行播放
isStop=false;
ThreadplayThread=newThread(newPlayThread());
playThread.start();
}catch(Exceptione){
e.printStackTrace();
}
}
classPlayThreadextendsThread{
bytetempBuffer[]=newbyte[320];
publicvoidrun(){
try{
intcnt;
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(Exceptione){
e.printStackTrace();
System.exit(0);
}
}
}
publicstaticvoidmain(Stringargs[]){
newMusicPlayer();
}
}
‘叁’ JAVA 实现音频播放
这个程序只要写对了音乐文件的URL地址,例如:new URL("file:/C:/tmp/1/Windows Ding.wav");
就可以播放音乐,除了可以播放.wav格式的音乐,还可以播放.au格式的音乐。
另外,如果你不希望音乐循环播放,你可以去掉audio1.loop();这一语句。
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
public class D extends JFrame{
D(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
URL codebase=null;
try {
codebase = new URL("file:/C:/tmp/1/Windows Ding.wav");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AudioClip audio1=Applet.newAudioClip(codebase);
audio1.loop();
}
public static void main(String[] args) {
new D();
}
}
热点内容