java 可以播放什么音乐播放器,求一个最简单的Java音乐播放器,能运行,播放的出来就行
大家好,java 可以播放什么音乐播放器相信很多的网友都不是很明白,包括求一个最简单的Java音乐播放器,能运行,播放的出来就行也是一样,不过没有关系,接下来就来为大家分享关于java 可以播放什么音乐播放器和求一个最简单的Java音乐播放器,能运行,播放的出来就行的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!
求一个最简单的Java音乐播放器,能运行,播放的出来就行
import javax.media.*;
import java.awt.*;
import java.awt.event.*;
class MediaPlayer extends Frame implements ActionListener,
ControllerListener, ItemListener
{
Player player;
Component vc, cc;
boolean first= true, loop= false;
String currentDirectory;
MediaPlayer(String title)
{
super(title);
addWindowListener
(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
//用户点击窗口系统菜单的关闭按钮
//调用dispose以执行windowClosed
dispose();
} public void windowClosed(WindowEvent e){
if(player!= null) player.close();
System.exit(0);
}
});
Menu m= new Menu("文件");
MenuItem mi= new MenuItem("打开");
mi.addActionListener(this);
m.add(mi);
m.addSeparator();
CheckboxMenuItem cbmi= new CheckboxMenuItem("循环", false);
cbmi.addItemListener(this);
m.add(cbmi);
m.addSeparator();
mi= new MenuItem("退出");
mi.addActionListener(this);
m.add(mi);
MenuBar mb= new MenuBar();
mb.add(m);
setMenuBar(mb);
setSize(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("退出"))
{
//调用dispose以便执行windowClosed
dispose();
return;
}
FileDialog fd= new FileDialog(this,"打开媒体文件",
FileDialog.LOAD);
fd.setDirectory(currentDirectory);
fd.show();
//如果用户放弃选择文件,则返回
if(fd.getFile()== null) return;
currentDirectory= fd.getDirectory();
if(player!= null)
player.close();
try
{
player= Manager.createPlayer(new MediaLocator("file:"+ fd.getDirectory()+ fd.getFile()));
}
catch(java.io.IOException e2)
{
System.out.println(e2);
return;
}
catch(NoPlayerException e2)
{
System.out.println("不能找到播放器.");
return;
}
if(player== null)
{
System.out.println("无法创建播放器.");
return;
}
first= false;
setTitle(fd.getFile());
player.addControllerListener(this);
player.prefetch();
}
public void controllerUpdate(ControllerEvent e)
{
//调用player.close()时ControllerClosedEvent事件出现。
//如果存在视觉部件,则该部件应该拆除(为一致起见,
//我们对控制面板部件也执行同样的操作)
if(e instanceof ControllerClosedEvent)
{
if(vc!= null)
{
remove(vc);
vc= null;
}
if(cc!= null)
{
remove(cc);
cc= null;
}
return;
}
if(e instanceof EndOfMediaEvent)
{
if(loop)
{
player.setMediaTime(new Time(0));
player.start();
}
return;
}
if(e instanceof PrefetchCompleteEvent)
{
player.start();
return;
}
if(e instanceof RealizeCompleteEvent)
{
vc= player.getVisualComponent();
if(vc!= null)
add(vc);
cc= player.getControlPanelComponent();
if(cc!= null)
add(cc, BorderLayout.SOUTH);
pack();
}
}
public void itemStateChanged(ItemEvent e)
{
loop=!loop;
}
public void paint(Graphics g)
{
if(first)
{
int w= getSize().width;
int h= getSize().height;
g.setColor(Color.blue);
g.fillRect(0, 0, w, h);
Font f= new Font("DialogInput", Font.BOLD, 16);
g.setFont(f);
FontMetrics fm= g.getFontMetrics();
int swidth= fm.stringWidth("***欢迎***");
g.setColor(Color.white);
g.drawString("***欢迎***",
(w- swidth)/ 2,
(h+ getInsets().top)/ 2);
}
//调用超类Frame的paint()方法,该paint()方法将调用Frame包含的各个容器
//和部件(包括控制面板部件)的paint()方法。
super.paint(g);
}
//不执行背景清除操作,以免控制面板部件闪烁
public void update(Graphics g)
{
paint(g);
}
public static void main(String [] args){
new MediaPlayer("媒体播放器1.0");
}}
java,我在写一个音乐播放器(只播放本地音乐),
请先确定您的音乐链接是否有效。操作方法:将您贴到音乐盒中的音乐链接,粘贴到IE浏览器的地址栏中打开,查看是否能够收听;2请确定您添加的音乐链接最后三个字母为mp3或wma,如果只是由您自己简单将网页名称修改后粘贴的链接,仍然是无效的。并注意最后面的地址不要多复制了一个空格。3在音乐收藏中,点击您音乐盒中不能收听音乐的编辑按钮,核对您所贴到音乐盒中的链接是否与您在网页上找到的一致。4如果您在音乐收藏中点击单首歌曲可以播放,但添加到播放列表中无法播放,请将此歌曲在播放列表中删除,再添加一次。为什么听不到自己空间里的音乐?请您核实浏览器是否安装了网页助手之类的插件。该插件若是设置了禁止播放网页音乐则是无法播放的,请将该屏蔽功能关闭即可。其次请安装 MediaPlayer播放软件后尝试!且请在“音乐盒”播放列表中注意“更新播放列表”。请您注意测试该歌曲的链接是否存在不稳定情况,建议您可以更换其他链接尝试。或者您也可以直接使用音乐库中的歌曲。其次请在“音乐盒”播放列表中注意“更新播放列表”。
如何用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;
publicclassMusicPlayerextendsFrame{
/**
*
*/
privatestaticfinallongserialVersionUID=-2605658046194599045L;
booleanisStop=true;//控制播放线程
booleanhasStop=true;//播放线程状态
Stringfilepath;//播放文件目录
Stringfilename;//播放文件名称
AudioInputStreamaudioInputStream;//文件流
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 可以播放什么音乐播放器,求一个最简单的Java音乐播放器,能运行,播放的出来就行的介绍到此结束,希望对大家有所帮助。