关于JFrame的播放声音文件的问题,急急急急!!! 参考书,教程上都写的是Applet小程序的声音播放,那么在JFrame中怎么现实声音的播放呢?请高手帮忙,发点具体代码上来,也可以联系本人,本人QQ:9349599,谢谢了。本人急啊,星期5就要上讲台演试程序了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Applet小程序都能写出来,那JFrame也应该不成问题呀 用JMF的payer可以。Frame只是个显示用的框架,怎么可能播放声音?Applet是用它本身的方法去播放的,和Frame没有关系。 如下代码所示,详见http://bbs.netjava.org/index.php?topic=12.0import sun.audio.*;import java.awt.*;import java.io.*;public class SoundPlayer extends Frame implements FilenameFilter { Button openButton = new Button("打开"); Button playButton = new Button("播放"); Button loopButton = new Button("循环"); Button stopButton = new Button("停止"); Label filename = new Label(" "); File theFile = null; AudioData theData = null; InputStream nowPlaying = null; public SoundPlayer() { super("Sound Player"); resize(300, 200); Panel north = new Panel(); north.setLayout(new FlowLayout(FlowLayout.LEFT)); north.add(new Label("File: ")); north.add("North", filename); add("North", north); Panel south = new Panel(); south.add(openButton); south.add(playButton); south.add(loopButton); south.add(stopButton); add("South", south); } public static void main(String[] args) { SoundPlayer sp = new SoundPlayer(); sp.show(); } public void open() { FileDialog fd = new FileDialog(this, "请选择一个要播放的文件:"); fd.setFilenameFilter(this); fd.show(); try { theFile = new File(fd.getDirectory() + "/" + fd.getFile()); if (theFile != null) { filename.setText(theFile.getName()); FileInputStream fis = new FileInputStream(theFile); AudioStream as = new AudioStream(fis); theData = as.getData(); } } catch (IOException e) { System.err.println(e); } } public void play() { stop(); if (theData == null) open(); if (theData != null) { AudioDataStream ads = new AudioDataStream(theData); AudioPlayer.player.start(ads); nowPlaying = ads; } } public void stop() { if (nowPlaying != null) { AudioPlayer.player.stop(nowPlaying); nowPlaying = null; } } public void loop() { stop(); if (theData == null) open(); if (theData != null) { ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData); AudioPlayer.player.start(cads); nowPlaying = cads; } } public boolean action(Event e, Object what) { if (e.target == playButton) { play(); return true; } else if (e.target == openButton) { open(); return true; } else if (e.target == loopButton) { loop(); return true; } else if (e.target == stopButton) { stop(); return true; } return false; } public boolean accept(File dir, String name) { name = name.toLowerCase(); if (name.endsWith(".au")) return true; if (name.endsWith(".wav")) return true; return false; }} 问个关于制作编译器的问题 哪个知道爱帮网用得哪些技术做的? 回车怎么激活按钮事件 请教 grid 布局遇到的问题 谁有用java写的html全解析程序. 高手請進 SWT连接视频设备DVR-DVS;SWT与视频;SWT与摄像头 如何得到焦点focus 怎样写一个通用的连接各种数据库的程序??谢谢。最近有源码? 请问如何查看和设置tomcat的编码方式? 外国一家公司的一道超难推理面试题,如果你做对了,直接录取拿5K以上的工资不成问题!!!!!! 如何获得JButton的相对位置?
http://bbs.netjava.org/index.php?topic=12.0import sun.audio.*;
import java.awt.*;
import java.io.*;
public class SoundPlayer extends Frame implements FilenameFilter { Button openButton = new Button("打开");
Button playButton = new Button("播放");
Button loopButton = new Button("循环");
Button stopButton = new Button("停止");
Label filename = new Label(" ");
File theFile = null;
AudioData theData = null;
InputStream nowPlaying = null; public SoundPlayer() {
super("Sound Player");
resize(300, 200);
Panel north = new Panel();
north.setLayout(new FlowLayout(FlowLayout.LEFT));
north.add(new Label("File: "));
north.add("North", filename);
add("North", north);
Panel south = new Panel();
south.add(openButton);
south.add(playButton);
south.add(loopButton);
south.add(stopButton);
add("South", south);
} public static void main(String[] args) {
SoundPlayer sp = new SoundPlayer();
sp.show();
} public void open() {
FileDialog fd = new FileDialog(this, "请选择一个要播放的文件:");
fd.setFilenameFilter(this);
fd.show();
try {
theFile = new File(fd.getDirectory() + "/" + fd.getFile());
if (theFile != null) {
filename.setText(theFile.getName());
FileInputStream fis = new FileInputStream(theFile);
AudioStream as = new AudioStream(fis);
theData = as.getData();
}
}
catch (IOException e) {
System.err.println(e);
}
} public void play() {
stop();
if (theData == null) open();
if (theData != null) {
AudioDataStream ads = new AudioDataStream(theData);
AudioPlayer.player.start(ads);
nowPlaying = ads;
}
} public void stop() {
if (nowPlaying != null) {
AudioPlayer.player.stop(nowPlaying);
nowPlaying = null;
}
} public void loop() {
stop();
if (theData == null) open();
if (theData != null) {
ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
AudioPlayer.player.start(cads);
nowPlaying = cads;
}
} public boolean action(Event e, Object what) { if (e.target == playButton) {
play();
return true;
}
else if (e.target == openButton) {
open();
return true;
}
else if (e.target == loopButton) {
loop();
return true;
}
else if (e.target == stopButton) {
stop();
return true;
} return false; } public boolean accept(File dir, String name) { name = name.toLowerCase();
if (name.endsWith(".au")) return true;
if (name.endsWith(".wav")) return true;
return false; }}