JAVA播放音乐的一个小问题

[复制链接]
查看11 | 回复2 | 2008-12-29 11:59:55 | 显示全部楼层 |阅读模式
以下是网上的一段代码,可用的
我根据我自己的需要,做了一个测试,只想要他的一部分代码。是
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JFrame;
public class TestAudioPlaySound extends JFrame{//播放声音用
URL file1 = getClass().getResource("call.wmv"); //声音文件1


public TestAudioPlaySound(){

//URL file1 = getClass().getResource("call.wmv"); //声音文件1

this.setSize(500, 500);

this.setVisible(true);

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

Button bb=new Button("play");



this.add(bb);

bb.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ee){

try{

System.out.println("ddd");
//AudioClip chosenClip = sound1; //选择的声音剪辑对象
//
/ chosenClip.play(); //播放选择的声音剪辑对象一次

AudioClip sound1 =java.applet.Applet.newAudioClip(file1); //声音剪辑对象1

sound1.loop();

}

catch(Exception e){e.printStackTrace();}



}

});



}

public static void main(String[] args) {

new TestAudioPlaySound();

}
}
结果却错误啦!!!!!!为什么!!!!!!!!!无法播放
报异常
空指针异常

回复

使用道具 举报

千问 | 2008-12-29 11:59:55 | 显示全部楼层
是不是这句出错了:AudioClip sound1 = java.applet.Applet.newAudioClip(file1); // 声音剪辑对象1这里可能是你的URL对象file1没有创建成功,所以才会出现异常。你要查看下你的声音文件call.wmv的路径对不对?如果没有问题,那就是file1的URL是否正确。或者sound1.loop(); 前面是否应该加上sound1.play();我觉得你最好还是把错误信息都贴出来!...
回复

使用道具 举报

千问 | 2008-12-29 11:59:55 | 显示全部楼层
出现什么异常~~~希望贴出异常代码来...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行