How can I play sound in Java?

后端 未结 10 807
傲寒
傲寒 2020-11-22 04:38

I want to be able to play sound files in my program. Where should I look?

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 05:25

    This thread is rather old but I have determined an option that could prove useful.

    Instead of using the Java AudioStream library you could use an external program like Windows Media Player or VLC and run it with a console command through Java.

    String command = "\"C:/Program Files (x86)/Windows Media Player/wmplayer.exe\" \"C:/song.mp3\"";
    try {
        Process p = Runtime.getRuntime().exec(command);
    catch (IOException e) {
        e.printStackTrace();
    }
    

    This will also create a separate process that can be controlled it the program.

    p.destroy();
    

    Of course this will take longer to execute than using an internal library but there may be programs that can start up faster and possibly without a GUI given certain console commands.

    If time is not of the essence then this is useful.

提交回复
热议问题