sound/src/main/java/net/java/html/sound/AudioClip.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 740 f34b06e2d139
permissions -rw-r--r--
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.
toni@107
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
toni@107
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
toni@107
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
toni@107
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
toni@107
    42
 */
toni@109
    43
package net.java.html.sound;
toni@107
    44
toni@107
    45
import java.util.ServiceLoader;
jtulach@740
    46
import net.java.html.BrwsrCtx;
jtulach@838
    47
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    48
import org.netbeans.html.sound.spi.AudioEnvironment;
jtulach@740
    49
import org.netbeans.html.sound.impl.BrowserAudioEnv;
toni@107
    50
jaroslav@223
    51
/** Handle to an audio clip which can be {@link #play() played}, {@link #pause() paused}
jaroslav@223
    52
 * and etc. Obtain new instance via {@link #create(java.lang.String) create} factory 
jaroslav@223
    53
 * method and then use it when necessary.
toni@107
    54
 *
toni@107
    55
 * @author antonepple
toni@107
    56
 */
jaroslav@223
    57
public abstract class AudioClip {
jaroslav@223
    58
    private AudioClip() {
toni@107
    59
    }
toni@107
    60
jaroslav@223
    61
    /** Creates new instance of an audio clip based on the provided URL.
jaroslav@248
    62
     * If no suitable audio environment provider is found, the method 
jaroslav@248
    63
     * returns a dummy instance that does nothing and only returns
jaroslav@248
    64
     * false from its {@link #isSupported()} method.
jtulach@740
    65
     * <p>
jtulach@740
    66
     * The <code>src</code> can be absolute URL or it can be relative
jtulach@740
    67
     * to current {@link BrwsrCtx browser context} - e.g. usually to the
jtulach@740
    68
     * page that is just being displayed.
jaroslav@223
    69
     * 
jaroslav@223
    70
     * @param src the URL where to find the audio clip
jaroslav@223
    71
     * @return the audio clip handle
jaroslav@223
    72
     * @throws NullPointerException if src is <code>null</code>
jaroslav@223
    73
     */
toni@108
    74
    public static AudioClip create(String src) {
jaroslav@223
    75
        src.getClass();
jtulach@740
    76
        BrwsrCtx brwsrCtx = BrwsrCtx.findDefault(AudioClip.class);
jtulach@740
    77
        AudioEnvironment brwsrAE = Contexts.find(brwsrCtx, AudioEnvironment.class);
jtulach@740
    78
        if (brwsrAE != null) {
jtulach@740
    79
            Impl handle = create(brwsrAE, src);
jtulach@740
    80
            if (handle != null) {
jtulach@740
    81
                return handle;
jtulach@740
    82
            }
jtulach@740
    83
        }
jaroslav@223
    84
        for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
jaroslav@223
    85
            Impl handle = create(ae, src);
jaroslav@223
    86
            if (handle != null) {
jaroslav@223
    87
                return handle;
jaroslav@223
    88
            }
jaroslav@223
    89
        }
jtulach@740
    90
        Impl handle = create(BrowserAudioEnv.DEFAULT, src);
jtulach@740
    91
        return handle != null ? handle : DummyClip.INSTANCE;
toni@107
    92
    }
jaroslav@223
    93
    
jaroslav@223
    94
    /** Plays the clip from begining to the end.
jaroslav@223
    95
     */
jaroslav@223
    96
    public abstract void play();
toni@108
    97
jaroslav@223
    98
    /** Pauses playback of the clip
jaroslav@223
    99
     */
jaroslav@223
   100
    public abstract void pause();
jaroslav@223
   101
jaroslav@223
   102
    /**
jaroslav@223
   103
     * Specifies the volume of the audio. Must be a number between 0.0 and 1.0:
jaroslav@223
   104
     * <ul>
jaroslav@223
   105
     *   <li>1.0 - highest volume</li>
jaroslav@223
   106
     *   <li>0.5 - 50% volume</li>
jaroslav@223
   107
     *   <li>0.0 - silent</li>
jaroslav@223
   108
     * </ul>
jaroslav@223
   109
     * 
jaroslav@223
   110
     * @param volume for the playback
jaroslav@223
   111
     */
