我写了一个播放声音文件的java应用程序,编译都没问题,但运行时就是不能播放声音,这段程序如下:
package menjin;
import sun.audio.*;
import java.io.*;
import java.applet.AudioClip;
import java.applet.Applet;
import java.net.URL;public class aa
{
public static void main(String args[]) throws FileNotFoundException,
IOException {
URL cb; File f = new File("F:\\menjin\\menjin\\music.wav");
cb=f.toURL();
System.out.println(f.getName());
AudioClip aau;
aau=Applet.newAudioClip(cb);
aau.play(); }
}
请问是什么原因?
package menjin;
import sun.audio.*;
import java.io.*;
import java.applet.AudioClip;
import java.applet.Applet;
import java.net.URL;public class aa
{
public static void main(String args[]) throws FileNotFoundException,
IOException {
URL cb; File f = new File("F:\\menjin\\menjin\\music.wav");
cb=f.toURL();
System.out.println(f.getName());
AudioClip aau;
aau=Applet.newAudioClip(cb);
aau.play(); }
}
请问是什么原因?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class playAudio extends JApplet {
AudioClip sound1, sound2, currentSound;
JButton playSound, loopSound, stopSound;
JComboBox choose;
public void init( ){ // 初始化各对象
Container container = getContentPane( ); // 获取内容格
container.setLayout( new FlowLayout( ) ); // 设置布局 String choices[ ] = { "Welcome", "Hi" }; // 下拉列表的选项
choose = new JComboBox( choices ); // 定义一个下拉列表 choose.addItemListener( // 定义一个匿名类,实现选择监听
new ItemListener( ) {
// 若重新选择声音文件,将停止当前播放,启动新选的声音剪辑
public void itemStateChanged( ItemEvent e ) {
currentSound.stop( ); // 停止当前播放的声音剪辑
// 确定新选的声音剪辑
currentSound = choose.getSelectedIndex( ) == 0 ? sound1 : sound2;
}
}
); container.add( choose ); // 将组件choose加入到容器中 // 定义按钮对象,并设置事件监听者
ButtonHandler handler = new ButtonHandler( );
playSound = new JButton( "Play" );
playSound.addActionListener( handler );
container.add( playSound ); loopSound = new JButton( "Loop" );
loopSound.addActionListener( handler );
container.add( loopSound ); stopSound = new JButton( "Stop" );
stopSound.addActionListener( handler );
container.add( stopSound ); // 载入声音剪辑,并对currentSound初始化
sound1 = getAudioClip( getDocumentBase( ), "welcome.wav" );
sound2 = getAudioClip( getDocumentBase( ), "hi.au" );
currentSound = sound1;
} // 当用户单击stop按钮时,终止播放声音剪辑
public void stop( ) {
currentSound.stop( );
} // 定义一个内部类,处理按钮事件
private class ButtonHandler implements ActionListener {
// 处理 play,loop和stop 按钮事件
public void actionPerformed( ActionEvent actionEvent ){
if ( actionEvent.getSource( ) == playSound )
currentSound.play( );
else if ( actionEvent.getSource( ) == loopSound )
currentSound.loop( );
else if ( actionEvent.getSource( ) == stopSound )
currentSound.stop( );
}
}
}
import java.io.File;
import java.io.IOException;
import java.net.URL;import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;public class T
{
public static void main(String[] args)
throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
File soundFile = new File("R:\\Test.wav");
URL soundURL = soundFile.toURI().toURL();
AudioInputStream ain = AudioSystem.getAudioInputStream(soundURL);
DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat());
final Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ain);
clip.start();
clip.addLineListener(new LineListener()
{
public void update(LineEvent event)
{
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
}
});
System.in.read();
}
}
我想没有吧.我以前写过一个,就是鼠标在一个组件上点下的时候播放声音,我想对你有帮助private void playSound() {
try{
FileInputStream fileau = new FileInputStream(new File("C:\\WINDOWS\\Media\\Windows XP 气球.wav"));
AudioStream as = new AudioStream(fileau);
AudioPlayer.player.start(as);
}catch (Exception e){
}
}这只是一个方法.在需要的地方调用就是了.多简单
在Applet里是有这个方法的,但如果在应用程序里,一输入这个就报错啊
我跟楼主一样想知道这个问题的解决方法最好是能贴一段代码给我们看一下啦
先谢谢了!