我正在尝试让一个测试程序运行,它可以从我的硬盘驱动器中播放声音文件,但我继续得到NullPointerException
. 这是到目前为止的代码,我主要从代码中的梦想中提取出来:
package ForSelf;
import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.net.*;
public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
private AudioClip song; // Sound player
private URL songPath; // Sound path
Sound(String filename){
try
{
songPath = new URL(getCodeBase(),filename); // Get the Sound URL
song = Applet.newAudioClip(songPath); // Load the Sound
}catch(Exception e){
e.printStackTrace();
//e.getMessage();
} // Satisfy the catch
}
public void playSound(){
song.loop(); // Play
}
public void stopSound(){
song.stop(); // Stop
}
public void playSoundOnce(){
song.play(); // Play only once
}
}
public void init(){
Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
Container c = getContentPane();
c.setBackground(Color.white);
c.setLayout(null);
playerPanel = new JPanel();
playSound = new JButton("Play");
stopSound = new JButton("Stop");
playerPanel.add(playSound);
playerPanel.add(stopSound);
c.add(playerPanel);
testsong.playSound();
}
public void paint(Graphics g){
super.paint(g);
playerPanel.setLocation(0, 0);
playerPanel.setSize(300, 300);
}
public void actionPerformed(ActionEvent e){
}
}
JComponents 将在稍后实现以播放和停止歌曲文件,我不确定它是否只是我的文件路径或什么,所以任何帮助将不胜感激。
更新后的代码如下:
package ForSelf;
import java.applet.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.net.*;
public class AudioTest extends JApplet{
JPanel playerPanel;
JButton playSound, stopSound;
public class Sound // Holds one audio file
{
private AudioClip song; // Sound player
private URL songPath; // Sound path
Sound(String filename){
try
{
songPath = new URL(getCodeBase(),filename); // Get the Sound URL
song = Applet.newAudioClip(songPath); // Load the Sound
}catch(Exception e){
e.printStackTrace();
//e.getMessage();
} // Satisfy the catch
}
public void playSound(){
song.loop(); // Play
}
public void stopSound(){
song.stop(); // Stop
}
public void playSoundOnce(){
song.play(); // Play only once
}
}
public void init(){
**String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
String puzzleSolutionGet = directory + "PuzzleSolutionGet";
Sound testsong = new Sound(puzzleSolutionGet);**
//Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");
Container c = getContentPane();
c.setBackground(Color.white);
c.setLayout(null);
playerPanel = new JPanel();
playSound = new JButton("Play");
stopSound = new JButton("Stop");
playerPanel.add(playSound);
playerPanel.add(stopSound);
c.add(playerPanel);
testsong.playSound();
}
public void paint(Graphics g){
super.paint(g);
playerPanel.setLocation(0, 0);
playerPanel.setSize(300, 300);
}
public void actionPerformed(ActionEvent e){
}
}
但是,当我运行它时,我遇到了以下异常:
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(URL.java:574)
at java.net.URL.<init>(URL.java:464)
at ForSelf.AudioTest$Sound.<init>(AudioTest.java:23)
at ForSelf.AudioTest.init(AudioTest.java:45)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
java.lang.NullPointerException
at ForSelf.AudioTest$Sound.playSound(AudioTest.java:32)
at ForSelf.AudioTest.init(AudioTest.java:62)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
这可能比以前提供更多信息。我什至将文件移动到我的桌面以便更快地访问,但我确定位置不是问题。