我写了一个Applet程序,想播放音频文件,但出现了异常,各路高手帮忙看一下,不胜感激!
以下是源代码:(其中的文件夹“新歌1”和本程序的源文件都存放在src文件夹里面)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Audio extends Applet implements ActionListener,ItemListener{
AudioClip clp;
Button ply,lop,stp;
Choice ch;
public void init(){
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.blue);
setFont(new Font("隶书",Font.BOLD,18));
ch=new Choice();
ch.add(getParameter("src//新歌1//传奇.wma"));
ch.add(getParameter("src//新歌1//羽泉-深呼吸.wma"));
ch.add(getParameter("src//新歌1//Banaroo-I Love You You Love Me.wma"));
ch.add(getParameter("src//新歌1//张靓颖-天下无双.wma"));
add(ch);
ch.setBounds(20,60,150,30);
ch.addItemListener(this);
ply=new Button("播放");
lop=new Button("循环播放");
stp=new Button("停止播放");
ply.setBackground(Color.yellow);
lop.setBackground(Color.BLUE);
stp.setBackground(Color.magenta);
ply.setBounds(200, 30, 80, 30);
lop.setBounds(200,80,80,30);
stp.setBounds(200,130,80,30);
add(ply); add(lop); add(stp);
lop.addActionListener(this);
ply.addActionListener(this);
stp.addActionListener(this);
clp=getAudioClip(getCodeBase(),"src//新歌1//传奇.wma");
}
public void itemStateChanged(ItemEvent e){
clp=getAudioClip(getCodeBase(),ch.getSelectedItem());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ply) clp.play();
else if(e.getSource()==lop) clp.loop();
else if(e.getSource()==stp) clp.stop();
}
}
以下是源代码:(其中的文件夹“新歌1”和本程序的源文件都存放在src文件夹里面)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Audio extends Applet implements ActionListener,ItemListener{
AudioClip clp;
Button ply,lop,stp;
Choice ch;
public void init(){
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.blue);
setFont(new Font("隶书",Font.BOLD,18));
ch=new Choice();
ch.add(getParameter("src//新歌1//传奇.wma"));
ch.add(getParameter("src//新歌1//羽泉-深呼吸.wma"));
ch.add(getParameter("src//新歌1//Banaroo-I Love You You Love Me.wma"));
ch.add(getParameter("src//新歌1//张靓颖-天下无双.wma"));
add(ch);
ch.setBounds(20,60,150,30);
ch.addItemListener(this);
ply=new Button("播放");
lop=new Button("循环播放");
stp=new Button("停止播放");
ply.setBackground(Color.yellow);
lop.setBackground(Color.BLUE);
stp.setBackground(Color.magenta);
ply.setBounds(200, 30, 80, 30);
lop.setBounds(200,80,80,30);
stp.setBounds(200,130,80,30);
add(ply); add(lop); add(stp);
lop.addActionListener(this);
ply.addActionListener(this);
stp.addActionListener(this);
clp=getAudioClip(getCodeBase(),"src//新歌1//传奇.wma");
}
public void itemStateChanged(ItemEvent e){
clp=getAudioClip(getCodeBase(),ch.getSelectedItem());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ply) clp.play();
else if(e.getSource()==lop) clp.loop();
else if(e.getSource()==stp) clp.stop();
}
}
弄个.au .avi看下有声没
这两种格式的文件applet确定能播放,并且有声音
java.lang.NullPointerException: cannot add null item to Choice
at java.awt.Choice.insertNoInvalidate(Unknown Source)
at java.awt.Choice.addItem(Unknown Source)
at java.awt.Choice.add(Unknown Source)
at Audio.init(Audio.java:14)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
看路大侠能不能再帮忙看一下?谢啦!
我想你这里应该是形如 getParameter("fileName") 之类的吧?
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Audio extends Applet implements ActionListener,ItemListener{
AudioClip clp;
Button ply,lop,stp;
Choice ch;
public void init(){
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.blue);
setFont(new Font("隶书",Font.BOLD,18));
ch=new Choice();
ch.add(getParameter("src//新歌1//传奇.wma"));
ch.add(getParameter("src//新歌1//羽泉-深呼吸.wma"));
ch.add(getParameter("src//新歌1//Banaroo-I Love You You Love Me.wma"));
ch.add(getParameter("src//新歌1//张靓颖-天下无双.wma"));
add(ch);
ch.setBounds(20,60,150,30);
ch.addItemListener(this);
ply=new Button("播放");
lop=new Button("循环播放");
stp=new Button("停止播放");
ply.setBackground(Color.yellow);
lop.setBackground(Color.BLUE);
stp.setBackground(Color.magenta);
ply.setBounds(200, 30, 80, 30);
lop.setBounds(200,80,80,30);
stp.setBounds(200,130,80,30);
add(ply); add(lop); add(stp);
lop.addActionListener(this);
ply.addActionListener(this);
stp.addActionListener(this);
clp=getAudioClip(getCodeBase(),"src//新歌1//传奇.wma");
}
public void itemStateChanged(ItemEvent e){
clp=getAudioClip(getCodeBase(),ch.getSelectedItem());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ply) clp.play();
else if(e.getSource()==lop) clp.loop();
else if(e.getSource()==stp) clp.stop();
}
}
public class Test { public static void main(String[] args) {
// System.out.println(test(3));
digui(5);
}
public static int digui(int n){
int num = 0 ;
System.out.println(n);
return num==n ? num:digui(n-1);
}
public static Double test(int n){
Double s = Math.pow(n,n);
System.out.println(s);
if(n>1){
return s+=test(--n);
}else{
return s ;
}
// return n == 1 ? s : (s+=test(n-1));
}
}