How to play audio in Java Application

前端 未结 4 2010
悲&欢浪女
悲&欢浪女 2020-12-07 05:40

I\'m making a java application and I need to play audio. I\'m playing mainly small sound files of my cannon firing (its a cannon shooting game) and the projectiles exploding

4条回答
  •  广开言路
    2020-12-07 06:18

    I guess you should run your playSound method in a background thread as mentioned in the doc here

    "you'll probably want to invoke this playback loop in a separate thread from the rest of the application program, so that your program doesn't appear to freeze when playing a long sound"

    Maybe by doing something like

    // shared executor
    ExecutorService soundExecutor = ...; //Executors.newSingleThreadExecutor();
    ...
    final File soundFile = ...;
    soundExecutor.submit(new Runnable(){
       public void run(){
            SoundUtils.playSoundFile(soundFile);
       }
    });
    

提交回复
热议问题