Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Issues with Sound setMicrosecondPosition in Mac Os X

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issues with Sound setMicrosecondPosition in Mac Os X

    Hi All

    I am trying to implement sound player in Java as following.
    public void play() throws Exception
    	{
    		AudioInputStream stream = AudioSystem.getAudioInputStream( new FileInputStream(new File(m_path)));
    		AudioFormat format = stream.getFormat();
    		DataLine.Info info = new DataLine.Info( Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
     
    		soundClip = (Clip) AudioSystem.getLine(info);
    		soundClip.open(stream);
     
    		soundClip.addLineListener(new LineListener() {
    			public void update(LineEvent event) {
    				if (event.getType() == LineEvent.Type.STOP)
    				{
    					System.out.println("playcomplete event: " + soundClip.getMicrosecondPosition());
    				}
    			}
    		});
                   soundClip.start();
     
    	}
    After completion of play LineEvent.Type.STOP is fired.

    Method call soundClip.getMicrosecondPosition() displays the proper values in windows but in Mac OSX it always prints 0.

    Please help me out


    Thanks in advance

    -Surendhar
    Last edited by helloworld922; August 17th, 2010 at 11:46 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Sound setMicrosecondPosition in Mac Os X

    Please surround your code with highlight tags (see my sig for how to do this).

    What is soundClip? It is entirely possible that between different implementations of the JVM there can be discrepencies in the behavior of certain functions, particularly when regarding "os-specific" components such as sound, video, GUI interfaces, etc. (I believe the JVM on Macs is implemented by Apple rather than Sun/Oracle, not completely positive about this).

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Sound setMicrosecondPosition in Mac Os X

    'soundClip' is instance of
    javax.sound.sampled.Clip.

    public void play() throws Exception
        {
            AudioInputStream stream = AudioSystem.getAudioInputStream( new FileInputStream(new File(m_path)));
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info( Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
     
            soundClip = (Clip) AudioSystem.getLine(info);
            soundClip.open(stream);
     
            soundClip.addLineListener(new LineListener() {
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP)
                    {
                        System.out.println("playcomplete event: " + soundClip.getMicrosecondPosition());
                    }
                }
            });
                   soundClip.start();
     
        }

    Is there any other way to know whether playing sound is completed?
    OR

    Do we have any other API to play .wav files.

    Please share your comments

    Thanks
    Surendhar
    Last edited by surendhar; August 18th, 2010 at 09:02 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Sound setMicrosecondPosition in Mac Os X

    Well, instead of trying to get soundClip's position, LineEvent has a frame position which records where the event occured. Note that this doesn't necessarily represent real time, so you'll need to convert it into seconds/milliseconds.

    public void update(LineEvent event)
    {
        if(event.getType() == LineEvent.Type.STOP)
        {
            System.out.println("play complete event: " + (double)(event.getFramePosition() * soundClip.getMicroSecondLength()) / soundClip.getFrameLength());
        }
    }

    There are other sound API's used in "gaming libraries", such as LWJGL (Light-weight Java Gaming Library) and JOAL (Java OpenAL), if you want you can give them a go.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with Sound setMicrosecondPosition in Mac Os X

    Thanks a lot for your reply.

    I have also the approach you suggested, but it doesn't work for Mac.

    Do we any other built-in API to play Sound, because I don't want to use any third party libraries.

    Thanks
    Surendhar

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Issues with Sound setMicrosecondPosition in Mac Os X

    As far as I know, no (at least not for sampled sound, there's the Midi API but that's for a different purpose). What is possible is that this a bug with the apple implementation (perhaps a resource is getting unloaded or the values aren't getting updated correctly). Have you tried debugging/stepping through your code to try and see where something could possibly be going wrong?

    If you're feeling bold, you can use JNI to provide the functionality you need, but this would amount to basically the same thing as using 3rd party libraries, and you will also need custom native code for every system your program is going to be running on. It's much easier to use a 3rd party library.

Similar Threads

  1. Java sound API
    By bondage in forum Java SE APIs
    Replies: 3
    Last Post: July 22nd, 2010, 02:01 PM
  2. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 08:49 AM
  3. need help with Timer and sound
    By amahara in forum AWT / Java Swing
    Replies: 4
    Last Post: February 18th, 2010, 12:22 PM
  4. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM
  5. Replies: 0
    Last Post: October 2nd, 2009, 11:51 PM