如何在Java中预加载AudioClip

时间:2015-04-30 11:50:48

标签: java performance audio libraries preload

我使用这个课程在我的节目中播放一首歌:

public class Sound extends Thread {
    private static AudioClip song;

    public Sound(String songUrl) {

        try{
            URL songFile = SoundRessources.class.getResource(songUrl);
            song = Applet.newAudioClip(songFile);
        }catch(NullPointerException e){
            System.out.println("impossible de trouver la musique");
        }

    }

    public void playSound() {
        song.play();    
    }
}

但是当我这样播放我的声音时:

new Sound("xxxxx/xxxx.wav").play();

声音开始播放需要几秒钟。

我的问题:我能预先加载声音吗?也许有一个库比这个小程序响应更快?或者你有更好的想法立即播放我的声音吗?

2 个答案:

答案 0 :(得分:1)

声音可以预加载为byte []。

在你的主要班级

ApplicationSound.setSplotchSound(AudioSystem.getAudioInputStream(
              Main.class.getResourceAsStream("splotch-sound.wav")));

和ApplicationSound

public class ApplicationSound
{
  private static byte[] splotchSound = { };
  private static AudioFormat audioFormat = new AudioFormat(44100, 16, 2, true, false);

  public static void setSplotchSound(AudioInputStream audioInputStream)
  {
    try
    {
       splotchSound = audioInputStream.readAllBytes();
    }
    catch (IOException e)
    {
       // nothing
    }
  }

  public static AudioInputStream getSplotchSound()
  {
     return new AudioInputStream(new ByteArrayInputStream(splotchSound),
        audioFormat, splotchSound.length);
  }
}

并在您的应用程序内

Clip clip = AudioSystem.getClip();
              clip.open(ApplicationSound.getSplotchSound());
              clip.start();

答案 1 :(得分:0)

这是我播放.wav音频文件的方法,可以立即启动它们。

// java -cp ./lib/mylib.jar SoundTest2 "clip1=rumble2.wav" "clip2=tos.wav"
// Type 1,2,q in a console and ENTER.
import java.util.*;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;

public class SoundTest2 {
    
    public Clip play(String filename, boolean autostart, float gain) throws Exception {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filename));
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.setFramePosition(0);

        // values have min/max values, for now don't check for outOfBounds values
        FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
        gainControl.setValue(gain);
        
        if(autostart) clip.start();
        return clip;
    }
    
//**************************************
    public static void main(String[] args) throws Exception {
        Map<String,String> params = parseParams(args);
        SoundTest1 test1 = new SoundTest1();
                
        Clip clip1 = test1.play(params.get("clip1"), false, 3.0f);
        Clip clip2 = test1.play(params.get("clip2"), false, 3.0f);

        Scanner scanner = new Scanner(System.in);
        while(true) {
            Clip clip=null;
            String cmd=scanner.nextLine();
            if(cmd.equals("1")) clip=clip1;
            else if(cmd.equals("2")) clip=clip2;
            else if(cmd.equals("q")) break;
            
            if(clip!=null) {
                clip.setFramePosition(0);
                clip.start();
            }
        }
    }
    
    private static Map<String,String> parseParams(String[] args) {
        Map<String,String> params = new HashMap<String,String>();
        for(String arg : args) {
            int delim = arg.indexOf('=');
            if (delim<0) params.put("", arg.trim());
            else if (delim==0) params.put("", arg.substring(1).trim());
            else params.put(arg.substring(0, delim).trim(), arg.substring(delim+1).trim() );
        }
        return params;
    }
    
}