How do I play a sound on the server from inside a servlet? The Java Sound API doesn't seem to work.

Jitendra Mehta

[More Q: I am trying to use java sound API in my servlet based application. One of the subclass in my 'playSound.class' extends thread. I am loading audioClip inside this thread, which is not working. It doesn't raise any error. If I skip the thread and load clip directly, it works fine.]

I changed my servlet to implement 'SingleThreadModel' and my code worked! Now I don't know if implementing 'SingleThreadModel' will hamper performance of my application. I am using only one servlet which is mounted when web server starts.

Below is my 'playSound' class.

package moore.mifc.system;
import java.applet.AudioClip;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.GridBagLayout;

import moore.mifc.system.*;
public class playSound {
    SoundList soundList;
    String login = "login.wav";
    String logout   = "logout.wav";
    String menu     = "menu.wav";
    String ready = "systemReady.wav";
    String unknown = "unknown.wav";
    String chosenFile;
    String status;

    static private final Log logSound = new Log ("logSound");

    AudioClip onceClip, loopClip;
    URL codeBase;

        
    boolean looping = false;

    public playSound() {
        startLoadingSounds();
    }



void startLoadingSounds() {
        //Start asynchronous sound loading.
        try {
                codeBase = new URL("file:mifc
es") ;
        
        } catch (MalformedURLException e) {
            logSound.out(e.getMessage());
        }
        
        soundList = new SoundList(codeBase);
        soundList.startLoading(login);
        soundList.startLoading(ready);
        soundList.startLoading(unknown);
    
}

    public void stop() {
        onceClip.stop();        //Cut short the one-time sound.
        if (looping) {
            loopClip.stop();    //Stop the sound loop.
        }
    }    

    public void start() {
        if (looping) {
            loopClip.loop();    //Restart the sound loop.
        }
    }    

   
    public void play( String what) {

        if (what.equalsIgnoreCase("login") )
        {
           //based on time play greetings..
            onceClip = soundList.getClip("login.wav");
            
            if (onceClip == null) {
                logSound.out("Sound " + "login" + " not loaded yet.");
                return;
            }

            stop();
            onceClip.play();     //Play it once.
            return;

        }
        
        else if( what.equalsIgnoreCase("ready") )
        {
            onceClip = soundList.getClip("systemReady.wav");
            
            if (onceClip == null) {
                logSound.out("Sound " + "ready" + " not loaded yet.");
            }
            
            stop();
            //Play it once.
            onceClip.play();
            return;

        }
        else if( what.equalsIgnoreCase("unknown") )
        {

            onceClip = soundList.getClip("unknown.wav");
            if (onceClip == null) {
                logSound.out("Sound " + "logout" + " not loaded yet.");
            }

            stop();
            onceClip.play();     //Play it once.
            return;

        }
        return;

    }
    
class SoundList extends java.util.Hashtable {
    JApplet applet;
    URL baseURL, completeURL;

    public SoundList(URL baseURL) {
        super(5); //Initialize Hashtable with capacity of 5 entries.
        logSound.out( "baseURL*******"+baseURL);
        
        this.baseURL = baseURL;
    }

    public void startLoading(String relativeURL) {

     /* uncomment this only if soundLoader class doesn't work...
	 try {
            completeURL = new URL(baseURL, relativeURL);
            logSound.out( "completeURL*******"+completeURL);
        } catch (MalformedURLException e){
            logSound.out("SoundLoader:"+e.getMessage());
        }

        AudioClip audioClip = Applet.newAudioClip(completeURL);
        soundList.putClip(audioClip, relativeURL);
       **/
        
        new SoundLoader(this, baseURL, relativeURL);
    }

    public AudioClip getClip(String relativeURL) {
        logSound.out( "relativeURL*******"+relativeURL);
        
        return (AudioClip)get(relativeURL);
    }

    public void putClip(AudioClip clip, String relativeURL) {
        put(relativeURL, clip);
    }
}

//This guy doesn't work if my servlet does NOT implement interface 'SingleThreadModel'  
class SoundLoader extends Thread {
    SoundList soundList;
    URL completeURL;
    String relativeURL;
    public SoundLoader(SoundList soundList,URL baseURL, String relativeURL) 
    {
        this.soundList = soundList;
        try {
            completeURL = new URL(baseURL, relativeURL);
        } catch (MalformedURLException e){
        }
        this.relativeURL = relativeURL;
        setPriority(MIN_PRIORITY);
        this.start(); 
    }
    public void run() {
        AudioClip audioClip = Applet.newAudioClip(completeURL);
        soundList.putClip(audioClip, relativeURL);
    }

    }    
    
    
}

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact