Jan Bodnar
Jan Bodnar

Reputation: 11637

Trouble playing the whole audio clip

I play sound effects (WAV files) in a game with javax.sound.sampled.Clip. However, the effect that has 2 seconds is not played entirely, only a part of it. If I add clip.loop(2) then the sound is played multiple times correctly, i.e. the whole 2 seconds. What is wrong and how to fix it?

package com.zetcode;

import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;

public enum SoundEffect {

    EXPLODE("resources/explosion.wav"),
    HITGROUND("resources/impact.wav"),
    CANNON("resources/cannon.wav");

    private Clip clip;

    SoundEffect(String soundFileName) {
        try {
            URL url = this.getClass().getClassLoader().getResource(soundFileName);

            try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url)) {
                clip = AudioSystem.getClip();
                clip.open(audioInputStream);
            }
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            JOptionPane.showMessageDialog(null, "Cannot play sound", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    public void play() {

        if (clip.isRunning()) {
            clip.stop();
        }
        clip.setFramePosition(0);
        clip.start();
    }

    static void init() {
        values();
    }    
}

Upvotes: 1

Views: 256

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

Are you aware and accounting for the fact that when a clip is played (via start() method), the thread playing the code is a daemon? Unlike regular threads, a daemon thread will not prevent a Java program from closing. If your program ends before the sound has finished executing, the daemon status thread also ends even if it is not finished.

Are you trying to run the sample code SoundClipTest or SoundEffectDemo? It looks to me like SoundClipTest is going to exit as soon as the program executes the start() method, whereas the SoundEffectDemo will persist and allow the sound to finish playing.


** Edit 1 **


Am just now noticing that you have made your SoundEffect an enum. I've never seen that approach used before.

One approach I've used is to create a class called GameSound, and have its constructor preload each Clip individually and hold them in memory, ready for playback. Then, I create a method or methods for their playback.

gameSound.playExplosion();

The GameSound class would have instance variables for each sound effect.

private Clip explosion;
private Clip cannon;

And GameSound constructor would have:

URL url = this.getClass().getResource("resources/explosion.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url); 
explosion = AudioSystem.getClip();
explosion.open(ais);

and GameSound would have method:

public void playExplosion()
{
    if (explosion.isRunning()) {
        explosion.stop();
    }
    explosion.setFramePosition(0);
    explosion.start();
}

If you prefer to have the play method take an argument and use that on a switch structure to select which sound to play, that is fine, too, especially as it eliminates some code duplication.

This approach might work better (easy to add to GameSound as code gets more complex), and shouldn't be too difficult to code. I'd be curious to learn whether or not changing to this plan makes the problem go away or not.


** Edit 2 **


Sorry for mistakes in the above code. I fixed them when I made the following example. One thought, though, when I went to listen to the sound effects you linked, several of the explosions were kind of truncated to begin with. Have you played the sounds in another app (like Windows Groove Music, or from the website where you got them) and verified that they are different when you play them in your code?

Here is a quickie "GameSound". I downloaded the Explosion+2.wav file from your reference as it was longer than the others and has a clear ending, and am referencing that.

package stackoverflow.janbodnar;

import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class GameSound {

    private final Clip explosion;

    public GameSound() throws LineUnavailableException, 
    IOException, UnsupportedAudioFileException
    {
        URL url = this.getClass().getResource(
                "resources/Explosion+2.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(url);
        explosion = AudioSystem.getClip();
        explosion.open(ais);
    }

    public void playExplosion()
    {
        if (explosion.isRunning()) {
            explosion.stop();
        }
        explosion.setFramePosition(0);
        explosion.start();
    }
}

And here is a simple Swing button that calls GameSound.

package stackoverflow.janbodnar;

import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GameSoundTest extends JFrame
{
    private static final long serialVersionUID = 1L;

    private final GameSound gameSound;

    public GameSoundTest() throws LineUnavailableException,
    IOException, UnsupportedAudioFileException
    {
        gameSound = new GameSound();

        JButton button = new JButton("Play Explosion");
        button.addActionListener(e -> gameSound.playExplosion());
        getContentPane().add(button);
    }

    private static void createAndShowGUI()
    {
        JFrame frame;
        try 
        {
            frame = new GameSoundTest();
            frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            frame.setBounds(0, 0, 200, 200);
            frame.setVisible(true);
        } 
        catch (LineUnavailableException | IOException 
                | UnsupportedAudioFileException e) 
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }
}

This all assumes, of course, that there is a subfolder called "resources" with the explosion SFX in it.

Upvotes: 1

Related Questions