资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java播放音频的源代码 java实现音乐播放

java应用程序中播放mid音乐的代码,求助!!!!

import sun.audio.*;

专注于为中小企业提供成都做网站、成都网站制作、成都外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业惠城免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

import java.io.*;

import java.awt.*;

import java.awt.event.*;

class Sound5

{

FileInputStream file;

BufferedInputStream buf;

public Sound5()

{

try

{

file=new FileInputStream("1.mid");

buf=new BufferedInputStream(file);

AudioStream audio=new AudioStream(buf);

AudioPlayer.player.start(audio);

}

catch (Exception e) {}

}

}

public class e8165 extends Frame implements ActionListener

{

e8165()

{

super("音频播放器");

setBounds(300,300,200,100);

setVisible(true);

Button btn=new Button("播放");

setLayout(new FlowLayout());

add(btn);

btn.addActionListener(this);

validate();

// Sound5 play = new Sound5();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ System.exit(1); }

});

}

public void actionPerformed(ActionEvent e)

{

Sound5 play = new Sound5();

}

public static void main(String[] args)

{

new e8165();

}

}

//下载一个.mid文件命名为1,放在上述代码的包下

如何在java应用程序中播放音频文件

在 applet 中播放声音文件非常简单,一般需要以下步骤:创建一个 AudioClip 对象

装入 .au 声音文件到 AudioClip 对象

一次播放或者不停循环播放声音

停止播放

下面是相应的代码:import java.applet.*;AudioClip ac = getAudioClip(getCodeBase(), soundFile);

ac.play(); //play once

ac.stop(); //stop playing

解决这个问题的窍门是利用由 Sun 及 其JDK 提供的某些 undocumented 的特征。先看看 Sun JDK 中的文件 classes.zip (使用任何解压工具即可),发现其中不仅包含标准的 Java 包如 java.applet 而且还存在包 sun.audio. (在 sun/audio 的目录下.)

包 sun.audio 中包含了用于播放声音文件所需的所有东西!下面是示例代码:import sun.audio.*; //import the sun.audio package

import java.io.*;//** add this into your application code as appropriate// Open an input stream to the audio file.

InputStream in = new FileInputStream(Filename);// Create an AudioStream object from the input stream.

AudioStream as = new AudioStream(in);// Use the static class member "player" from class AudioPlayer to play

// clip.

AudioPlayer.player.start(as);// Similarly, to stop the audio.

AudioPlayer.player.stop(as);如果要用一个 URL 做为声音流的源(source),则用下面的代码所示替换输入流来创建声音流:AudioStream as = new AudioStream (url.openStream());如果需要持续播放声音文件,则要稍稍复杂一点:// Create audio stream as discussed previously.

// Create AudioData source.

AudioData data = as.getData();// Create ContinuousAudioDataStream.

ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);// Play audio.

跪求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音乐播放器的源代码

import javax.media.ControllerEvent;

import javax.media.ControllerListener;

import javax.media.EndOfMediaEvent;

import javax.media.PrefetchCompleteEvent;

import javax.media.RealizeCompleteEvent;

import javax.media.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MediaPlayer extends JFrame implements ActionListener,

ItemListener, ControllerListener {

String title;

Player player;

boolean first = true, loop = false;

Component vc, cc;

String currentDirectory=null;

// 构造函数,其中包括了设置响应窗口事件的监听器。

MediaPlayer(String title) {

super(title);

/* 关闭按钮的实现。。 */

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

}

public void windowClosed(WindowEvent e) {

if (player != null)

player.close();

System.exit(0);

}

});

// 调用程序菜单栏的方法成员完成菜单的布置

setupMenu();

setSize(400, 400);

setVisible(true);

}

// 本方法用以设置程序菜单栏

public void setupMenu() {

// 设置一个菜单

Menu f = new Menu("文件");

// 往设置的菜单添加菜单项

MenuItem mi = new MenuItem("打开");

f.add(mi);

mi.addActionListener(this);

f.addSeparator();

CheckboxMenuItem cbmi = new CheckboxMenuItem("循环", false);

cbmi.addActionListener(this);

f.add(cbmi);

f.addSeparator();

MenuItem ee = new MenuItem("退出");

ee.addActionListener(this);

f.add(ee);

f.addSeparator();

Menu l = new Menu("播放列表");

Menu c = new Menu("播放控制");

MenuItem move = new MenuItem("播放");

move.addActionListener(this);

c.add(move);

c.addSeparator();

MenuItem pause = new MenuItem("暂停");

pause.addActionListener(this);

c.add(pause);

c.addSeparator();

MenuItem stop = new MenuItem("停止");

stop.addActionListener(this);

c.add(stop);

c.addSeparator();

// 设置一个菜单栏

MenuBar mb = new MenuBar();

mb.add(f);

mb.add?;

mb.add(l);

// 将构造完成的菜单栏交给当前程序的窗口;

setMenuBar(mb);

}

// 动作时间响应成员;捕捉发送到本对象的各种事件;

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String cufile, selectfile, currentDirectory;

if (e.getActionCommand().equals("退出")) {

// 调用dispose以便执行windowClosed

dispose();

return;

}

// 此事表明拥护选择了“播放”命令;

// 如果当前有一个文件可以播放则执行播放命令;

if (e.getActionCommand().equals("播放")) {

if (player != null) {

player.start();

}

return;

}

// 如果当前正在播放某一文件,则执行暂停;

if (e.getActionCommand().equals("暂停")) {

if (player != null) {

player.stop();

}

return;

}

// 停止命令的响应;

