Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem Playing WAV files in J2SE 1.5  (Read 2564 times)
0 Members and 1 Guest are viewing this topic.
Offline Andrew Davison

Junior Devvie


Medals: 2



« Posted 2004-08-07 00:29:08 »

Dear All,

I've included a test program at the end, with a bug which seems
to have appeared in J2SE 1.5 (beta 2).

When I call the program with an ordinary WAV file, most of
the time nothing is played, although the program does wait
for the duration of the audio before exiting.

Compile the program:
 > javac PlayClip.java

Run:
 > java PlayClip dog.wav

Any WAV file will do.

- Andrew




// PlayClip.java

import java.io.*;
import javax.sound.sampled.*;


public class PlayClip implements LineListener
{
 private Clip clip = null;


 public PlayClip(String fnm)
 {
   loadClip(fnm);

   if (clip != null) {
     System.out.println("Playing...");
     clip.start();   // start playing
   }

   // wait for the sound to finish playing; guess at 10 mins!
   System.out.println("Waiting");
   try {
     Thread.sleep(600000);   // 10 mins in ms
   }
   catch(InterruptedException e)
   { System.out.println("Sleep Interrupted"); }
 } // end of PlayClip()

 private void loadClip(String fnm)
 {
   try {
     // link an audio stream to the sound clip's file
     AudioInputStream stream = AudioSystem.getAudioInputStream(
                         getClass().getResource(fnm) );

     AudioFormat format = stream.getFormat();
     DataLine.Info info = new DataLine.Info(Clip.class, format);

     // make sure sound system supports data line
     if (!AudioSystem.isLineSupported(info)) {
       System.out.println("Unsupported Clip File: " + fnm);
       System.exit(0);
     }

     // get clip line resource
     clip = (Clip) AudioSystem.getLine(info);

     // listen to clip for events
     clip.addLineListener(this);

     clip.open(stream);    // open the sound file as a clip
     stream.close(); // we're done with the input stream
   } // end of try block

   catch (Exception e) {
     System.out.println(e);
     System.exit(0);
   }
 } // end of loadClip()

 public void update(LineEvent lineEvent)
 // called when the clip's line detects open, close, start, stop events
 {
   // has the clip has reached its end?
   if (lineEvent.getType() == LineEvent.Type.STOP) {
     System.out.println("Exiting...");
     clip.stop();
     System.exit(0);
   }
 } // end of update()

 // --------------------------------------------------

 public static void main(String[] args)
 {
   if (args.length != 1) {
     System.out.println("Usage: java PlayClip <clip file>");
     System.exit(0);
   }
   new PlayClip(args[0]);
 } // end of main()

} // end of PlayClip.java


Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Offline Andrew Davison

Junior Devvie


Medals: 2



« Reply #1 - Posted 2004-08-10 00:57:57 »

I'm replying to myself.

The problem is to do with the duration of the sound.

If the WAV file is less than a second long then no
sound is played.

My hacky solution is to obtain the duration of the
sound with:
       double duration = clip.getMicrosecondLength()/1000000.0;

Then calculate a count which is used to loop the
sound so it plays for a total of 1 second or more.

     loopCount = (int) (1.0 / duration);


     // clip.start();   // start playing
     clip.loop(loopCount);

This is clearly not so great if the sound is meant to
be brief.

- Andrew

Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2004-08-10 02:21:50 »

With Applet's sound stuff you have a delay each time a sound is played for the first time (they get moved into ram then).

With the sampled package it's a bit different - Clips are played instantly (because they are already in ram). However, the very first time you try to play a sound a mixer object (the default mixer) is created. Therefore you end up with a small delay, which was in your case big enough for not playing the sound at all.

You can test that easily. Play the sample wait a second, play it again, wait 2 seconds and exit... or make a little test application, which plays the sound if you click a button. You'll see that there won't be a delay the second time.

For games it doesn't really matter. You play lot's of sounds and most people don't even notice that the first sound effect was somewhat off.

However, if you really want to fix that... creating a mixer object by yourself should do the trick.

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2004-08-10 02:50:32 »

When I load sounds for my game, I'll create a Clip of the sound as I load it, and call what I need to get the Mixer created, and then just toss it away.  Getting the mixer made seems to me to be part of the loading process, so I think that's a good time to do it.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Andrew Davison

Junior Devvie


Medals: 2



« Reply #4 - Posted 2004-08-10 04:37:54 »


As I understand it, the clip.open(stream); line will
gather the necessary resources, including a reference
to the default mixer.

If I add a sleep period after that call, the problem
is still present. A call to isOpen() says that the clip
is open.

This problem doesn't occur when I run the program
from my first posting in J2SE 1.4.2.

- Andrew

Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #5 - Posted 2004-08-10 13:01:02 »

hmm I'd guess that it's still open because you haven't played the Clip yet.  For mine I just .flush() and .close() it after that first load.  I think the only way it'll do that on its own is if you call .start() to let it run through and thus avoid the permanent open-ness... but you don't want it to play when you're loading it because you just want to make the mixer so it won't sound silly Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Andrew Davison

Junior Devvie


Medals: 2



« Reply #6 - Posted 2004-08-10 23:48:28 »

Sun has just added this bug to their database, as
Bug Id: 5085008

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5085008

(It may take a day or so to appear in their external
database.)

- Andrew

Dr. Andrew Davison
Dept. of Computer Engineering
Prince of Songkla University, Hat Yai
Songkhla 90112, Thailand
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!