ハード ドライブからサウンド ファイルを再生するテスト プログラムを動作させようとしていますが、引き続き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)
これにより、以前よりも情報量が増える可能性があります。アクセスを高速化するためにファイルをデスクトップに移動しましたが、場所が問題ではないことは確かです。