快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

java如何实现播放mp3

简单的实例,代码如下,纯粹JMF加载MP3并播放:

10余年的宝丰网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整宝丰建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“宝丰网站设计”,“宝丰网站推广”以来,每个客户项目都认真落实执行。

import javax.media.*;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class PlayerMusic implements ControllerListener {// ControllerListener

// 控制事件

private Player player;

private boolean first, loop;

private String path;

private List mp3List;

private int mp3NO = 0;

PlayerMusic(List mp3List) {

this.mp3List = mp3List;

}

public void start() {

try {

player = Manager.createPlayer(new MediaLocator("file://" + mp3List.get(mp3NO)));

} catch (NoPlayerException ex) {

ex.printStackTrace();

System.out.println("不能播放文件");

return;

} catch (IOException ex) {

ex.printStackTrace();

return;

}

if (player == null) {

System.out.println("播放器为空");

return;

}

first = false;

player.addControllerListener(this);

// 提取媒体内容

player.prefetch();

}

public void controllerUpdate(ControllerEvent e) {

// 当媒体播放结束时,循环播放

if (e instanceof EndOfMediaEvent) {

mp3NO++;

if(mp3NOthis.mp3List.size()){

this.start();

}

return;

}

// 当预提取媒体的内容结束

if (e instanceof PrefetchCompleteEvent) {

player.start();

return;

}

// 当实例化后

if (e instanceof RealizeCompleteEvent) {

// pack(); //执行pack()操作

return;

}

}

public static void main(String[] args) {

List mp3List = new ArrayList();

mp3List.add("d://a.mp3");

mp3List.add("d://b.mp3");

mp3List.add("d://c.mp3");

PlayerMusic pm = new PlayerMusic(mp3List);

pm.start();

}

}

怎样在java中播放音乐

使用play()方法进行播放,loop()方法循环播放,stop()方法停止播放。

实际例子:

File file1 = new File("src\\music\\11.wav");

AudioClip sound1;

sound1 = Applet.newAudioClip(file1.toURL());

sound1.play();

这样就实现了播放音乐的功能,注意J2SE默认只支持 wav格式的音频。

跪求java 音乐播放的代码啊,完美运行的就行

import java.applet.Applet;

import java.applet.AudioClip;

import java.awt.AWTException;

import java.awt.Frame;

import java.awt.SystemTray;

import java.awt.TrayIcon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

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.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.*;

public class bofan_2 extends JFrame implements ActionListener

{

boolean looping=false;

File file1=null;

AudioClip sound1;

AudioClip chosenClip;

private JComboBox box1=null; //歌曲列表

private JButton butbofan=null; //播放

private JButton butboxhuan=null; //循环播放

private JButton buttinzi=null; //停止

private JButton butshan=null; //上一首

private JButton butzhantin=null; //暂停

private JButton butxia=null; //下一首

private TrayIcon trayIcon;//托盘图标

private SystemTray systemTray;//系统托盘

public bofan_2()

{

this.setSize(420,400);

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setLayout(null);

box1=new JComboBox();

box1.addItem("伤心太平洋");

box1.addItem("劲爆的士高");

box1.addItem("老夫少妻");

box1.addItem("爱不再来");

box1.addItem("抽身");

box1.addItem("伤心城市");

box1.addItem("二零一二");

box1.addItem("精忠报国");

box1.addItem("秋沙");

box1.addItem("吻别");

box1.addItem("音乐疯起来");

box1.setBounds(10,20,150,20);

butbofan=new JButton("播放");

butbofan.addActionListener(this);

butbofan.setBounds(165,50,60,20);

butboxhuan=new JButton("循环播放");

butboxhuan.addActionListener(this);

butboxhuan.setBounds(230,50,90,20);

buttinzi=new JButton("停止");

buttinzi.setEnabled(false);

buttinzi.addActionListener(this);

buttinzi.setBounds(335,50,60,20);

butshan=new JButton("上一首");

butshan.addActionListener(this);

butshan.setBounds(165,90,80,20);

butzhantin=new JButton("暂停");

butzhantin.setEnabled(false);

butzhantin.addActionListener(this);

butzhantin.setBounds(250,90,60,20);

butxia=new JButton("下一首");

butxia.addActionListener(this);

butxia.setBounds(320,90,80,20);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.getContentPane().add(box1);

this.getContentPane().add(butbofan);

this.getContentPane().add(butboxhuan);

this.getContentPane().add(buttinzi);

this.getContentPane().add(butshan);

this.getContentPane().add(butzhantin);

this.getContentPane().add(butxia);

try {

UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel");

} catch (ClassNotFoundException e)

{

e.printStackTrace();

} catch (InstantiationException e)

{

e.printStackTrace();

} catch (IllegalAccessException e)

{

e.printStackTrace();

} catch (UnsupportedLookAndFeelException e)

{

e.printStackTrace();

}

setSize(450,450);

systemTray = SystemTray.getSystemTray();//获得系统托盘的实例

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try {

trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));

systemTray.add(trayIcon);//设置托盘的图标,0.gif与该类文件同一目录

}

