Threads Clip loop loopt nicht

realodds

Aktives Mitglied
Hallo, in meinem Spiel will ich die Musik abspielen. Dazu habe ich eine Klasse erstellt:
Java:
package gui;

import java.io.File;
import java.util.ArrayList;

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

public class Music {

    private Clip clip;
    private String path;
    private long stopped;
    private final boolean continous;
    private final File file;
    private boolean running = false, runned = false;

    public Music(String task, boolean continous) {
        // test
        this.continous = true;
        //this.continous = continous;
        this.path = task;
        file = new File(this.path);
        if (!file.exists())
            System.err.println("File \"" + this.path + "\" is not existing");
    }

    public void play() {
        if (!Frame.getFrame().getKeyBoard().isMute()) {
            playMusic();
            running = true;
            runned = true;
        }
    }

    public Music clone() {
        return new Music(path, continous);
    }

    public boolean isRunning() {
        return clip.isRunning();
    }

    private void playMusic() {
        if (file == null)
            return;
            AudioInputStream audio = null;
            try {
                audio = AudioSystem.getAudioInputStream(file);
                clip = AudioSystem.getClip();
                clip.open(audio);
            } catch (Exception e) {
                e.printStackTrace();
            }
            clip.start();
            if (continous)
                clip.loop(10); / * egal ob 10, 11 oder unendlich, es spielt nicht mehrfach ab */
    }
}
Der Sound wird 1 mal abgespielt und danach nicht mehr.
Was muss ich ändern??
 

mihe7

Top Contributor
https://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/Clip.html hat gesagt.:
You can also create a loop, so that when the clip is played it will cycle repeatedly. Loops are specified with a starting and ending sample frame, along with the number of times that the loop should be played.
-> Ich würde mal sagen, dass Du erstmal setLoopPoints aufrufen musst.
 

Ähnliche Java Themen

Neue Themen


Oben