java开发音乐的代码 基于java的音乐播放器的设计开发

如何用Java来编写一个音乐播放器首先要在环境电脑中安装下JMF环境java开发音乐的代码 , 才能引入javax.sound.sampled.*这个包java开发音乐的代码,一下是用过的代码
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 = https://www.04ip.com/post/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大神求解啊 , 如何用java编写一个音乐播放器,然后播放器里面的歌可以顺序播放的 。参考代码如下
首先下载播放mp3的包,比如mp3spi1.9.4.jar 。在工程中添加这个包 。
播放器演示代码如下
package com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame {
/**
*
*/
private static final long serialVersionUID = -2605658046194599045L;
boolean isStop = true;// 控制播放线程
boolean hasStop = true;// 播放线程状态
String filepath;// 播放文件目录
String filename;// 播放文件名称
AudioInputStream audioInputStream;// 文件流
AudioFormat audioFormat;// 文件格式
SourceDataLine sourceDataLine;// 输出设备
List list;// 文件列表
Label labelfilepath;//播放目录显示标签
Label labelfilename;//播放文件显示标签
public MusicPlayer() {
// 设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3 Music Player");
setSize(350, 370);
// 建立菜单栏
MenuBar menubar = new MenuBar();
Menu menufile = new Menu("File");
MenuItem menuopen = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
// 文件列表
list = new List(10);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 双击时处理
if (e.getClickCount() == 2) {
// 播放选中的文件
filename = list.getSelectedItem();
play();
}
}
});
add(list, "Center");
// 信息显示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label("Dir:");
labelfilename = new Label("File:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, "North");
// 注册窗体关闭事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
// 打开
private void open() {
FileDialog dialog = new FileDialog(this, "Open", 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText("Dir:"filepath);
// 显示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {
list.add(filename);
}
}
}
}
// 播放
private void play() {
try {
isStop = true;// 停止播放线程
// 等待播放线程停止
System.out.print("Start:"filename);
while (!hasStop) {
System.out.print(".");
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.out.println("");
File file = new File(filepathfilename);
labelfilename.setText("Playing:"filename);
// 取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
audioFormat = audioInputStream.getFormat();
// 转换mp3文件编码
if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(), 16, audioFormat
.getChannels(), audioFormat.getChannels() * 2,
audioFormat.getSampleRate(), false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}
// 打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 创建独立线程进行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
public void run() {
try {
int cnt;
hasStop = false;
// 读取数据到缓存数据
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (isStop)
break;
if (cnt0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
怎么在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中添加背景音乐的代码不知道你是在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 音乐播放的代码?。?完美运行的就行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("劲爆java开发音乐的代码的士高");
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();//获得系统托盘java开发音乐的代码的实例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));
systemTray.add(trayIcon);//设置托盘的图标java开发音乐的代码,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();
}
}
/*
可以用加载声音文件的方法java开发音乐的代码:
第一?。簃ysound= new Sound();
mysound.attachSound(声音id名字);
ptime = 0;
播放按钮as:
on(release){
mysound.start(ptime);
}
暂停按钮as:
on(release){
ptime = mysound.position/1000;
mysound.stop();
}
*/
Java怎么实现音乐播放java swt实现播放音乐代码如下java开发音乐的代码:
public void play(String Filename)
{
try{
// 用输入流打开一音频文件
InputStream in = new FileInputStream(Filename);//FIlename 是java开发音乐的代码你加载java开发音乐的代码的声音文件如(“game.wav”)
// 从输入流中创建一个AudioStream对象
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);//用静态成员player.start播放音乐
//AudioPlayer.player.stop(as);//关闭音乐播放
//如果要实现循环播放java开发音乐的代码,则用下面的三句取代上面的“AudioPlayer.player.start(as);”这句
/*AudioData data = https://www.04ip.com/post/as.getData();
ContinuousAudioDataStream gg= new ContinuousAudioDataStream (data);
AudioPlayer.player.start(gg);// Play audio.
*/
//如果要用一个 URL 做为声音流的源(source),则用下面的代码所示替换输入流来创建声音流java开发音乐的代码:
/*AudioStream as = new AudioStream (url.openStream());
*/
} catch(FileNotFoundException e){
System.out.print("FileNotFoundException ");
} catch(IOException e){
System.out.print("有错误!");
【java开发音乐的代码 基于java的音乐播放器的设计开发】}
}
关于java开发音乐的代码和基于java的音乐播放器的设计开发的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读