Advertisement
sriharshachilakapati

GWT-AL example

Jan 8th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package com.shc.gwtal.examples.client;
  2.  
  3. import com.google.gwt.core.client.EntryPoint;
  4. import com.google.gwt.core.client.GWT;
  5. import com.google.gwt.xhr.client.XMLHttpRequest;
  6.  
  7. import com.shc.gwtal.client.AudioBufferSourceNode;
  8. import com.shc.gwtal.client.AudioContext;
  9. import com.shc.gwtal.client.AudioContextException;
  10.  
  11. public class Main implements EntryPoint
  12. {
  13.     @Override
  14.     public void onModuleLoad()
  15.     {
  16.         try
  17.         {
  18.             // Create an audio context
  19.             AudioContext context = AudioContext.create();
  20.            
  21.             String url = "https://upload.wikimedia.org/wikipedia/commons/1/1d/Demo_chorus.ogg";
  22.  
  23.             // Create an XMLHttpRequest to load the file from URL
  24.             XMLHttpRequest request = XMLHttpRequest.create();
  25.             request.setResponseType(XMLHttpRequest.ResponseType.ArrayBuffer);
  26.  
  27.             request.open("GET", url);
  28.            
  29.             // Attach a state change listener to check for the loading of the file
  30.             request.setOnReadyStateChange((r) ->
  31.             {
  32.                 if (request.getReadyState() == XMLHttpRequest.DONE)
  33.                 {
  34.                     // Start decoding the data into an AudioBuffer
  35.                     context.decodeAudioData
  36.                     (
  37.                         // The response that we got from the request
  38.                         request.getResponseArrayBuffer(),
  39.                            
  40.                         // Upon success, we are returned with an AudioBuffer
  41.                         (buffer) ->
  42.                         {
  43.                             // Create the source, set the buffer, connect to the destination and play
  44.                             AudioBufferSourceNode source = context.createBufferSource();
  45.                             source.setBuffer(buffer);
  46.  
  47.                             source.connect(context.getDestination());
  48.                             source.start(0);
  49.                         }
  50.                     );
  51.                 }
  52.             });
  53.  
  54.             // Send the request so browser starts loading the file
  55.             request.send();
  56.         }
  57.         catch (AudioContextException e)
  58.         {
  59.             // In case of any exception, log to the console
  60.             GWT.log(e.toString());
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement