1

So far I only found two ways:

  1. throw exception
  2. play nothing

ad.2. Pretty recent code (2010)

but does nothing, not sound at all.

ad.1. For example:

All I get is exception "Audio Device Unavailable". As this article (2008) -- lsof |grep snd — how to free a linux sound device -- explains Java has to get exclusive access to audio device.

However I cannot afford such condition. I use sound to notify myself on long running process (several hours), I cannot get rid of all sounds (including those forced via Flash ads) just to make Java comfy.

So for now I use total extreme -- I simply launch external program: https://stackoverflow.com/a/8370223/210342 . This is ugly as hell because well new program is launched just to make notification.

Question

Is there currently a way to play a WAV file:

  • on Linux,
  • in cooperative way (during Java playback there may be other audio played as well),
  • which makes sound,
  • in Java (no launching external programs)?

Java 1.6, openSUSE 11.4

Community
  • 1
  • 1
greenoldman
  • 16,895
  • 26
  • 119
  • 185

4 Answers4

2

Have you tried the Java Media Framework (JMF)?

The Java Media Framework API (JMF) enables audio, video and other time-based media to be added to applications and applets built on Java technology. This optional package, which can capture, playback, stream, and transcode multiple media formats, extends the Java 2 Platform, Standard Edition (J2SE) for multimedia developers by providing a powerful toolkit to develop scalable, cross-platform technology.

A T
  • 13,008
  • 21
  • 97
  • 158
  • Thank you. Actually it does not work for me ;-) but it does not work for a lot of people, even for the plain wav files. Thanks to other posts with problems related to JMF I found other suggestions -- like trying VLC-J. – greenoldman Jan 17 '12 at 20:20
0

This site provides a simple swing application to capture and play wav files http://www.java-tips.org/java-se-tips/javax.sound/capturing-audio-with-java-sound-api.html You can modify and use the code from the below site http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

Cant you simply create a new thread whenever the play action is needed. So that the playing continues irrespective of the background processes

Raveesh Sharma
  • 1,486
  • 5
  • 21
  • 38
  • Please, pay a little attention -- I gave the second link already, it is repeating the same method I already mentioned it is **not** working. The sound is blocked not by Java, but by the system, adding new threads won't change the fact the audio is blocked (at least Java sees it that way). – greenoldman Jan 17 '12 at 19:42
0

So, thanks to AT answer I drifted from JMF to JMF problems, and that led me to VLC-J. And this is the answer -- in order to use multimedia in Java program simply don't use Java (pity, btw. that Java in year 2012 does not have reliable multimedia layer, gee.).

Marking as solved, because VLC-J allowed me to play audio without problem.

Main project site: http://code.google.com/p/vlcj/

Minimal playback program: http://code.google.com/p/vlcj/source/browse/trunk/vlcj/src/test/java/uk/co/caprica/vlcj/test/minimalmp3/Mp3Test.java

greenoldman
  • 16,895
  • 26
  • 119
  • 185
-1

Java has this support built in (available in javax.sound, javax.sound.sampled packages) on 1.4+ version. It works colaboratively with other applications on the system as it uses platform mixer (ALSA or PULSE)

Daniel Voina
  • 3,185
  • 1
  • 27
  • 32
  • The more you repeat the "answer" it does not become more right. Please read the question next time, thank you in advance. – greenoldman Jan 17 '12 at 17:38
  • @macias: In this case, have you checked if the permissions on /dev/(audio|mixer) allow you to open the device? Can you write to PULSE sockets? What distribution are you using? – Daniel Voina Jan 18 '12 at 07:45
  • If I couldn't, I wouldn't get any sound, would I? Again, check the question, I gave information about Java and system. However, in a second I will close this question as answered, because yesterday's AT's answer gave me the right idea. – greenoldman Jan 18 '12 at 20:22
  • @macias: indeed vlcj is a solution but it adds an extra layer of indirection java will depend on libvlc that will depend on OSS|ALSA|Pulse. Please check this thread for a different (not necessarily better) solution: http://forums.fedoraforum.org/showthread.php?t=248326 – Daniel Voina Jan 19 '12 at 15:26
  • of course I would gladly focus on Java-only solution, but sadly, I didn't find any, and don't have time to test and test, this and that, and something else. I spent with Java-only approach too many time, and vlc-j took me what, downloading the jar, and pasting the code. Job done. – greenoldman Jan 20 '12 at 07:03