简体   繁体   中英

How to make sound loop using this method?

I did sound in a REALLY wierd way, do you think that there may be a way to loop it? cuz it only plays once then stops. Here is code for sound:

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class Sound {
private final static int BUFFER_SIZE = 12800000;
private static File soundFile;
private static AudioInputStream audioStream;
private static AudioFormat audioFormat;
private static  SourceDataLine sourceLine;

/**
 * 
 * @param filename the name of the file that is going to be played
 *
 */
public static void playSound(String filename){

    String strFilename = filename;

    try {
        soundFile = new File(strFilename);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e){
        e.printStackTrace();
       System.exit(1);
    }

    audioFormat = audioStream.getFormat();

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
    try {
        sourceLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceLine.open(audioFormat);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }


    sourceLine.start();

    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData, 0, abData.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            @SuppressWarnings("unused")
            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
        }
    }

    sourceLine.drain();
    sourceLine.close();
}
}

To activate it i do:

new Thread(new Runnable() {
          public void run() {
            Sound.playSound("hurt.au");
          }
        }).start();

(if i do just Sound.playSound("hurt.au"); the game freezes because the sound plays on the games main thread so i put the sound in it's own thread to save the game from freezing)

So the question is, how do i loop it?

You will need some sort of test to see whether the sound currently playing is finished or not. If you are making a game, I might suggest using some libraries to make it easier. Such as slick2d. This has inbuilt functions to loop a sound instead of just playing it once. If you choose not to, you will have to keep track of the thread and on every update of your game state look at the sound object and ask the sourceline if it has finished playing or not. If it has then sourceline.start() else no-op. You could also put the thread inside the sound.playsound method itself, thereby making your code a little bit less coupled.

http://www.slick2d.org/

I really recommend using a 3rd party library to make it easier on yourself though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM