H2ONaCl
H2ONaCl

Reputation: 11289

Java sound replaying via Clip and AudioInputStream is not working

This is a slightly modified example from the Java Sound info page. https://stackoverflow.com/tags/javasound/info Unfortunately, it only plays the sound once but the intention is twice.

import java.io.File;
import javax.sound.sampled.*;

public class TestNoise {
    public static void main(String[] args) throws Exception {
        File f = new File("/home/brian/drip.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(f);

        AudioFormat af = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, af);
        Clip clip = (Clip)AudioSystem.getLine(info);

        clip.open(ais);
        clip.start();    // heard this
        Java.killTime(); 
        clip.start();    // NOT HEARD
        Java.killTime();
    }
}

Edit: To understand the answer, see the link provided by Wanderlust or just do what it says in the comment below his answer.

Upvotes: 0

Views: 2688

Answers (2)

user23047726
user23047726

Reputation: 1

This wonderful API worked for me:

import javax.sound.sampled.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

/**
 * Handles playing, stopping, and looping of sounds for the game.
 *
 * @author Langdon Staab
 * @author Tyler Tomas
 */
public class Sound {
    Clip clip;

    @SuppressWarnings("CallToPrintStackTrace")
    public Sound(String filename) {
        try (InputStream in = getClass().getResourceAsStream(filename)) {
            assert in != null;
            InputStream bufferedIn = new BufferedInputStream(in);
            try (AudioInputStream audioIn = AudioSystem.getAudioInputStream(bufferedIn)) {
                clip = AudioSystem.getClip();
                clip.open(audioIn);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Malformed URL: \n" + e);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Unsupported Audio File: \n" + e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Input/Output Error: \n" + e);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            throw new RuntimeException("Sound: Line Unavailable Exception Error: \n" + e);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // play, stop, loop the sound clip
    }

    public void play() {
        clip.setFramePosition(0);  // Must always rewind!
        clip.start();
    }

    public void loop() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    public void stop() {
        clip.stop();
    }

    public boolean isPlaying() {
        //return false;
        return clip.getFrameLength() > clip.getFramePosition();
    }
}

Upvotes: 0

wanderlust
wanderlust

Reputation: 1936

For playing the clip for the second time you must call

clip.start();
clip.stop();

cause after the second call of clip.start(); it is trying to play the file from the place where it stopped previously.

Upvotes: -1

Related Questions