Link to home
Start Free TrialLog in
Avatar of jdaues
jdaues

asked on

play sound in application, again

i would like to revisit my previous question about playing sound in an application.
i cannot get the example in the previous example to work.
anybody who can give me code like the example that compiles and runs gets the booty.
Avatar of jdaues
jdaues

ASKER

ok, let's make more interesting...100 points
it seems that it's not easy to play sound from application  ...
you can check the JavaSound / JavaMedia packages (www.javasoft.com)

Put again the whole question.
I don't want to search through all.

Dracula
Avatar of jdaues

ASKER

well, i didn't think it was easy because i had looked thru about 10 books, none of which had an example, and 2 of which said that it couldn't be done.
Sailwind, however, says the he/she can do it.

(we're talking jdk 1.1.x here. from what i read, jdk1.2 makes it pretty easy)

here is what Sailwind wrote:


    We'll pass in the AudioClip object from the applet to
    the application.

    let's have the application to play the audioclip, we'll call it
    audioPlayer.

    public class audioPlayer extends Frame {

          AudioClip clip;

          // constructor
          audioPlayer(AudioClip clip) {
              this.clip = clip;

              setSize(200,200);
              setVisible(true);
              setOpaque(true);
              show();

              playAudio(clip);
          }

          // method that plays the audio clip
          public void playAudio(clip) {
                // do the playing function with the clip
          }

          public static void main(String[] argv) {
               AudioPlayer ap = new AudioPlayer();
          }      
    }

    Now we'll define the applet that calls this application            

    class AudioApplet extends Applet {

          init() {

               filename = "mymusic.wav";
               URL url = new URL(getCodeBase() + filename);

               AudioClip ac= this.getToolKit.getAudioclip(url);
               AudioPlayer ap = new AudioPlayer(ac);
         }

         public static void main(String[] argv) {
           init();
        }
    }

    And viola. The applet will get the audio clip, and pass it
    to the AudioPlayer application who does the real work.
    This is a pseudocode, so forgive me if I misspelled some
    of the methods. But I'm sure you get the idea. Hope this helps!


Avatar of jdaues

ASKER

i have discovered from another source that what i may need is in sun.audio.* classes in the JDK.  The AudioDevice class uses mmedia.dll under NT (part of JDK). but i suppose that means its for windows only.

Yes, i was going to suggest using "sun.audio.*" classes. It is where all the real sound related work is done for the Sun's JDK (Windows + Solaris). For Microsofts JVM probably there are some com.microsoft.audio.* classes that have the same functionality.

The problem is that loading of audioclips in applet is processed from the AppletContext. AppletContext is interface.The real class that implements this behaviour is granted from the browser (in NN it is probably sun.audio.AudioClip.class, in IE - com.microsoft.audio.AudioClip.class). So applets work in browser enviroment and browser does the real Audio loading and playing for them (applets can reach this behaviour using getAppletContext()). Applications don't run in browser enviroment. If you want to play sound from application, you can
1.) use some undocumented (and not so platform undependant - JVM implementation specific) features.
2.) use some library that is supposed to work. JavaSoft's API JavaMedia / JavaSound may help you (I haven't used it, but I think that it is 1.1 compatible)

i don't know who is 'Sailwind', but this code is pure rubish (excuse me)
there is no such method (in my JDK at least)
   AudioClip ac= this.getToolKit.getAudioclip(url);
the only way you can get AudioClip (in Applet) is
   AudioClip ac = applet.getAppletContext().getAudioclip(url);

and what is AudioPlayer ???

you can take a look at https://www.experts-exchange.com/Q.10096989 ("Sound in Java applets")it was about applet that plays some other (not .au format) music.
I made some comments that may be interesting for you.

so genarally - you can't
but there are some (partially) workarounds ;-)

hope this helps
  heyhey
Avatar of jdaues

ASKER

i want my code to work on PC and Mac.
maybe the thing to do is to go to JDB 1.2 ....yikes...

Avatar of jdaues

ASKER

JDK 1.2

again.

1. have you checked JavaSound (JavaMedia) API - they are probably 1.1 compatible?
2. will I get some points ? :))

best regards
  heyhey
so can I lock the question and get the points ?
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdaues

ASKER

sorry for the delay