Solved

Playing Sounds

Posted on 2000-04-23
8
290 Views
Last Modified: 2008-03-17
I'm planning out a java program that I would like to run as an applet and as an application.

I would like it to play sounds.

I have some questions going in.

I would like to use java 1.1 so a larger number of people will get the applet.

Can I play .wav files with java 1.1?  If not what file types can I use and how would I convert .wav files to that type?

Could I play the sounds if I put a main() in the applet and run it as an app?

Steve
0
Comment
Question by:Steve34
  • 5
  • 3
8 Comments
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 50 total points
ID: 2742430
>>Can I play .wav files with java 1.1?
You most certainly can.
>>Could I play the sounds if I put a main() in the applet and run it as an app?
I understand what you mean here, but don't say that, it's incorrect. Say "Would it work if I did it from an Application?" - Yes, it will work.

Ok, here's how you do it from an applet:

AudioClip clip = getAudioClip(getCodeBase(),"music.wav");
clip.play();

or just
play(getCodeBase(),"music.wav");

just make sure there's a music.wav file in the directory you are running your applet from.

To do that from an application, you just have to use a different method (which you can also use for an applet btw, but it's less convenient) to get the clip.

AudioClip clip = null;
try{
  clip = Applet.newAudioClip(new File("music.wav")).toURL());
} catch (MalformedURLException e){
      e.printStackTrace();
   }
// and now play it just the same way:
clip.play();


you can also invoke clip.stop() to stop it when its playing and clip.loop() to have it play again when it finishes infinitely until you invoke clip.stop()...

If you have more questions on the subject please ask...
0
 

Author Comment

by:Steve34
ID: 2759583
Thanks for the examples!
0
 

Author Comment

by:Steve34
ID: 2760923
Sasha;

I just tried your code.

With the applet version the sound did not play from the *.wav file I used.  My java console gave me the error message "invalid file type".

Your code worked when I substituted a *.au file in.

The code for playing sounds in applications didn't work at all.

Is there a way around any of this or an easy way to convert *.wav files to *.au files?

Thanks in advance

Steve
0
 

Author Comment

by:Steve34
ID: 2760926
Sasha;

I just tried your code.

With the applet version the sound did not play from the *.wav file I used.  My java console gave me the error message "invalid file type".

Your code worked when I substituted a *.au file in.

The code for playing sounds in applications didn't work at all.

Is there a way around any of this or an easy way to convert *.wav files to *.au files?


I am using Netscape 4.75 on a Windows98 box

Thanks in advance

Steve
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2760960
Hmm, actually, I have a working application that I've developed that plays wav files. I conclude from this that this was added in JDK 1.2. You have several ways out of this: You can install the Java plugin on your computer which will enable you running applets in JDK 1.2. Download it from http://java.sun.com/products/plugin/index.html. You could find a au to wav converter which is probably easier, but unfortunately I can't help you with it, I don't know such a converter... Try searching the web with a search engine for "wav au converter"... I'm sure it will produce positive results...
0
 

Author Comment

by:Steve34
ID: 2763613
Okay, I gave up on apps/sounds with java 1.1.

I tried your code with Java2 and I still got nothing.  I am a win98 box with the jdk1.2 and jre 1.2.

I downloaded Sun's sound app example and it works ( but is complex and confusing ).

Here is my/your code ( how I implemented it ).  any help would be appreciated.

( all files are in the same directiory )

Thanks in advance

Steve



import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class SoundExp
{
    public static void main(String[] args )
    {
        AudioClip clip = null;

        try
        {
            clip = Applet.newAudioClip(   new File("gong.wav").toURL()   );
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        // and now play it just the same way:
        clip.play();
                     

    }// end function main()


}// end class SoundExp
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2763709
I can't explain it to you very well because I don't completely understand it myself but this has to do with threads. When you are running main, a new non daemon Thread is started and main is called from it. When you call clip.play() from main, it opens another daemon thread and plays the clip in it, the method play() returns immediately. The condition when a Java program dies is when all the non daemon threads have died, so when play() returns, the program exits killing the newly opened daemon thread that is supposed to play the clip. When you do something related to the AWT, like opening a window, another non daemon thread is started, the AWTEventDispatcher, which is responsible to call all of the event listeners whenever an even occurs over a component, as long as that thread is alive, the program will not end. Ever wondered why the following doesn't exit the program at once?

public class MyApp{

  public static void main(String [] args){
    new Frame().show();
  }

}

Exactly for the reason I explained - when you show the Frame, the AWTEventDispatcher is run and  the application will not end until it exits.
Here is an example that does work:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;



public class MyApp extends WindowAdapter implements ActionListener{

  static AudioClip clip;
 
  public static void main(String[] args ){
    Frame f = new Frame();
    f.reshape(50,50,500,500);
    f.addWindowListener(new MyApp());
    f.setLayout(null);
    f.show();
    Button b = new Button("Click me");
    b.addActionListener(new MyApp());
    b.reshape(50,50,60,20);
    f.add(b);
   
    try{
      clip = Applet.newAudioClip(   (new File("Gong.wav")).toURL()   );
    } catch (MalformedURLException e){
        e.printStackTrace();
      }
    // and now play it just the same way:
  }// end function main()

  public void windowClosing(WindowEvent evt){
    evt.getWindow().dispose();
    System.exit(0);
  }

  public void actionPerformed(ActionEvent evt){
    if (clip == null)
      System.out.println("clip is null");
    else{
      System.out.println("playing clip");
      clip.play();
    }
     
  }

}

Good luck, Sasha.
0
 

Author Comment

by:Steve34
ID: 2764506
Wow, it does work.  Thanks!

The only difference is that the sound playing code is embedded in a frame obj.   Interesting!

There are some hard core SUN types on the JDC forums.

I think I will bring up your points there.   I might learn something.

This is the second time since I have been programming Java that the opaque event handling threads have become unopaque in relation to my programs ( only when I've tried interesting things ).

The authors should really start talking about these things more.

Thanks again

Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now