最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javasound - How to play audio (wav) in Java? AudioSystem.getMixerInfo() returns empty array - Stack Overflow

programmeradmin0浏览0评论

I followed the steps here to play audio in Java. From the docs:

Your program might need a specific kind of mixer, or you might want to display a list of all the available mixers so that the user can select one. In either case, you need to learn what kinds of mixers are installed. AudioSystem provides the following method:

static Mixer.Info[] getMixerInfo()

However when I call the method I am getting an empty array. I am developing using JDK 21 on WSL. I am developing a "plain" console Java application without any GUI libraries. Things I have checked:

  • I am able to play sound using ffplay
  • I have libjsound.so under $JAVA_HOME/lib

Does the use of this API require one to develop a rich Java application like JavaFX etc.? I don't see anything like that mentioned in the docs.

I followed the steps here to play audio in Java. From the docs:

Your program might need a specific kind of mixer, or you might want to display a list of all the available mixers so that the user can select one. In either case, you need to learn what kinds of mixers are installed. AudioSystem provides the following method:

static Mixer.Info[] getMixerInfo()

However when I call the method I am getting an empty array. I am developing using JDK 21 on WSL. I am developing a "plain" console Java application without any GUI libraries. Things I have checked:

  • I am able to play sound using ffplay
  • I have libjsound.so under $JAVA_HOME/lib

Does the use of this API require one to develop a rich Java application like JavaFX etc.? I don't see anything like that mentioned in the docs.

Share Improve this question edited Jan 31 at 17:22 greg-449 111k235 gold badges109 silver badges159 bronze badges asked Jan 31 at 17:18 morpheusmorpheus 20.4k29 gold badges108 silver badges186 bronze badges 4
  • 1 I am developing using JDK 21 on WSL. Did you try running it directly in Windows? – g00se Commented Jan 31 at 17:22
  • AudioSystem has no special requirements, a quick test here it gives 7 mixers just calling from a program with just a main method (macOS) – greg-449 Commented Jan 31 at 17:32
  • When I run ldd libjsound.so, it says it depends on libasound.so.2. Does your WSL have that? – VGR Commented Jan 31 at 18:05
  • You can try this playWavFile method. – Gilbert Le Blanc Commented Jan 31 at 19:56
Add a comment  | 

1 Answer 1

Reset to default 0

I think you can count on letting Java select the default mixer, unless you have some special need (which you haven't mentioned).

The code examples in the wiki for the Javasound tag have examples and information for how to use a Clip or SourceDataLine.

The supporting code package, javax.sound.sampled is part of Core Java, (no additional libraries needed), but this package does require the inclusion of the module java.desktop.

发布评论

评论列表(0)

  1. 暂无评论