java调用音乐代码 java播放音乐代码

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程序中加音乐?

可以通过Service来播放背景音乐,以下是实现代码:

1.在AndroidManifest.xml文件中的application标签内加入下边语句

service android:name=".MusicServer"

intent-filter

action android:name="com.angel.Android.MUSIC"/

category android:name="android.intent.category.default" /

/intent-filter

/service

2.新建MusicServer.java类,内容为

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

public class MusicServer extends Service {

private MediaPlayer mediaPlayer;

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onStart(Intent intent,int startId){

super.onStart(intent, startId);

if(mediaPlayer==null){

// R.raw.mmp是资源文件,MP3格式的

mediaPlayer = MediaPlayer.create(this, R.raw.abc);

mediaPlayer.setLooping(true);

mediaPlayer.start();

}

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

mediaPlayer.stop();

}

}

3.将歌曲放入raw文件夹下,名称为abc。

4.在Activity中加入代码

private Intent intent = new Intent("com.angel.Android.MUSIC");

onCreate方法中加入startService(intent);

就可以播放了。

跪求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里哪添加?Swing界面中吗?

下面这个是我之前做Swing界面程序时添加音乐的代码,希望对你有帮助

AudioClip[] musics;//定义音乐集合

musics = new AudioClip[2];//初始化

URL url1 = this.getClass().getResource("/ReadyGo.WAV"); //定义音乐文件地址

URL url2 = this.getClass().getResource("/back1.mid"); //定义音乐文件地址

musics[0] = JApplet.newAudioClip(url1);

musics[1] = JApplet.newAudioClip(url2);

musics[0].play();//音乐开始执行

musics[1].stop();//停止播放

怎样通过java打开音乐播放器

java中打开音乐播放器的方式是使用audioclip类来播放音乐,实例如下:

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import javax.swing.*;

import java.io.File;

class AudioPlayDemo extends JFrame implements ActionListener {

boolean looping = false; 

File file1 = new File("music\\明天会更好.wav");

AudioClip sound1;

AudioClip chosenClip;

JButton playButton = new JButton("播放"); 

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

JButton stopButton = new JButton("停止"); 

JLabel status = new JLabel("选择播放文件"); 

JPanel controlPanel = new JPanel(); 

Container container = getContentPane(); 

public AudioPlayDemo() { 

try {

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

chosenClip = sound1;

} catch(OutOfMemoryError e){

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

e.printStackTrace();

} catch(Exception e){

e.printStackTrace();

}

playButton.addActionListener(this);

loopButton.addActionListener(this);

stopButton.addActionListener(this);

stopButton.setEnabled(false); 

controlPanel.add(playButton);

controlPanel.add(loopButton);

controlPanel.add(stopButton);

container.add(controlPanel, BorderLayout.CENTER);

container.add(status, BorderLayout.SOUTH);

setSize(300, 130); 

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序

}

public void actionPerformed(ActionEvent event) {

if (chosenClip == null) {

status.setText("声音未载入");

return; 

}

Object source = event.getSource(); //获取用户洗涤激活的按钮

if (source == playButton) {

stopButton.setEnabled(true); 

loopButton.setEnabled(true); 

chosenClip.play(); 

status.setText("正在播放");

}

if (source == loopButton) {

looping = true;

chosenClip.loop(); 

loopButton.setEnabled(false); 

stopButton.setEnabled(true); 

status.setText("正在循环播放"); 

}

if (source == stopButton) {

if (looping) {

looping = false;

chosenClip.stop(); 

loopButton.setEnabled(true);

} else {

chosenClip.stop();

}

stopButton.setEnabled(false); 

status.setText("停止播放");

}

}

public static void main(String s[]) {

new AudioPlayDemo(); 

}

}

只能播放wav格式的歌曲

分享名称:java调用音乐代码 java播放音乐代码
网页地址:https://www.cdcxhl.com/article46/hghjhg.html

成都网站建设公司_创新互联,为您提供软件开发小程序开发手机网站建设营销型网站建设App开发网站改版

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化