if (e.getActionCommand().equals("停止")) {

if (player != null) {

player.stop();

player.setMediaTime(new Time(0));

}

return;

}

// 用户选择要播放的媒体文件

if (e.getActionCommand().equals("打开")) {

FileDialog fd = new FileDialog(this, "打开媒体文件", FileDialog.LOAD);

// fd.setDirectory(currentDirectory);

2008-2-6 02:46 回复

肆方茉莉

62位粉丝

6楼

fd.setVisible(true);

// 如果用户放弃选择文件,则返回

if (fd.getFile() == null) {

return;

}

// 保存了所选文件的名称及其路径名称已被稍后使用

// 同时设置当前文件夹路径

selectfile = fd.getFile();

currentDirectory = fd.getDirectory();

cufile = currentDirectory + selectfile;

// 将用户选择的文件作为一个菜单项加入播放列表,该菜单项名为该文件名;

// 被点击后给出的命令串是该文件的全路径名

MenuItem mi = new MenuItem(selectfile);

mi.setActionCommand(cufile);

MenuBar mb = getMenuBar();

Menu m = mb.getMenu(2);

mi.addActionListener(this);

m.add(mi);

} else {

// 程序逻辑运行到次表示用户选择了一个“播放列表”中的媒体文件

// 此时可以通过如下动作获得该文件的全路径名

cufile = e.getActionCommand();

selectfile = cufile;

}

// 如果存在一个播放器,则先将其关闭,稍后再重新创建

// 创建播放器时需要捕捉一些异常

if (player != null) {

player.close();

}

try {

player = Manager.createPlayer(new MediaLocator("file:" + cufile));

} catch (Exception e2) {

System.out.println(e2);

return;

}/*

* catch(NoPlayerException e2){ System.out.println("不能找到播放器");

* return ; }

*/

if (player == null) {

System.out.println("无法创建播放器");

return;

}

first = false;

setTitle(selectfile);

// 设置处理播放控制器实际的对象;

/**/

player.addControllerListener(this);

player.prefetch();

}

// 菜单状态改变事件的响应函数;

public void itemStateChanged(ItemEvent arg0) {

// TODO Auto-generated method stub

}

public static void main(String[] args) {

// TODO Auto-generated method stub

new MediaPlayer("播放器");

}

// 调用绘图函数进行界面的绘制 // public void update() {

// }

// 绘图函数成员 //public void paint(Graphics g) {

// }

public void controllerUpdate(ControllerEvent e) {

// TODO Auto-generated method stub

Container tainer = getContentPane();

// 调用player.close()时ControllerClosedEvent事件出现

// 如果存在视觉部件,则该部件应该拆除(为了一致起见,我们对控制面版部件也执行同样的操作,下一次需要时再构造)

if (e instanceof ControllerClosedEvent) {

if (vc != null) {

remove(vc);

vc = null;

}

if (cc != null) {

remove(cc);

cc = null;

}

}

// 播放结束时,将播放指针置于文件之首,如果设定了循环播放,则再次启动播放器;

if (e instanceof EndOfMediaEvent) {

player.setMediaTime(new Time(0));

if (loop) {

player.start();

}

return;

}

// PrefetchCompletEvent事件发生后调用start,正式启动播放

if (e instanceof PrefetchCompleteEvent) {

player.start();

return;

}

// 本事件表示由于播放的资源已经确定;此时要将媒体的图形conmopnent

// 如果有显示出来,同时将播放器player的控制显示到窗口里;

if (e instanceof RealizeCompleteEvent) {

// 如果媒体中有图像,将对应图像component载入窗体;

vc = player.getVisualComponent();

if (vc != null)

tainer.add(vc, BorderLayout.CENTER);

// 将对应控制器component载入窗体;

cc = player.getControlPanelComponent();

cc.setBackground(Color.blue);

if (cc != null)

tainer.add(cc, BorderLayout.SOUTH);

// 有一些特殊媒体在播放时提供另外的控制手段,将控制器一并加入窗口;

/*

* gc=player.getGainControl(); gcc=gc.getControlComponent();

* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH);

*/

// 根据媒体文件中是否有图像,设定相应的窗口大小

if (vc != null) {

pack();

return;

} else {

setSize(300, 75);

setVisible(true);

return;

}

}

} }

用Java代码能播放音频文件吗?

可以实现,参考如下代码:

package com.lolo;

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.DataLine;

public class PlayMusic {

//    程序退出时执行的代码

public void doShutDownWork() {

Runtime.getRuntime().addShutdownHook(new Thread() {

public void run() {

try {

//Toolkit.getDefaultToolkit().beep();

Play();

Play();

} catch (Exception ex) {

}

}

});

}

//播放音频文件

public void Play(){

String fileurl = "file/tada.wav";

try{

AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileurl));

AudioFormat aif = ais.getFormat();

SourceDataLine sdl = null;

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

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

sdl.open(aif);

sdl.start();

//play

int nByte = 0;

byte[] buffer = new byte[128];

while(nByte != -1){

nByte = ais.read(buffer,0,128);

if(nByte = 0){

int oByte = sdl.write(buffer, 0, nByte);

//System.out.println(oByte);

}

}

sdl.stop();

}catch(UnsupportedAudioFileException e){

e.printStackTrace();

} catch (IOException e) {

// TODO 自动产生 catch 区块

e.printStackTrace();

} catch (LineUnavailableException e) {

// TODO 自动产生 catch 区块

e.printStackTrace();

}

}

}


本文名称:java播放音频的源代码 java实现音乐播放
链接分享:http://cdkjz.cn/article/dodidip.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220