sound/src/main/java/net/java/html/sound/AudioClip.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 Aug 2013 15:29:42 +0200
branchsound
changeset 223 ef2399c0fb9e
parent 109 0b12d1c32314
child 248 cc466a4b6f58
permissions -rw-r--r--
Implementation of the AudioClip that uses HTML5 Audio tag
toni@107
     1
/**
jaroslav@223
     2
 * HTML via Java(tm) Language Bindings
jaroslav@223
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
toni@107
     4
 *
jaroslav@223
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@223
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@223
     7
 * the Free Software Foundation, version 2 of the License.
toni@107
     8
 *
jaroslav@223
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@223
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@223
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@223
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@223
    13
 * designates this particular file as subject to the
jaroslav@223
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@223
    15
 * in the License file that accompanied this code.
toni@107
    16
 *
jaroslav@223
    17
 * You should have received a copy of the GNU General Public License
jaroslav@223
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@223
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
toni@107
    20
 */
toni@109
    21
package net.java.html.sound;
toni@107
    22
toni@107
    23
import java.util.ServiceLoader;
toni@109
    24
import org.apidesign.html.sound.spi.AudioEnvironment;
toni@107
    25
jaroslav@223
    26
/** Handle to an audio clip which can be {@link #play() played}, {@link #pause() paused}
jaroslav@223
    27
 * and etc. Obtain new instance via {@link #create(java.lang.String) create} factory 
jaroslav@223
    28
 * method and then use it when necessary.
toni@107
    29
 *
toni@107
    30
 * @author antonepple
toni@107
    31
 */
jaroslav@223
    32
public abstract class AudioClip {
jaroslav@223
    33
    private AudioClip() {
toni@107
    34
    }
toni@107
    35
jaroslav@223
    36
    /** Creates new instance of an audio clip based on the provided URL.
jaroslav@223
    37
     * 
jaroslav@223
    38
     * @param src the URL where to find the audio clip
jaroslav@223
    39
     * @return the audio clip handle
jaroslav@223
    40
     * @throws NullPointerException if src is <code>null</code>
jaroslav@223
    41
     */
toni@108
    42
    public static AudioClip create(String src) {
jaroslav@223
    43
        src.getClass();
jaroslav@223
    44
        for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
jaroslav@223
    45
            Impl handle = create(ae, src);
jaroslav@223
    46
            if (handle != null) {
jaroslav@223
    47
                return handle;
jaroslav@223
    48
            }
jaroslav@223
    49
        }
jaroslav@223
    50
        throw new IllegalStateException();
toni@107
    51
    }
jaroslav@223
    52
    
jaroslav@223
    53
    /** Plays the clip from begining to the end.
jaroslav@223
    54
     */
jaroslav@223
    55
    public abstract void play();
toni@108
    56
jaroslav@223
    57
    /** Pauses playback of the clip
jaroslav@223
    58
     */
jaroslav@223
    59
    public abstract void pause();
jaroslav@223
    60
jaroslav@223
    61
    /**
jaroslav@223
    62
     * Specifies the volume of the audio. Must be a number between 0.0 and 1.0:
jaroslav@223
    63
     * <ul>
jaroslav@223
    64
     *   <li>1.0 - highest volume</li>
jaroslav@223
    65
     *   <li>0.5 - 50% volume</li>
jaroslav@223
    66
     *   <li>0.0 - silent</li>
jaroslav@223
    67
     * </ul>
jaroslav@223
    68
     * 
jaroslav@223
    69
     * @param volume for the playback
jaroslav@223
    70
     */
jaroslav@223
    71
    public abstract void setVolume(double volume);
jaroslav@223
    72
jaroslav@223
    73
//    public abstract void playFrom(int seconds);
jaroslav@223
    74
jaroslav@223
    75
    //
jaroslav@223
    76
    // Implementation
jaroslav@223
    77
    //
jaroslav@223
    78
    
jaroslav@223
    79
    private static <Audio> Impl<Audio> create(AudioEnvironment<Audio> env, String src) {
jaroslav@223
    80
        Audio a = env.create(src);
jaroslav@223
    81
        if (a != null) {
jaroslav@223
    82
            return new Impl<Audio>(env, src, a);
jaroslav@223
    83
        } else {
jaroslav@223
    84
            return null;
jaroslav@223
    85
        }
toni@107
    86
    }
jaroslav@223
    87
    
jaroslav@223
    88
    private static final class Impl<Audio> extends AudioClip {
jaroslav@223
    89
        private final String src;
jaroslav@223
    90
        private final Audio clip;
jaroslav@223
    91
        private final AudioEnvironment<Audio> env;
toni@107
    92
jaroslav@223
    93
        public Impl(AudioEnvironment<Audio> env, String src, Audio clip) {
jaroslav@223
    94
            this.clip = clip;
jaroslav@223
    95
            this.env = env;
jaroslav@223
    96
            this.src = src;
jaroslav@223
    97
        }
toni@107
    98
jaroslav@223
    99
        @Override
jaroslav@223
   100
        public void play() {
jaroslav@223
   101
            env.play(clip);
jaroslav@223
   102
        }
toni@108
   103
jaroslav@223
   104
        @Override
jaroslav@223
   105
        public void pause() {
jaroslav@223
   106
            env.pause(clip);
jaroslav@223
   107
        }
toni@108
   108
jaroslav@223
   109
        @Override
jaroslav@223
   110
        public void setVolume(double volume) {
jaroslav@223
   111
            env.setVolume(clip, volume);
jaroslav@223
   112
        }
toni@107
   113
jaroslav@223
   114
        @Override
jaroslav@223
   115
        public int hashCode() {
jaroslav@223
   116
            return 59 * src.hashCode();
jaroslav@223
   117
        }
toni@107
   118
jaroslav@223
   119
        @Override
jaroslav@223
   120
        public boolean equals(Object obj) {
jaroslav@223
   121
            if (obj instanceof Impl) {
jaroslav@223
   122
                return src.equals(((Impl)obj).src);
jaroslav@223
   123
            }
toni@107
   124
            return false;
toni@107
   125
        }
jaroslav@223
   126
    } // end of Impl
toni@107
   127
}