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.
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package net.java.html.sound;
    44 
    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;
    50 
    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.
    54  *
    55  * @author antonepple
    56  */
    57 public abstract class AudioClip {
    58     private AudioClip() {
    59     }
    60 
    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.
    65      * <p>
    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.
    69      * 
    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>
    73      */
    74     public static AudioClip create(String src) {
    75         src.getClass();
    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);
    80             if (handle != null) {
    81                 return handle;
    82             }
    83         }
    84         for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
    85             Impl handle = create(ae, src);
    86             if (handle != null) {
    87                 return handle;
    88             }
    89         }
    90         Impl handle = create(BrowserAudioEnv.DEFAULT, src);
    91         return handle != null ? handle : DummyClip.INSTANCE;
    92     }
    93     
    94     /** Plays the clip from begining to the end.
    95      */
    96     public abstract void play();
    97 
    98     /** Pauses playback of the clip
    99      */
   100     public abstract void pause();
   101 
   102     /**
   103      * Specifies the volume of the audio. Must be a number between 0.0 and 1.0:
   104      * <ul>
   105      *   <li>1.0 - highest volume</li>
   106      *   <li>0.5 - 50% volume</li>
   107      *   <li>0.0 - silent</li>
   108      * </ul>
   109      * 
   110      * @param volume for the playback
   111      */
   112     public abstract void setVolume(double volume);
   113     
   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
   117      */
   118     public abstract boolean isSupported();
   119 
   120 //    public abstract void playFrom(int seconds);
   121 
   122     //
   123     // Implementation
   124     //
   125     
   126     private static <Audio> Impl<Audio> create(AudioEnvironment<Audio> env, String src) {
   127         Audio a = env.create(src);
   128         if (a != null) {
   129             return new Impl<Audio>(env, src, a);
   130         } else {
   131             return null;
   132         }
   133     }
   134     
   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;
   139 
   140         public Impl(AudioEnvironment<Audio> env, String src, Audio clip) {
   141             this.clip = clip;
   142             this.env = env;
   143             this.src = src;
   144         }
   145 
   146         @Override
   147         public void play() {
   148             env.play(clip);
   149         }
   150 
   151         @Override
   152         public void pause() {
   153             env.pause(clip);
   154         }
   155 
   156         @Override
   157         public void setVolume(double volume) {
   158             env.setVolume(clip, volume);
   159         }
   160 
   161         @Override
   162         public boolean isSupported() {
   163             return env.isSupported(clip);
   164         }
   165 
   166         @Override
   167         public int hashCode() {
   168             return 59 * src.hashCode();
   169         }
   170 
   171         @Override
   172         public boolean equals(Object obj) {
   173             if (obj instanceof Impl) {
   174                 return src.equals(((Impl)obj).src);
   175             }
   176             return false;
   177         }
   178     } // end of Impl
   179     
   180     private static final class DummyClip extends AudioClip {
   181         static AudioClip INSTANCE = new DummyClip();
   182         
   183         @Override
   184         public void play() {
   185         }
   186 
   187         @Override
   188         public void pause() {
   189         }
   190 
   191         @Override
   192         public void setVolume(double volume) {
   193         }
   194 
   195         @Override
   196         public boolean isSupported() {
   197             return false;
   198         }
   199     } // end of DummyClip
   200 }