java 如何连接音乐 java 如何连接音乐我要做个闹钟程序,想加一个连接播放音乐的功能,怎么做啊?用什么类,什么方法啊?高手指点。谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package org.bruce.play_audio;import java.io.File;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.Player;public class PlayMP3 { //这个是用 JMF 播放的! public static void main(String[] args) { File f1 = new File("src/org/bruce/play_audio/fire1.mp3"); Player player = null; try { MediaLocator locator = new MediaLocator(f1.toURI().toURL()); player = Manager.createRealizedPlayer(locator);// player.prefetch(); // 这句代码是干嘛的? player.start(); Thread.sleep(400); } catch (Exception ex) { ex.printStackTrace(); } finally { player.close(); } } }需要jdk1.6,里面自带了JavaMediaFramework package org.bruce.play_audio;import java.io.File;import javax.sound.midi.MidiSystem;import javax.sound.midi.Sequence;import javax.sound.midi.Sequencer;public class PlayMidSimplified { Sequencer midi; Sequence temp; public PlayMidSimplified() { try { // 获得连接到默认设备上的默认的 Sequencer。 midi = MidiSystem.getSequencer(false); midi.open(); temp = MidiSystem.getSequence( new File("src/org/bruce/play_audio/tankwar_bgsound.mid")); midi.setSequence(temp); midi.start(); // 睡多久是根据midi音乐的长度来定的,不睡的话程序一下就结束了! Thread.sleep(6000); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new PlayMidSimplified(); }} package org.bruce.play_audio;import java.awt.Component;import java.awt.GridLayout;import java.io.File;import java.io.IOException;import javax.media.ControllerEvent;import javax.media.ControllerListener;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.NoPlayerException;import javax.media.Player;import javax.media.RealizeCompleteEvent;import javax.swing.JFrame;import javax.swing.JPanel;//南北区宽度由容器宽度决定,高度由被添加的组件决定//东西区高度由容器高度决定,宽度由被添加的组件决定//中间区,高度宽度都决定于剩余值,被添加组件的高度和宽度被忽略public class PlayLocalMP3 extends JFrame { private static final long serialVersionUID = 7723418221792502794L; @SuppressWarnings("unused") private static String path1 = "F:/KuGou/Cinderella.mp3"; @SuppressWarnings("unused") private static String path2 = "F:/KuGou/hongchenyouni.wma"; @SuppressWarnings("unused") private static String path3 = "F:/KuGou/zhuyu.wmv"; @SuppressWarnings("unused") private static String path4 = "F:/KuGou/My anata.swf"; private static String path5 = "F:/KuGou/BY2-爱丫爱丫.mp3"; private JPanel jContentPane; // 构造方法 public PlayLocalMP3() { this.setSize(300, 300); this.setLocation(300, 300); //添加一个两行两列的格子布局 jContentPane = new JPanel(new GridLayout(2, 2)); this.add(jContentPane); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //妈的难道只支持.mp3格式的? //路径还不能带英文!? -- 经测试可以带中文啊!! //这个是用 JMF 播放的! public static void main(String[] args) { File f1 = new File(path5); Player player = null; try { MediaLocator locator = new MediaLocator(f1.toURI().toURL()); player = Manager.createRealizedPlayer(locator); player.prefetch(); player.start(); } catch (Exception ex) { ex.printStackTrace(); } finally {// player.close(); } } // 这个是间接调用系统的播放器播放的! public static void main2(String[] args) { PlayLocalMP3 pmp3 = new PlayLocalMP3(); File mp3File = new File("F:/KuGou/Cinderella.mp3"); pmp3.playTheMP3(mp3File); } //通过调用系统默认的MP3播放器播放MP3(比如 酷狗、 迅雷看看播放器 什么的!) private void playTheMP3(File file) { final Player dualPlayer; try { if(file.getAbsolutePath().endsWith("mp3")){ // mp3 file will invoke system default player to play System.out.println("system player is invoked."); // 用cmd播放执行文件的命令(支持各种格式,比如MP3、exe、jar...): // "cmd.exe /c 可识别格式文件在磁盘上的绝对路径" Runtime.getRuntime().exec("cmd.exe /c" + file.getAbsolutePath()); return; } // 这里开始是JMF处理WAV文件,之前的MP3放开运行的话,不支持44.1Kz String locateString = "file:///" + file.getAbsolutePath(); // + fileName; dualPlayer = Manager.createPlayer(new MediaLocator(locateString)); dualPlayer.addControllerListener(new ControllerListener(){ @Override public synchronized void controllerUpdate(ControllerEvent event) { if(event instanceof RealizeCompleteEvent){ Component comp; if((comp = dualPlayer.getVisualComponent()) != null){ System.out.println("comp 不为 null!"); comp.setVisible(true); PlayLocalMP3.this.setVisible(true); jContentPane.add(comp); } else { System.out.println("comp为: " + comp); } // 验证此容器及其所有子组件。 // 使用 validate 方法会使容器再次布置其子组件。 // 已经布置容器后,在修改此容器的子组件的时候(在容器中添加或移除组件, // 或者更改与布局相关的信息),应该调用上述方法。 jContentPane.validate(); } } }); dualPlayer.start(); } catch (NoPlayerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }从中挑选一个吧,记得要结贴给分啊! 关于动态代理Proxy.newProxyInstance方法参数的理解问题 IO流的一编程题 求助--在线等 b 和 c 多个编辑框共享一个右键菜单,如何判断是在哪个编辑框弹出的菜单? lucene删除索引时出现两个cfs文件,数据也没有删除 使用数组产生的java.lang.NullPointerException错误,不知道如何解决 大家帮我看看这段代码为什么错误呢? File类中的mkdirs与mkdir有什么不同???? 请教取日期 求得到当前系统时间并转成YYYYMMDDHHMI格式的简单方法 请教:java程序中super()的使用问题 一个字节可以强制转换为字符,并打印出来?
import javax.media.MediaLocator;
import javax.media.Player;public class PlayMP3 {
//这个是用 JMF 播放的!
public static void main(String[] args) {
File f1 = new File("src/org/bruce/play_audio/fire1.mp3");
Player player = null;
try {
MediaLocator locator = new MediaLocator(f1.toURI().toURL());
player = Manager.createRealizedPlayer(locator);
// player.prefetch(); // 这句代码是干嘛的?
player.start();
Thread.sleep(400);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
player.close();
}
}
}需要jdk1.6,里面自带了JavaMediaFramework
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;public class PlayMidSimplified {
Sequencer midi;
Sequence temp; public PlayMidSimplified() {
try {
// 获得连接到默认设备上的默认的 Sequencer。
midi = MidiSystem.getSequencer(false);
midi.open();
temp = MidiSystem.getSequence(
new File("src/org/bruce/play_audio/tankwar_bgsound.mid"));
midi.setSequence(temp);
midi.start();
// 睡多久是根据midi音乐的长度来定的,不睡的话程序一下就结束了!
Thread.sleep(6000);
} catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
new PlayMidSimplified();
}
}
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;//南北区宽度由容器宽度决定,高度由被添加的组件决定
//东西区高度由容器高度决定,宽度由被添加的组件决定
//中间区,高度宽度都决定于剩余值,被添加组件的高度和宽度被忽略
public class PlayLocalMP3 extends JFrame {
private static final long serialVersionUID = 7723418221792502794L;
@SuppressWarnings("unused")
private static String path1 = "F:/KuGou/Cinderella.mp3";
@SuppressWarnings("unused")
private static String path2 = "F:/KuGou/hongchenyouni.wma";
@SuppressWarnings("unused")
private static String path3 = "F:/KuGou/zhuyu.wmv";
@SuppressWarnings("unused")
private static String path4 = "F:/KuGou/My anata.swf";
private static String path5 = "F:/KuGou/BY2-爱丫爱丫.mp3";
private JPanel jContentPane;
// 构造方法
public PlayLocalMP3() {
this.setSize(300, 300);
this.setLocation(300, 300);
//添加一个两行两列的格子布局
jContentPane = new JPanel(new GridLayout(2, 2));
this.add(jContentPane);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} //妈的难道只支持.mp3格式的?
//路径还不能带英文!? -- 经测试可以带中文啊!!
//这个是用 JMF 播放的!
public static void main(String[] args) {
File f1 = new File(path5);
Player player = null;
try {
MediaLocator locator = new MediaLocator(f1.toURI().toURL());
player = Manager.createRealizedPlayer(locator);
player.prefetch();
player.start();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// player.close();
}
}
// 这个是间接调用系统的播放器播放的!
public static void main2(String[] args) {
PlayLocalMP3 pmp3 = new PlayLocalMP3();
File mp3File = new File("F:/KuGou/Cinderella.mp3");
pmp3.playTheMP3(mp3File);
}
//通过调用系统默认的MP3播放器播放MP3(比如 酷狗、 迅雷看看播放器 什么的!)
private void playTheMP3(File file) {
final Player dualPlayer;
try {
if(file.getAbsolutePath().endsWith("mp3")){
// mp3 file will invoke system default player to play
System.out.println("system player is invoked.");
// 用cmd播放执行文件的命令(支持各种格式,比如MP3、exe、jar...):
// "cmd.exe /c 可识别格式文件在磁盘上的绝对路径"
Runtime.getRuntime().exec("cmd.exe /c" + file.getAbsolutePath());
return;
}
// 这里开始是JMF处理WAV文件,之前的MP3放开运行的话,不支持44.1Kz
String locateString = "file:///" + file.getAbsolutePath(); // + fileName;
dualPlayer = Manager.createPlayer(new MediaLocator(locateString));
dualPlayer.addControllerListener(new ControllerListener(){
@Override
public synchronized void controllerUpdate(ControllerEvent event) {
if(event instanceof RealizeCompleteEvent){
Component comp;
if((comp = dualPlayer.getVisualComponent()) != null){
System.out.println("comp 不为 null!");
comp.setVisible(true);
PlayLocalMP3.this.setVisible(true);
jContentPane.add(comp);
} else {
System.out.println("comp为: " + comp);
}
// 验证此容器及其所有子组件。
// 使用 validate 方法会使容器再次布置其子组件。
// 已经布置容器后,在修改此容器的子组件的时候(在容器中添加或移除组件,
// 或者更改与布局相关的信息),应该调用上述方法。
jContentPane.validate();
}
}
});
dualPlayer.start();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }从中挑选一个吧,记得要结贴给分啊!