During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.sound;
45 import java.util.ServiceLoader;
46 import net.java.html.BrwsrCtx;
47 import org.netbeans.html.context.spi.Contexts;
48 import org.netbeans.html.sound.spi.AudioEnvironment;
49 import org.netbeans.html.sound.impl.BrowserAudioEnv;
51 /** Handle to an audio clip which can be {@link #play() played}, {@link #pause() paused}
52 * and etc. Obtain new instance via {@link #create(java.lang.String) create} factory
53 * method and then use it when necessary.
57 public abstract class AudioClip {
61 /** Creates new instance of an audio clip based on the provided URL.
62 * If no suitable audio environment provider is found, the method
63 * returns a dummy instance that does nothing and only returns
64 * false from its {@link #isSupported()} method.
66 * The <code>src</code> can be absolute URL or it can be relative
67 * to current {@link BrwsrCtx browser context} - e.g. usually to the
68 * page that is just being displayed.
70 * @param src the URL where to find the audio clip
71 * @return the audio clip handle
72 * @throws NullPointerException if src is <code>null</code>
74 public static AudioClip create(String src) {
76 BrwsrCtx brwsrCtx = BrwsrCtx.findDefault(AudioClip.class);
77 AudioEnvironment brwsrAE = Contexts.find(brwsrCtx, AudioEnvironment.class);
78 if (brwsrAE != null) {
79 Impl handle = create(brwsrAE, src);
84 for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
85 Impl handle = create(ae, src);
90 Impl handle = create(BrowserAudioEnv.DEFAULT, src);
91 return handle != null ? handle : DummyClip.INSTANCE;
94 /** Plays the clip from begining to the end.
96 public abstract void play();
98 /** Pauses playback of the clip
100 public abstract void pause();
103 * Specifies the volume of the audio. Must be a number between 0.0 and 1.0:
105 * <li>1.0 - highest volume</li>
106 * <li>0.5 - 50% volume</li>
107 * <li>0.0 - silent</li>
110 * @param volume for the playback
112 public abstract void setVolume(double volume);
114 /** Check whether the audio clip is supported and can be played.
115 * @return true if it is likely that after calling {@link #play()}
116 * a sound will be produced
118 public abstract boolean isSupported();
120 // public abstract void playFrom(int seconds);
126 private static <Audio> Impl<Audio> create(AudioEnvironment<Audio> env, String src) {
127 Audio a = env.create(src);
129 return new Impl<Audio>(env, src, a);
135 private static final class Impl<Audio> extends AudioClip {
136 private final String src;
137 private final Audio clip;
138 private final AudioEnvironment<Audio> env;
140 public Impl(AudioEnvironment<Audio> env, String src, Audio clip) {
152 public void pause() {
157 public void setVolume(double volume) {
158 env.setVolume(clip, volume);
162 public boolean isSupported() {
163 return env.isSupported(clip);
167 public int hashCode() {
168 return 59 * src.hashCode();
172 public boolean equals(Object obj) {
173 if (obj instanceof Impl) {
174 return src.equals(((Impl)obj).src);
180 private static final class DummyClip extends AudioClip {
181 static AudioClip INSTANCE = new DummyClip();
188 public void pause() {
192 public void setVolume(double volume) {
196 public boolean isSupported() {
199 } // end of DummyClip