Jump to content

Sound in Java Application

- - - - -

  • Please log in to reply
11 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
NO Sound!
Im trying to build a class to provide sound support for another program. Below is the stand alone test.
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudioSupport
{
    String filename;
    File soundFile = null;
    Clip audioClip;
    AudioInputStream audio;


    public AudioSupport(String string)
    {
        filename =  string;
        init();
    }

    public  void init()
    {
        soundFile = new File(filename);
        
        if (!soundFile.exists()) 
           System.err.println("pathname does not exist: " + filename);
        try {
            audioClip = AudioSystem.getClip();
            audio = AudioSystem.getAudioInputStream(soundFile);
            audioClip.open(audio);
            audioClip.start();    //a loop here doesn't seem to work either

             } catch (LineUnavailableException lue)
             {
                System.out.println("caught: " + lue);
             } catch(UnsupportedAudioFileException uafe)
             {
                 System.out.println("caught: " + uafe);
             }
               catch(IOException ioe)
               {
                   System.out.println("caught: " + ioe);
               }
    }

    public static void main(String args[])
    {
        new AudioSupport("D:/path/toproject/src/Sound/notify.wav");
    }
}
Can't seem to figure out whats wrong. No compile errors, but no sound either
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Well I learned something...
java - why this code doesn't play the sound file - Stack Overflow

In short, you have to call Thread.sleep( ... ) in order for the daemon thread to run, and sleep the main thread.
The main thread is finishing before the daemon thread, which is why the program promptly quits, and no sound is played.

You'll have to calculate the amount of time to sleep by taking the length of each frame and multiplying by the rate.


        long timeToSleep = (long) (clip.getFrameLength() * clip.getFormat().getFrameRate() * 1000);

        clip.start();

        Thread.sleep(timeToSleep);
Odd, IMO.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You code works, Java will however stop when the end of init() is reached, which is right after the start().
It's likely that you actually start playing, but after 0-1 secs, the program finishes and stops so you never actually hear it.

Add Thread.sleep(10000); After the init(), or as 2nd line in the main method.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

lethalwire said:

Well I learned something...Odd, IMO.
That makes both of us. Maybe i am blind but i looked through the api for Clip also looked at Thrail on the Clip section and saw no mention of a daemon.
Works well now. Much Thanks.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, I had this screen open a bit too long without refreshing, didn't see the reply :P

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
No probs. You were on target anyway!
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This math doesn't seem correct though...

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Maybe try getMicrosecondLength

getMicrosecondLength() | Clip (Java 2 Platform SE 5.0)
Instead of
clip.getFrameLength() * clip.getFormat().getFrameRate() 


---------- Post added at 01:25 AM ---------- Previous post was at 01:21 AM ----------

And think about it:
[COLOR=#333333][FONT=monospace](clip.getFrameLength() * clip.getFormat().getFrameRate() * 1000)[/FONT][/COLOR]
getFrameRate is frames / seconds, right. So let's assume a very very short file, with 2 frames, at a rate of 2 frames/second. Without much math we just know it lasts 1 second, right eg. 1000 ms.
With this formula it becomes
2 * 2 * 1000 == Too much
[COLOR=#333333][FONT=monospace](clip.getFrameLength() [SIZE=4][B]/[/B][/SIZE] clip.getFormat().getFrameRate() * 1000)[/FONT][/COLOR]
Might work better.

#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This is correct:

long timeToSleep = (long) ( audio.getFrameLength() / audioClip.getFormat().getFrameRate() * 1000);



---------- Post added at 05:27 PM ---------- Previous post was at 05:26 PM ----------

1 minute too late =(

That's what it was though, the /...

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
^^

Anyway, just wanted to add 1 more thing.

For this test class, it's ok to do this Thread.sleep(), I don't think you actually need that in your "full" program tho,
cause I swear the first program that keeps on playing its music till the end -even after I finished it- gets thrown off my pc :-P

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

wim DC said:

^^

Anyway, just wanted to add 1 more thing.

For this test class, it's ok to do this Thread.sleep(), I don't think you actually need that in your "full" program tho,
cause I swear the first program that keeps on playing its music till the end -even after I finished it- gets thrown off my pc :-P
I guess I'll revise my game for you then. :p

#12
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Well that is correct b/c its a chat app and while the chat window(main thread) is open the sound will play without interruption(once the window is out of focus). But i wanted to be sure so i can reuse to code in other programs, or give someone who may probably be stuck like i was.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users