View Javadoc
1   /*
2    * Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.applet;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.ByteArrayInputStream;
31  import java.net.URL;
32  import java.net.URLConnection;
33  import java.applet.AudioClip;
34  
35  import com.sun.media.sound.JavaSoundAudioClip;
36  
37  
38  /**
39   * Applet audio clip;
40   *
41   * @author Arthur van Hoff, Kara Kytle
42   */
43  
44  public class AppletAudioClip implements AudioClip {
45  
46      // url that this AudioClip is based on
47      private URL url = null;
48  
49      // the audio clip implementation
50      private AudioClip audioClip = null;
51  
52      boolean DEBUG = false /*true*/;
53  
54      /**
55       * Constructs an AppletAudioClip from an URL.
56       */
57      public AppletAudioClip(URL url) {
58  
59          // store the url
60          this.url = url;
61  
62          try {
63              // create a stream from the url, and use it
64              // in the clip.
65              InputStream in = url.openStream();
66              createAppletAudioClip(in);
67  
68          } catch (IOException e) {
69                  /* just quell it */
70              if (DEBUG) {
71                  System.err.println("IOException creating AppletAudioClip" + e);
72              }
73          }
74      }
75  
76      /**
77       * Constructs an AppletAudioClip from a URLConnection.
78       */
79      public AppletAudioClip(URLConnection uc) {
80  
81          try {
82              // create a stream from the url, and use it
83              // in the clip.
84              createAppletAudioClip(uc.getInputStream());
85  
86          } catch (IOException e) {
87                  /* just quell it */
88              if (DEBUG) {
89                  System.err.println("IOException creating AppletAudioClip" + e);
90              }
91          }
92      }
93  
94  
95      /**
96       * For constructing directly from Jar entries, or any other
97       * raw Audio data. Note that the data provided must include the format
98       * header.
99       */
100     public AppletAudioClip(byte [] data) {
101 
102         try {
103 
104             // construct a stream from the byte array
105             InputStream in = new ByteArrayInputStream(data);
106 
107             createAppletAudioClip(in);
108 
109         } catch (IOException e) {
110                 /* just quell it */
111             if (DEBUG) {
112                 System.err.println("IOException creating AppletAudioClip " + e);
113             }
114         }
115     }
116 
117 
118     /*
119      * Does the real work of creating an AppletAudioClip from an InputStream.
120      * This function is used by both constructors.
121      */
122     void createAppletAudioClip(InputStream in) throws IOException {
123 
124         try {
125             audioClip = new JavaSoundAudioClip(in);
126         } catch (Exception e3) {
127             // no matter what happened, we throw an IOException to avoid changing the interfaces....
128             throw new IOException("Failed to construct the AudioClip: " + e3);
129         }
130     }
131 
132 
133     public synchronized void play() {
134 
135                 if (audioClip != null)
136                         audioClip.play();
137     }
138 
139 
140     public synchronized void loop() {
141 
142                 if (audioClip != null)
143                         audioClip.loop();
144     }
145 
146     public synchronized void stop() {
147 
148                 if (audioClip != null)
149                         audioClip.stop();
150     }
151 }