Can find AudioEnvironment in BrwsrCtx
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 21 Jul 2014 08:13:23 +0200
changeset 740f34b06e2d139
parent 739 7a2f7066fc67
child 741 d0f85050eb03
Can find AudioEnvironment in BrwsrCtx
sound/src/main/java/net/java/html/sound/AudioClip.java
sound/src/main/java/org/apidesign/html/sound/spi/AudioEnvironment.java
sound/src/main/java/org/netbeans/html/sound/impl/BrowserAudioEnv.java
src/main/javadoc/overview.html
     1.1 --- a/sound/src/main/java/net/java/html/sound/AudioClip.java	Fri Jul 18 17:02:21 2014 +0200
     1.2 +++ b/sound/src/main/java/net/java/html/sound/AudioClip.java	Mon Jul 21 08:13:23 2014 +0200
     1.3 @@ -43,7 +43,10 @@
     1.4  package net.java.html.sound;
     1.5  
     1.6  import java.util.ServiceLoader;
     1.7 +import net.java.html.BrwsrCtx;
     1.8 +import org.apidesign.html.context.spi.Contexts;
     1.9  import org.apidesign.html.sound.spi.AudioEnvironment;
    1.10 +import org.netbeans.html.sound.impl.BrowserAudioEnv;
    1.11  
    1.12  /** Handle to an audio clip which can be {@link #play() played}, {@link #pause() paused}
    1.13   * and etc. Obtain new instance via {@link #create(java.lang.String) create} factory 
    1.14 @@ -59,6 +62,10 @@
    1.15       * If no suitable audio environment provider is found, the method 
    1.16       * returns a dummy instance that does nothing and only returns
    1.17       * false from its {@link #isSupported()} method.
    1.18 +     * <p>
    1.19 +     * The <code>src</code> can be absolute URL or it can be relative
    1.20 +     * to current {@link BrwsrCtx browser context} - e.g. usually to the
    1.21 +     * page that is just being displayed.
    1.22       * 
    1.23       * @param src the URL where to find the audio clip
    1.24       * @return the audio clip handle
    1.25 @@ -66,13 +73,22 @@
    1.26       */
    1.27      public static AudioClip create(String src) {
    1.28          src.getClass();
    1.29 +        BrwsrCtx brwsrCtx = BrwsrCtx.findDefault(AudioClip.class);
    1.30 +        AudioEnvironment brwsrAE = Contexts.find(brwsrCtx, AudioEnvironment.class);
    1.31 +        if (brwsrAE != null) {
    1.32 +            Impl handle = create(brwsrAE, src);
    1.33 +            if (handle != null) {
    1.34 +                return handle;
    1.35 +            }
    1.36 +        }
    1.37          for (AudioEnvironment<?> ae : ServiceLoader.load(AudioEnvironment.class)) {
    1.38              Impl handle = create(ae, src);
    1.39              if (handle != null) {
    1.40                  return handle;
    1.41              }
    1.42          }
    1.43 -        return DummyClip.INSTANCE;
    1.44 +        Impl handle = create(BrowserAudioEnv.DEFAULT, src);
    1.45 +        return handle != null ? handle : DummyClip.INSTANCE;
    1.46      }
    1.47      
    1.48      /** Plays the clip from begining to the end.
     2.1 --- a/sound/src/main/java/org/apidesign/html/sound/spi/AudioEnvironment.java	Fri Jul 18 17:02:21 2014 +0200
     2.2 +++ b/sound/src/main/java/org/apidesign/html/sound/spi/AudioEnvironment.java	Mon Jul 21 08:13:23 2014 +0200
     2.3 @@ -42,9 +42,15 @@
     2.4   */
     2.5  package org.apidesign.html.sound.spi;
     2.6  
     2.7 +import net.java.html.BrwsrCtx;
     2.8 +import org.apidesign.html.context.spi.Contexts;
     2.9 +
    2.10  /** Basic interface for sound playback providers. Register your implementation
    2.11   * in a way {@link java.util.ServiceLoader} can find it - e.g. use
    2.12 - * {@link org.openide.util.lookup.ServiceProvider} annotation.
    2.13 + * {@link org.openide.util.lookup.ServiceProvider} annotation. Possibly
    2.14 + * one can register the provider into {@link Contexts#newBuilder()}, in
    2.15 + * case the implementation is somehow associated 
    2.16 + * with the actual {@link BrwsrCtx} (works since version 0.8.3).
    2.17   *
    2.18   * @author antonepple
    2.19   * @param <Audio> custom type representing the internal audio state
     3.1 --- a/sound/src/main/java/org/netbeans/html/sound/impl/BrowserAudioEnv.java	Fri Jul 18 17:02:21 2014 +0200
     3.2 +++ b/sound/src/main/java/org/netbeans/html/sound/impl/BrowserAudioEnv.java	Mon Jul 21 08:13:23 2014 +0200
     3.3 @@ -44,14 +44,18 @@
     3.4  
     3.5  import net.java.html.js.JavaScriptBody;
     3.6  import org.apidesign.html.sound.spi.AudioEnvironment;
     3.7 -import org.openide.util.lookup.ServiceProvider;
     3.8  
     3.9 -/** Registers an audio provider that delegates to HTML5 Audio tag.
    3.10 +/** The default audio provider that delegates to HTML5 Audio tag
    3.11 + * it is used if no other {@link AudioEnvironment} is found.
    3.12   *
    3.13   * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.14   */
    3.15 -@ServiceProvider(service = AudioEnvironment.class, position = 100)
    3.16  public final class BrowserAudioEnv implements AudioEnvironment<Object> {
    3.17 +    public static final AudioEnvironment<?> DEFAULT = new BrowserAudioEnv();
    3.18 +    
    3.19 +    private BrowserAudioEnv() {
    3.20 +    }
    3.21 +    
    3.22      @Override
    3.23      @JavaScriptBody(args = { "src" }, body = ""
    3.24          + "if (!Audio) return null;"
     4.1 --- a/src/main/javadoc/overview.html	Fri Jul 18 17:02:21 2014 +0200
     4.2 +++ b/src/main/javadoc/overview.html	Mon Jul 21 08:13:23 2014 +0200
     4.3 @@ -79,7 +79,8 @@
     4.4          
     4.5          <p>
     4.6              Setters or array properties on classes generated by {@link net.java.html.json.Model}
     4.7 -            annotation can be accessed from any thread. 
     4.8 +            annotation can be accessed from any thread. {@link org.apidesign.html.sound.spi.AudioEnvironment}
     4.9 +            can be registered into {@link net.java.html.BrwsrCtx}.
    4.10          </p>
    4.11          
    4.12          <h3>What's New in Version 0.8.2?</h3>