重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java音乐播放代码 java 音频播放

如何用Java来编写一个音乐播放器

首先要在环境电脑中安装下JMF环境,才能引入javax.sound.sampled.*这个包,一下是用过的代码

成都创新互联公司自2013年起,是专业互联网技术服务公司,拥有项目网站建设、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元八步做网站,已为上家服务,为八步各地企业和个人服务,联系电话:18982081108

package TheMusic;

import java.io.*;

import javax.sound.sampled.*;

public class Music {

public static void main(String[] args) {

// TODO Auto-generated method stub

//修改你的音乐文件路径就OK了

AePlayWave apw=new AePlayWave("突然好想你.wav");

apw.start();

}

}

在程序中实例化这个类,启动线程,实例化的时候参照Test修改路径就OK播放声音的类

Java代码

public class AePlayWave extends Thread {

private String filename;

public AePlayWave(String wavfile) {

filename = wavfile;

}

public void run() {

File soundFile = new File(filename);

AudioInputStream audioInputStream = null;

try {

audioInputStream = AudioSystem.getAudioInputStream(soundFile);

} catch (Exception e1) {

e1.printStackTrace();

return;

}

AudioFormat format = audioInputStream.getFormat();

SourceDataLine auline = null;

DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try {

auline = (SourceDataLine) AudioSystem.getLine(info);

auline.open(format);

} catch (Exception e) {

e.printStackTrace();

return;

}

auline.start();

int nBytesRead = 0;

byte[] abData = new byte[512];

try {

while (nBytesRead != -1) {

nBytesRead = audioInputStream.read(abData, 0, abData.length);

if (nBytesRead = 0)

auline.write(abData, 0, nBytesRead);

}

} catch (IOException e) {

e.printStackTrace();

return;

} finally {

auline.drain();

auline.close();

}

}

}

java音乐播放

Applet我没有学过-------现在都用Swing了,AWT都很少用的。

我跑了下你的程序,你的错误在这:

sound=getAudioClip(getDocumentBase(),"test.mp3");

也就是AudioClip 对象没有创建成功!

在监听器中触发事件的时候,你首先要判断AudioClip 不为空,然后才能进行播放!

if (obj == plays) {

if (sound != null) {

sound.play();

} else {

sound = getAudioClip(getDocumentBase(), "D:\\lalala.wma");

sound.play();

}

} else if (obj == stops) {

。。。。。

这样写是对的写法

但是sound = getAudioClip(getDocumentBase(), "D:\\lalala.wma");

还是没有构建成功。所以,你构建AudioClip估计是错误的写法。

Appletde API太老了,我没有看,所以给你个Swing写的代码,你跑下看看,编译通过的。建议你不要用Applet了!

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import javax.media.Manager;

import javax.media.MediaLocator;

import javax.media.Player;

import javax.swing.*;

public class MusicDemo extends JFrame implements ActionListener {

Player player = null;

String tilte = "音乐播放器";

public MusicDemo(String title) {

super(title); // 设置显示标题(必须)

setResizable(false);// 设置是否可以拖放窗口大小

File mufile = new File("D:\\Lovinyou.mp3");

try {

if (player == null) {

if (mufile.exists()) {

MediaLocator locator = new MediaLocator("file:"

+ mufile.getAbsolutePath());

player = Manager.createRealizedPlayer(locator);

player.prefetch();

}

}

// player.addControllerListener(this);

player.start();// 开始播放

add(player.getControlPanelComponent(), "South");

double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();

double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();

setLocation((int) lx / 2 - 200, (int) ly / 2 - 150);// 设置显示位置(必须)

// 设置frame的大小(必须)

setSize(400, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭按钮退出程序(必须)

setVisible(true);// (必须)

} catch (Exception e) {

e.getStackTrace();

}

}

public String getTilte() {

return tilte;

}

public void setTilte(String tilte) {

this.tilte = tilte;

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

}

public static void main(String[] args) {

MusicDemo d = new MusicDemo("播放音乐");

}

}

记得在File mufile = new File("D:\\Lovinyou.mp3"); 这地方换为你音乐的路径

跪求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://cqcxhl.cn/article/hpphoe.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP