• 0

[JAVA] Sound


Question

Hi guys,

Below is a code that i use to play sound

bf = new BufferedInputStream(KeySchedulingScreen.class.getResourceAsStream(filename));

ais = AudioSystem.getAudioInputStream(bf);

AudioFormat format = ais.getFormat();

if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED)

{

format = new AudioFormat(

AudioFormat.Encoding.PCM_SIGNED,

format.getSampleRate(),

format.getSampleSizeInBits()*2,

format.getChannels(),

format.getFrameSize()*2,

format.getFrameRate(),

true); // big endian

ais = AudioSystem.getAudioInputStream(format, ais);

}

// Create the clip

DataLine.Info info = new DataLine.Info(

Clip.class, ais.getFormat(), ((int)ais.getFrameLength()*format.getFrameSize()));

Clip clip = (Clip) AudioSystem.getLine(info);

// This method does not return until the audio file is completely loaded

clip.open(ais);

// Start playing

clip.start();

This code works fine on windows. But when i try on my mac. It gave me an exception.

java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian, and buffers of 9838592 to 9838592 bytes is supported.

The line in bold is the line that is throwing that error.

Any idea how to solve ?

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Unfortunately (and somebody correct me if I'm wrong), but I read somewhere that JavaSound is completely implemented for Mac OSX. This doesn't do well for the cross-platform ideals of Java. The options are:

Quicktime

CoreAudio

Native bindings to CoreAudio in Java.

Sorry I couldn't be much help

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/#findComment-589701086
Share on other sites

  • 0

Sun seem to think that JavaSound is a core API and should be completely cross-platform. They even use it as the demo app in the following x-platform tutorial http://java.sun.com/developer/technicalArt...aLP/JavaToMac3/

Do you get the same error regardless of what (kind of) file you try to play, or is it just one (type of) file?

+Antaris: Is there a typo in your post?

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/#findComment-589701840
Share on other sites

  • 0
AudioStream as = new AudioStream(myClass.class.getResourceAsStream("Sound/cow.wav"));

AudioPlayer.player.start(as);

The above code works for both MAC and Windows. But when compiled, it gave me warnings.

sun.audio.AudioStream is Sun proprietary API and may be removed in a future release

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/#findComment-589704212
Share on other sites

  • 0

Hmn.. kjordan, i was previously using JMF but i could not access resources in my jar file using JMF.

mPlayer = Manager.createPlayer(myClass.class.getResource("Sound/cow.wav"));

mPlayer.start();

The above code works if my SOUND folder with cow.wav in it is at the root folder. But if i package the Sound folder in a jar and correctly pointing it, i get a exception saying cannot find a player for that stream

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/#findComment-589709850
Share on other sites

  • 0
The above code works if my SOUND folder with cow.wav in it is at the root folder. But if i package the Sound folder in a jar and correctly pointing it, i get a exception saying cannot find a player for that stream

Folder and file names seem to be case sensitive when getting resources from a jar.

Link to comment
https://www.neowin.net/forum/topic/662026-java-sound/#findComment-589713536
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I'd say the first one failed to be as popular as Apple anticipated, but the easy adjustment here is to make fewer of them next time around. It would only be a "flop" if it isn't possible for Apple to recover the design and factory tooling costs given the number of units sold, which I doubt would be the case. It isn't like no one bought them; it just failed to become the new hot phone of the year.
    • You're right that it does not follow the plain meaning of the word, but in this context, it is a legal term defined in 49 U.S.C. Kind of how "wire fraud" laws apply even if a physical wire was not used. Given that it is codified in law, and it isn't just automotive journalists that don't understand evolving technology, I highly doubt congress would change a well understood term just because technology makes the term slightly less actuate.
    • This is exactly why I keep saying we are not ready for human free self-driving. These little "bugs" are may seem like random one-offs. There was also the Waymo that drove between police with drawn weapons and the suspect they were pointing them at. From a software perspective it is easy to understand how those extremely rare situations may not have been programed for, but that is the point. If AI needs to be told to watch out for every possible contingency, then it can never be successful. There will always be the possibility of a first encounter that the AI needs to understand to avoid.
    • TeraCopy 4.0 Final by Razvan Serea TeraCopy is a compact program designed to copy and move files at the maximum possible speed, also providing you with a lot of features. Copy files faster. TeraCopy uses dynamically adjusted buffers to reduce seek times. Asynchronous copy speeds up file transfer between two physical hard drives. Pause and resume transfers. Pause copy process at any time to free up system resources and continue with a single click. Error recovery. In case of copy error, TeraCopy will try several times and in the worse case just skips the file, not terminating the entire transfer. Interactive file list. TeraCopy shows failed file transfers and lets you fix the problem and recopy only problem files. Shell integration. TeraCopy can completely replace Explorer copy and move functions, allowing you work with files as usual. TeraCopy is free for non-commercial use only. For commercial use you need to buy a license. The paid version of the program includes the following features: Copy/move to your favorite folders. Save reports as HTML and CSV files. Select files with the same extension/folder. Remove the selected files from the copy queue. Download: TeraCopy 4.0 | 14.6MB (Freeware, paid upgrade available) View: TeraCopy Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Eurosoft10 earned a badge
      Week One Done
    • One Month Later
      Eurosoft10 earned a badge
      One Month Later
    • One Year In
      Skeet Campbell earned a badge
      One Year In
    • One Month Later
      Sharbel earned a badge
      One Month Later
    • First Post
      BizSAR earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      586
    2. 2
      +Edouard
      187
    3. 3
      Michael Scrip
      74
    4. 4
      PsYcHoKiLLa
      72
    5. 5
      neufuse
      69
  • Tell a friend

    Love Neowin? Tell a friend!