sound/src/main/java/net/java/html/sound/AudioClip.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 740 f34b06e2d139
permissions -rw-r--r--
Updating copyright headers to mention current year
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;
toni@109
    46
import org.apidesign.html.sound.spi.AudioEnvironment;
toni@107
    47
jaroslav@223
    48
/** Handle to an audio clip which can be {@link #play() played}, {@link #pause() paused}
jaroslav@223
    49
 * and etc. Obtain new instance via {@link #create(java.lang.String) create} factory 
jaroslav@223
    50
 * method and then use it when necessary.
toni@107
    51
 *
toni@107
    52
 * @author antonepple
toni@107
    53
 */
jaroslav@223
    54
public abstract class AudioClip {
jaroslav@223
    55
    private AudioClip() {
toni@107
    56
    }
toni@107
    57
jaroslav@223
    58
    /** Creates new instance of an audio clip based on the provided URL.
jaroslav@248
    59
     * If no suitable audio environment provider is found, the method 
jaroslav@248
    60
     * returns a dummy instance that does nothing and only returns
jaroslav@248
    61
     * false from its {@link #isSupported()} method.
jaroslav@223
    62
     * 
jaroslav@223
    63
     * @param src the URL where to find the audio clip
jaroslav@223
    64
     * @return the audio clip handle
jaroslav@223
    65
     * @throws NullPointerException if src is <code>null</code>
jaroslav@223
    66
     */
toni@108
    67
    public static AudioClip create(String src) {
jaroslav@223
    68
        src.getClass();
jaroslav@223
    69
        for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
jaroslav@223
    70
            Impl handle = create(ae, src);
jaroslav@223
    71
            if (handle != null) {
jaroslav@223
    72
                return handle;
jaroslav@223
    73
            }
jaroslav@223
    74
        }
jaroslav@248
    75
        return DummyClip.INSTANCE;
toni@107
    76
    }
jaroslav@223
    77
    
jaroslav@223
    78
    /** Plays the clip from begining to the end.
jaroslav@223
    79
     */
jaroslav@223
    80
    public abstract void play();
toni@108
    81
jaroslav@223
    82
    /** Pauses playback of the clip
jaroslav@223
    83
     */
jaroslav@223
    84
    public abstract void pause();
jaroslav@223
    85
jaroslav@223
    86
    /**
jaroslav@223
    87
     * Specifies the volume of the audio. Must be a number between 0.0 and 1.0:
jaroslav@223
    88
     * <ul>
jaroslav@223
    89
     *   <li>1.0 - highest volume</li>
jaroslav@223
    90
     *   <li>0.5 - 50% volume</li>
jaroslav@223
    91
     *   <li>0.0 - silent</li>
jaroslav@223
    92
     * </ul>
jaroslav@223
    93
     * 
jaroslav@223
    94
     * @param volume for the playback
jaroslav@223
    95
     */
jaroslav@223
    96
    public abstract void setVolume(double volume);
jaroslav@248
    97
    
jaroslav@248
    98
    /** Check whether the audio clip is supported and can be played.
jaroslav@248
    99
     * @return true if it is likely that after calling {@link #play()} 
jaroslav@248
   100
     *   a sound will be produced
jaroslav@248
   101
     */
jaroslav@248
   102
    public abstract boolean isSupported();
jaroslav@223
   103
jaroslav@223
   104
//    public abstract void playFrom(int seconds);
jaroslav@223
   105
jaroslav@223
   106
    //
jaroslav@223
   107
    // Implementation
jaroslav@223
   108
    //
jaroslav@223
   109
    
jaroslav@223
   110
    private static <Audio> Impl<Audio> create(AudioEnvironment<Audio> env, String src) {
jaroslav@223
   111
        Audio a = env.create(src);
jaroslav@223
   112
        if (a != null) {
jaroslav@223
   113
            return new Impl<Audio>(env, src, a);
jaroslav@223
   114
        } else {
jaroslav@223
   115
            return null;
jaroslav@223
   116
        }
toni@107
   117
    }
jaroslav@223
   118
    
jaroslav@223
   119
    private static final class Impl<Audio> extends AudioClip {
jaroslav@223
   120
        private final String src;
jaroslav@223
   121
        private final Audio clip;
jaroslav@223
   122
        private final AudioEnvironment<Audio> env;
toni@107
   123
jaroslav@223
   124
        public Impl(AudioEnvironment<Audio> env, String src, Audio clip) {
jaroslav@223
   125
            this.clip = clip;
jaroslav@223
   126
            this.env = env;
jaroslav@223
   127
            this.src = src;
jaroslav@223
   128
        }
toni@107
   129
jaroslav@223
   130
        @Override
jaroslav@223
   131
        public void play() {
jaroslav@223
   132
            env.play(clip);
jaroslav@223
   133
        }
toni@108
   134
jaroslav@223
   135
        @Override
jaroslav@223
   136
        public void pause() {
jaroslav@223
   137
            env.pause(clip);
jaroslav@223
   138
        }
toni@108
   139
jaroslav@223
   140
        @Override
jaroslav@223
   141
        public void setVolume(double volume) {
jaroslav@223
   142
            env.setVolume(clip, volume);
jaroslav@223
   143
        }
toni@107
   144
jaroslav@223
   145
        @Override
jaroslav@248
   146
        public boolean isSupported() {
jaroslav@248
   147
            return env.isSupported(clip);
jaroslav@248
   148
        }
jaroslav@248
   149
jaroslav@248
   150
        @Override
jaroslav@223
   151
        public int hashCode() {
jaroslav@223
   152
            return 59 * src.hashCode();
jaroslav@223
   153
        }
toni@107
   154
jaroslav@223
   155
        @Override
jaroslav@223
   156
        public boolean equals(Object obj) {
jaroslav@223
   157
            if (obj instanceof Impl) {
jaroslav@223
   158
                return src.equals(((Impl)obj).src);
jaroslav@223
   159
            }
toni@107
   160
            return false;
toni@107
   161
        }
jaroslav@223
   162
    } // end of Impl
jaroslav@248
   163
    
jaroslav@248
   164
    private static final class DummyClip extends AudioClip {
jaroslav@248
   165
        static AudioClip INSTANCE = new DummyClip();
jaroslav@248
   166
        
jaroslav@248
   167
        @Override
jaroslav@248
   168
        public void play() {
jaroslav@248
   169
        }
jaroslav@248
   170
jaroslav@248
   171
        @Override
jaroslav@248
   172
        public void pause() {
jaroslav@248
   173
        }
jaroslav@248
   174
jaroslav@248
   175
        @Override
jaroslav@248
   176
        public void setVolume(double volume) {
jaroslav@248
   177
        }
jaroslav@248
   178
jaroslav@248
   179
        @Override
jaroslav@248
   180
        public boolean isSupported() {
jaroslav@248
   181
            return false;
jaroslav@248
   182
        }
jaroslav@248
   183
    } // end of DummyClip
toni@107
   184
}