catch (IOException e1)

{

e1.printStackTrace();

}

catch (AWTException e2)

{

e2.printStackTrace();

}

this.addWindowListener(

new WindowAdapter(){

public void windowIconified(WindowEvent e)

{

dispose();//窗口最小化时dispose该窗口

}

});

trayIcon.addMouseListener(new MouseAdapter()

{

public void mouseClicked(MouseEvent e){

if(e.getClickCount() == 2)//双击托盘窗口再现

setExtendedState(Frame.NORMAL);

setVisible(true);

}

});

this.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();

if (source== butbofan)

{

System.out.println((String) box1.getSelectedItem());

file1=new File((String) box1.getSelectedItem()+".wav");

butboxhuan.setEnabled(true);

buttinzi.setEnabled(true);

butzhantin.setEnabled(true);

butzhantin.setText("暂停");

try {

sound1 = Applet.newAudioClip(file1.toURL());

chosenClip = sound1;

} catch(OutOfMemoryError er){

System.out.println("内存溢出");

er.printStackTrace();

} catch(Exception ex){

ex.printStackTrace();

}

chosenClip.play();

this.setTitle("正在播放"+(String) box1.getSelectedItem());

}

if (source== butboxhuan)

{

file1=new File((String) box1.getSelectedItem()+".wav");

try {

sound1 = Applet.newAudioClip(file1.toURL());

chosenClip = sound1;

} catch(OutOfMemoryError er){

System.out.println("内存溢出");

er.printStackTrace();

} catch(Exception ex){

ex.printStackTrace();

}

looping = true;

chosenClip.loop();

butboxhuan.setEnabled(false);

buttinzi.setEnabled(true);

butzhantin.setText("暂停");

this.setTitle("正在循环播放"+(String) box1.getSelectedItem());

}

if (source== buttinzi)

{

if (looping)

{

looping = false;

chosenClip.stop();

butboxhuan.setEnabled(true);

butzhantin.setText("暂停");

} else {

chosenClip.stop();

}

buttinzi.setEnabled(false);

this.setTitle("停止播放");

}

if(source==butshan)

{

butzhantin.setText("暂停");

}

if(source==butzhantin)

{

buttinzi.setEnabled(false);

butzhantin.setText("继续");

if(source==butzhantin)

{

butzhantin.setText("暂停");

}

}

if(source==butxia)

{

butzhantin.setText("暂停");

}

}

public static void main(String[] args)

{

bofan_2 xx=new bofan_2();

}

}

/*

可以用加载声音文件的方法:

第一帧:mysound= new Sound();

mysound.attachSound(声音id名字);

ptime = 0;

播放按钮as:

on(release){

mysound.start(ptime);

}

暂停按钮as:

on(release){

ptime = mysound.position/1000;

mysound.stop();

}

*/


当前名称:播放音乐代码java java写一个简单的音乐播放器
转载源于:http://6mz.cn/article/doigjic.html

其他资讯