001: /*
002: * @(#)AppletAudioClip.java 1.23 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: /* AppletAudioClip from Amber (PersonalJava 3.1) */
029:
030: package sun.applet;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.net.URL;
035: import java.net.URLConnection;
036: import java.applet.AudioClip;
037: import sun.audio.*;
038:
039: /**
040: * Applet audio clip;
041: *
042: * @version 1.12, 07/01/98
043: * @author Arthur van Hoff
044: */
045: public class AppletAudioClip implements AudioClip {
046: URL url;
047: AudioData data;
048: InputStream stream;
049:
050: /**
051: * This should really fork a seperate thread to
052: * load the data.
053: */
054: public AppletAudioClip(URL url) {
055: InputStream in = null;
056: try {
057: try {
058: URLConnection uc = url.openConnection();
059: uc.setAllowUserInteraction(true);
060: in = uc.getInputStream();
061: data = new AudioStream(in).getData();
062: } finally {
063: if (in != null) {
064: in.close();
065: }
066: }
067: } catch (IOException e) {
068: data = null;
069: }
070: }
071:
072: /*
073: * For constructing directly from Jar entries. Or any other
074: * raw Audio data.
075: */
076:
077: public AppletAudioClip(byte[] data) {
078: this .data = new AudioData(data);
079: }
080:
081: public synchronized void play() {
082: stop();
083: if (data != null) {
084: java.awt.Toolkit.getDefaultToolkit();
085: stream = new AudioDataStream(data);
086: AudioPlayer.player.start(stream);
087: }
088: }
089:
090: public synchronized void loop() {
091: stop();
092: if (data != null) {
093: stream = new ContinuousAudioDataStream(data);
094: AudioPlayer.player.start(stream);
095: }
096: }
097:
098: public synchronized void stop() {
099: if (stream != null) {
100: AudioPlayer.player.stop(stream);
101: try {
102: stream.close();
103: } catch (IOException e) {
104: }
105: }
106: }
107:
108: public String toString() {
109: return getClass().toString() + "[" + url + "]";
110: }
111: }
|