jaroslav@223
   112
    public abstract void setVolume(double volume);
jaroslav@248
   113
    
jaroslav@248
   114
    /** Check whether the audio clip is supported and can be played.
jaroslav@248
   115
     * @return true if it is likely that after calling {@link #play()} 
jaroslav@248
   116
     *   a sound will be produced
jaroslav@248
   117
     */
jaroslav@248
   118
    public abstract boolean isSupported();
jaroslav@223
   119
jaroslav@223
   120
//    public abstract void playFrom(int seconds);
jaroslav@223
   121
jaroslav@223
   122
    //
jaroslav@223
   123
    // Implementation
jaroslav@223
   124
    //
jaroslav@223
   125
    
jaroslav@223
   126
    private static <Audio> Impl<Audio> create(AudioEnvironment<Audio> env, String src) {
jaroslav@223
   127
        Audio a = env.create(src);
jaroslav@223
   128
        if (a != null) {
jaroslav@223
   129
            return new Impl<Audio>(env, src, a);
jaroslav@223
   130
        } else {
jaroslav@223
   131
            return null;
jaroslav@223
   132
        }
toni@107
   133
    }
jaroslav@223
   134
    
jaroslav@223
   135
    private static final class Impl<Audio> extends AudioClip {
jaroslav@223
   136
        private final String src;
jaroslav@223
   137
        private final Audio clip;
jaroslav@223
   138
        private final AudioEnvironment<Audio> env;
toni@107
   139
jaroslav@223
   140
        public Impl(AudioEnvironment<Audio> env, String src, Audio clip) {
jaroslav@223
   141
            this.clip = clip;
jaroslav@223
   142
            this.env = env;
jaroslav@223
   143
            this.src = src;
jaroslav@223
   144
        }
toni@107
   145
jaroslav@223
   146
        @Override
jaroslav@223
   147
        public void play() {
jaroslav@223
   148
            env.play(clip);
jaroslav@223
   149
        }
toni@108
   150
jaroslav@223
   151
        @Override
jaroslav@223
   152
        public void pause() {
jaroslav@223
   153
            env.pause(clip);
jaroslav@223
   154
        }
toni@108
   155
jaroslav@223
   156
        @Override
jaroslav@223
   157
        public void setVolume(double volume) {
jaroslav@223
   158
            env.setVolume(clip, volume);
jaroslav@223
   159
        }
toni@107
   160
jaroslav@223
   161
        @Override
jaroslav@248
   162
        public boolean isSupported() {
jaroslav@248
   163
            return env.isSupported(clip);
jaroslav@248
   164
        }
jaroslav@248
   165
jaroslav@248
   166
        @Override
jaroslav@223
   167
        public int hashCode() {
jaroslav@223
   168
            return 59 * src.hashCode();
jaroslav@223
   169
        }
toni@107
   170
jaroslav@223
   171
        @Override
jaroslav@223
   172
        public boolean equals(Object obj) {
jaroslav@223
   173
            if (obj instanceof Impl) {
jaroslav@223
   174
                return src.equals(((Impl)obj).src);
jaroslav@223
   175
            }
toni@107
   176
            return false;
toni@107
   177
        }
jaroslav@223
   178
    } // end of Impl
jaroslav@248
   179
    
jaroslav@248
   180
    private static final class DummyClip extends AudioClip {
jaroslav@248
   181
        static AudioClip INSTANCE = new DummyClip();
jaroslav@248
   182
        
jaroslav@248
   183
        @Override
jaroslav@248
   184
        public void play() {
jaroslav@248
   185
        }
jaroslav@248
   186
jaroslav@248
   187
        @Override
jaroslav@248
   188
        public void pause() {
jaroslav@248
   189
        }
jaroslav@248
   190
jaroslav@248
   191
        @Override
jaroslav@248
   192
        public void setVolume(double volume) {
jaroslav@248
   193
        }
jaroslav@248
   194
jaroslav@248
   195
        @Override
jaroslav@248
   196
        public boolean isSupported() {
jaroslav@248
   197
            return false;
jaroslav@248
   198
        }
jaroslav@248
   199
    } // end of DummyClip
toni@107
   200
}