3

私は無数の異なる StackOverflow の回答と他のサイトからの回答を見てきましたが、解決策のどれも私の問題を解決していません。私の人生では、.wav ファイルを再生することはできません。

これが私のコードです:

サウンドクラス:

public class Sound {
/**
 * Static file paths for each sound.
 */
public static String stepSound = "/resources/step.wav";

/**
 * Audio input stream for this sound.
 */
private AudioInputStream audioInputStream;

/**
 * Audio clip for this sound.
 */
private Clip clip;

/* -- Constructor -- */

/**
 * Creates a new sound at the specified file path.
 * 
 * @param   path    File path to sound file
 */
public Sound(String path) {
    // Get the audio from the file
    try {
        // Convert the file path string to a URL
        URL sound = getClass().getResource(path);
        System.out.println(sound);

        // Get audio input stream from the file
        audioInputStream = AudioSystem.getAudioInputStream(sound);

        // Get clip resource
        clip = AudioSystem.getClip();

        // Open clip from audio input stream
        clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
        e.printStackTrace();
    }
}

/* -- Method -- */

/**
 * Play the sound.
 */
public void play() {
    // Stop clip if it's already running
    if (clip.isRunning())
        stop();

    // Rewind clip to beginning
    clip.setFramePosition(0);

    // Play clip
    clip.start();
}

/**
 * Stop the sound.
 */
public void stop() {
    clip.stop();
}
}

エラーにつながるコンストラクター呼び出し:

// Play step sound
new Sound(Sound.stepSound).play();

このような問題がこの Web サイトで質問または回答されたのはこれが初めてではないことはわかっていますが、この時点で他の解決策を何時間も試してきましたが、見つけたのは痛みと欲求不満だけです. 必要に応じて、さらにコードを投稿できます。前もって感謝します。

編集: .jar ファイルを解凍し、ファイルが実際に存在することを確認しました。問題は、URL が null になってしまうため、 aNullPointerExceptionがスローされることです。

編集 #2:別の問題が発生した場合に備えて、さらにコードを追加しました。

4

0 に答える 0