classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] Patch: RFC: make applet use javax.sound.sampled


From: Tom Tromey
Subject: [cp-patches] Patch: RFC: make applet use javax.sound.sampled
Date: 14 Nov 2005 12:24:36 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

I'm not checking this in yet -- I would like someone more familiar
with Applet to look at it first.

This changes Applet to use javax.sound.sampled to implement
AudioClip.  It also updates DummyAppletContext to use this.

I don't know why there is a separate newAudioClip... is the idea that
a typical AppletContext should do caching?

Tom


2005-11-14  Tom Tromey  <address@hidden>

        * java/applet/Applet.java (URLAudioClip): New class.
        (newAudioClip): Implemented.
        * gnu/java/beans/DummyAppletContext.java (getAudioClip): Use
        Applet.newAudioClip.
        (DUMMY_CLIP): Removed.
        (DummyAudioClip): Removed.

Index: gnu/java/beans/DummyAppletContext.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/beans/DummyAppletContext.java,v
retrieving revision 1.2
diff -u -r1.2 DummyAppletContext.java
--- gnu/java/beans/DummyAppletContext.java      2 Jul 2005 20:32:12 -0000       
1.2
+++ gnu/java/beans/DummyAppletContext.java      14 Nov 2005 18:27:02 -0000
@@ -63,7 +63,6 @@
 class DummyAppletContext implements AppletContext
 {
   private static final Enumeration EMPTY_ENUMERATION = 
Collections.enumeration(Collections.EMPTY_SET);
-  private static final AudioClip DUMMY_CLIP = new DummyAudioClip();
 
   DummyAppletContext()
   {
@@ -80,14 +79,7 @@
    */
   public AudioClip getAudioClip(URL url)
   {
-    try
-      {
-       return (url.openConnection() != null ? DUMMY_CLIP : null);
-      }
-    catch (IOException ioe)
-      {
-       return null;
-      }
+    return Applet.newAudioClip(url);
   }
 
   /** Loads the <code>Image</code> instance by delegating to
@@ -169,32 +161,5 @@
   public Iterator getStreamKeys()
   {
     return Collections.EMPTY_SET.iterator();
-  }
-
-  /** Dummy <code>AudioClip</code> implementation that does nothing but
-   * preventing <code>NullPointerException</code>S being thrown in programs
-   * that expect a valid <code>AudioClip</code> instance to be returned by
-   * their Applet.
-   *
-   * @author Robert Schuster
-   */
-  static class DummyAudioClip implements AudioClip
-  {
-    public void play()
-    {
-    }
-
-    public void stop()
-    {
-    }
-
-    public void loop()
-    {
-    }
-
-    public String toString()
-    {
-      return "DummyAudioClip never plays anything - implement javax.sound and 
make us happy :)";
-    }
   }
 }
Index: java/applet/Applet.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/applet/Applet.java,v
retrieving revision 1.14
diff -u -r1.14 Applet.java
--- java/applet/Applet.java     5 Jul 2005 14:46:11 -0000       1.14
+++ java/applet/Applet.java     14 Nov 2005 18:27:03 -0000
@@ -54,6 +54,10 @@
 import javax.accessibility.AccessibleRole;
 import javax.accessibility.AccessibleState;
 import javax.accessibility.AccessibleStateSet;
+import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.Clip;
+import javax.sound.sampled.LineUnavailableException;
+import javax.sound.sampled.UnsupportedAudioFileException;
 
 /**
  * This is the base applet class.  An applet is a Java program that
@@ -257,8 +261,6 @@
    * Returns an audio clip from the specified URL. This clip is not tied to
    * any particular applet.
    *
-   * XXX Classpath does not yet implement this.
-   *
    * @param url the URL of the audio clip
    * @return the retrieved audio clip
    * @throws NullPointerException if url is null
@@ -267,8 +269,7 @@
    */
   public static final AudioClip newAudioClip(URL url)
   {
-    // This requires an implementation of AudioClip in gnu.java.applet.
-    throw new Error("Not implemented");
+    return new URLAudioClip(url);
   }
 
   /**
@@ -521,4 +522,71 @@
       return s;
     }
   } // class AccessibleApplet
+
+  private static class URLAudioClip implements AudioClip
+  {
+    // The URL we will try to play.
+    // This is null if we have already tried to create an
+    // audio input stream from this URL.
+    private URL url;
+
+    // The real audio clip.  This is null before the URL is read,
+    // and might be null afterward if we were unable to read the URL
+    // for some reason.
+    private Clip clip;
+    
+    public URLAudioClip(URL url)
+    {
+      this.url = url;
+    }
+
+    private synchronized Clip getClip()
+    {
+      if (url == null)
+        return clip;
+      try
+        {
+          clip = AudioSystem.getClip();
+          clip.open(AudioSystem.getAudioInputStream(url));
+        }
+      catch (LineUnavailableException _)
+        {
+          // Ignore.
+        }
+      catch (IOException _)
+        {
+          // Ignore.
+        }
+      catch (UnsupportedAudioFileException _)
+        {
+          // Ignore.
+        }
+      url = null;
+      return clip;
+    }
+
+    public void loop()
+    {
+      Clip myclip = getClip();
+      if (myclip != null)
+        myclip.loop(Clip.LOOP_CONTINUOUSLY);
+    }
+
+    public void play()
+    {
+      Clip myclip = getClip();
+      if (myclip != null)
+        myclip.start();
+    }
+
+    public void stop()
+    {
+      Clip myclip = getClip();
+      if (myclip != null)
+        {
+          myclip.stop();
+          myclip.setFramePosition(0);
+        }
+    }
+  }
 } // class Applet




reply via email to

[Prev in Thread] Current Thread [Next in Thread]