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 @SuppressWarnings("deprecation") 45 public class AppletAudioClip implements AudioClip { 46 47 // url that this AudioClip is based on 48 private URL url = null; 49 50 // the audio clip implementation 51 private AudioClip audioClip = null; 52 53 boolean DEBUG = false /*true*/; 54 55 /** 56 * Constructs an AppletAudioClip from an URL. 57 */ 58 public AppletAudioClip(URL url) { 59 60 // store the url 61 this.url = url; 62 63 try { 64 // create a stream from the url, and use it 65 // in the clip. 66 InputStream in = url.openStream(); 67 createAppletAudioClip(in); 68 69 } catch (IOException e) { 70 /* just quell it */ 71 if (DEBUG) { 72 System.err.println("IOException creating AppletAudioClip" + e); 73 } 74 } 75 } 76 77 /** 78 * Constructs an AppletAudioClip from a URLConnection. 79 */ 80 public AppletAudioClip(URLConnection uc) { 81 82 try { 83 // create a stream from the url, and use it 84 // in the clip. 85 createAppletAudioClip(uc.getInputStream()); 86 87 } catch (IOException e) { 88 /* just quell it */ 89 if (DEBUG) { 90 System.err.println("IOException creating AppletAudioClip" + e); 91 } 92 } 93 } 94 95 96 /** 97 * For constructing directly from Jar entries, or any other 98 * raw Audio data. Note that the data provided must include the format 99 * header. 100 */ 101 public AppletAudioClip(byte [] data) { 102 103 try { 104 105 // construct a stream from the byte array 106 InputStream in = new ByteArrayInputStream(data); 107 108 createAppletAudioClip(in); 109 110 } catch (IOException e) { 111 /* just quell it */ 112 if (DEBUG) { 113 System.err.println("IOException creating AppletAudioClip " + e); 114 } 115 } 116 } 117 118 119 /* 120 * Does the real work of creating an AppletAudioClip from an InputStream. 121 * This function is used by both constructors. 122 */ 123 void createAppletAudioClip(InputStream in) throws IOException { 124 125 try { 126 audioClip = new JavaSoundAudioClip(in); 127 } catch (Exception e3) { 128 // no matter what happened, we throw an IOException to avoid changing the interfaces.... 129 throw new IOException("Failed to construct the AudioClip: " + e3); 130 } 131 } 132 133 134 public synchronized void play() { 135 136 if (audioClip != null) 137 audioClip.play(); 138 } 139 140 141 public synchronized void loop() { 142 143 if (audioClip != null) 144 audioClip.loop(); 145 } 146 147 public synchronized void stop() { 148 149 if (audioClip != null) 150 audioClip.stop(); 151 } 152 }