1

ハード ドライブからサウンド ファイルを再生するテスト プログラムを動作させようとしていますが、引き続き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)

これにより、以前よりも情報量が増える可能性があります。アクセスを高速化するためにファイルをデスクトップに移動しましたが、場所が問題ではないことは確かです。

4

2 に答える 2

1

バックスラッシュの代わりにスラッシュを使用する必要があります。Linuxパス名( "/home/username/song.wav")を使用してLinuxマシンでコードを実行しましたが、例外はスローされませんでした(警告のみ)。

私はあなたが使用する必要があるとほとんど確信しています:

Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav");

Windowsは、ディレクトリ区切り文字としてスラッシュと円記号の両方を受け入れることに注意してください。

ただし、これでうまくいかない場合は、System.getProperty( "user.dir")メソッドとSystem.getProperty( "file.separator")メソッドを使用して、現在のディレクトリの文字列を作成してみてください。最後にファイル名を連結します。

String directory = System.getProperty("user.dir") + System.getProperty("file.separator");
String puzzleSolutionGet = directory + "PuzzleSolutionGet";
Sound testsong = new Sound(puzzleSolutionGet);

このようにして、コードはどのOSでも実行されます。

また、例外がスローされたと思われる行を切り替えてコードをデバッグし、例外がスローされるまですべての行にステップインする時間を大幅に節約できます。デバッガーを使用したことがない場合は、IBMによる基本を説明するクールなリンクを次に示します。

http://www.ibm.com/developerworks/library/os-ecbug/

于 2012-04-25T15:12:58.270 に答える
0
Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav");
...
Sound(String filename){
     try
     {
        songPath = new URL(getCodeBase(),filename); // Get the Sound URL

パスは からの相対パスである必要がありcodebase、スラッシュを使用し/ます。EG がcodebase次を指している場合:

"C:\\Users\\MyName\\lib"

コンストラクターは次のようになります。

Sound testsong = new Sound("../Music/PuzzleSolutionGet.wav");
于 2012-04-25T14:46:59.883 に答える