Example usage for com.google.gwt.media.client Audio createIfSupported

List of usage examples for com.google.gwt.media.client Audio createIfSupported

Introduction

In this page you can find the example usage for com.google.gwt.media.client Audio createIfSupported.

Prototype

public static Audio createIfSupported() 

Source Link

Document

Return a new Audio if supported, and null otherwise.

Usage

From source file:com.allen_sauer.gwt.voices.client.Html5Sound.java

License:Apache License

/**
 * @param mimeType the requested MIME type and optional codec according to RFC 4281
 * @return the level of support for the provided MIME type
 *//*from w  ww  .j a  va2  s.  c o  m*/
public static MimeTypeSupport getMimeTypeSupport(String mimeType) {
    if (!Audio.isSupported()) {
        return MimeTypeSupport.MIME_TYPE_NOT_SUPPORTED;
    }
    String support = Audio.createIfSupported().getAudioElement().canPlayType(mimeType);
    assert support != null;
    if (AudioElement.CAN_PLAY_PROBABLY.equals(support)) {
        return MimeTypeSupport.MIME_TYPE_SUPPORT_READY;
    }
    if (AudioElement.CAN_PLAY_MAYBE.equals(support)) {
        return MimeTypeSupport.MIME_TYPE_SUPPORT_READY;
    }
    return MimeTypeSupport.MIME_TYPE_SUPPORT_UNKNOWN;
}

From source file:com.allen_sauer.gwt.voices.client.Html5Sound.java

License:Apache License

private void createAudioElement() {
    if (endedRegistration != null) {
        endedRegistration.removeHandler();
    }/*  www  .  j a  va2 s. co  m*/
    if (audio != null) {
        // TODO: remove, once DOM attachment no longer required to sink (bitless) events
        audio.removeFromParent();
    }
    assert Audio.isSupported();
    audio = Audio.createIfSupported();
    assert audio != null;
    AudioElement elem = audio.getAudioElement();
    assert elem != null;

    endedRegistration = audio.addEndedHandler(endedHandler);

    // TODO: remove, once DOM attachment no longer required to sink (bitless) events
    RootPanel.get().add(audio);

    if (isCrossOrigin()) {
        elem.setAttribute("crossOrigin", "anonymous");
    }
    elem.setSrc(getUrl());
}

From source file:com.google.gwt.sample.mobilewebapp.client.ui.SoundEffects.java

License:Apache License

/**
 * Prefetch the error sound.//from w  w w .ja v  a2  s.  c  o m
 */
public void prefetchError() {
    if (isSupported && error == null) {
        error = Audio.createIfSupported();
        error.addSource("audio/error.ogg", AudioElement.TYPE_OGG);
        error.addSource("audio/error.mp3", AudioElement.TYPE_MP3);
        error.addSource("audio/error.wav", AudioElement.TYPE_WAV);
        prefetchAudio(error);
    }
}

From source file:com.googlecode.konamigwt.hadoken.client.Hadoken.java

License:BEER-WARE LICENSE

/**
 * This is the entry point method./*from  w  w w . jav  a 2 s  .  c o  m*/
 */
public void onModuleLoad() {

    final Button sendButton = new Button("Send");
    final TextBox nameField = new TextBox();
    nameField.setText("GWT User");
    final Label errorLabel = new Label();

    // We can add style names to widgets
    sendButton.addStyleName("sendButton");

    // Add the nameField and sendButton to the RootPanel
    // Use RootPanel.get() to get the entire body element
    RootPanel.get("nameFieldContainer").add(nameField);
    RootPanel.get("sendButtonContainer").add(sendButton);
    RootPanel.get("errorLabelContainer").add(errorLabel);

    // Focus the cursor on the name field when the app loads
    nameField.setFocus(true);
    nameField.selectAll();

    // Create the popup dialog box
    final DialogBox dialogBox = new DialogBox();
    dialogBox.setText("Remote Procedure Call");
    dialogBox.setAnimationEnabled(true);
    final Button closeButton = new Button("Close");
    // We can set the id of a widget by accessing its Element
    closeButton.getElement().setId("closeButton");
    final Label textToServerLabel = new Label();
    final HTML serverResponseLabel = new HTML();
    VerticalPanel dialogVPanel = new VerticalPanel();
    dialogVPanel.addStyleName("dialogVPanel");
    dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
    dialogVPanel.add(textToServerLabel);
    dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
    dialogVPanel.add(serverResponseLabel);
    dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
    dialogVPanel.add(closeButton);
    dialogBox.setWidget(dialogVPanel);

    // Add a handler to close the DialogBox
    closeButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {

            dialogBox.hide();
            sendButton.setEnabled(true);
            sendButton.setFocus(true);
        }
    });

    // Quick and Dirty implementation
    new Konami(new KonamiHandler() {
        @Override
        public void onKonamiCodePerformed() {
            final Image image = new Image("media/ryu.gif");
            DOM.appendChild(RootPanel.get().getElement(), image.getElement());

            final Audio audio = Audio.createIfSupported();
            if (audio != null) {
                audio.setSrc("media/hadoken.ogg");
                DOM.appendChild(RootPanel.get().getElement(), audio.getElement());
                audio.play();
            }
            Timer timer = new Timer() {

                @Override
                public void run() {
                    DOM.removeChild(RootPanel.get().getElement(), image.getElement());
                    if (audio != null) {
                        DOM.removeChild(RootPanel.get().getElement(), audio.getElement());
                    }
                }

            };
            timer.schedule(1100);

        }
    }).start();

    // Create a handler for the sendButton and nameField
    class MyHandler implements ClickHandler, KeyUpHandler {
        /**
         * Fired when the user clicks on the sendButton.
         */
        public void onClick(ClickEvent event) {
            sendNameToServer();
        }

        /**
         * Fired when the user types in the nameField.
         */
        public void onKeyUp(KeyUpEvent event) {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                sendNameToServer();
            }
        }

        /**
         * Send the name from the nameField to the server and wait for a response.
         */
        private void sendNameToServer() {
            // First, we validate the input.
            errorLabel.setText("");
            String textToServer = nameField.getText();
            if (!FieldVerifier.isValidName(textToServer)) {
                errorLabel.setText("Please enter at least four characters");
                return;
            }

            // Then, we send the input to the server.
            sendButton.setEnabled(false);
            textToServerLabel.setText(textToServer);
            serverResponseLabel.setText("");
            serverResponseLabel.removeStyleName("serverResponseLabelError");
            serverResponseLabel.setHTML("Hello " + textToServer);
            dialogBox.center();
            closeButton.setFocus(true);
        }
    }

    // Add a handler to send the name to the server
    MyHandler handler = new MyHandler();
    sendButton.addClickHandler(handler);
    nameField.addKeyUpHandler(handler);
}

From source file:com.webwoz.client.client.WebWOZClient.java

License:Apache License

private void startSession() {

    // define whether client logout should be used or not
    // useClientLogout = true;

    // initialize variables
    processing = "Processing";

    // panels to structure layout
    headPanel = new VerticalPanel();
    titlePanel = new HorizontalPanel();
    contentPanel = new VerticalPanel();
    footPanel = new VerticalPanel();
    loginPanel = new HorizontalPanel();
    asrPanel = new VerticalPanel();
    textPanel = new VerticalPanel();
    audioPanel = new VerticalPanel();
    mmPanel = new VerticalPanel();
    txtInPanel = new HorizontalPanel();

    // labels// ww  w .j  a  v a  2s  .  c om
    userLabel = new Label("User: ");
    pwLabel = new Label("Password: ");
    loginMessage = new Label();

    // text boxes
    userTextBox = new TextBox();
    pwTextBox = new PasswordTextBox();

    // text areas
    textInputTextArea = new TextArea();

    // buttons
    loginButton = new Button("login");
    logoutButton = new Button("logout");
    sendButton = new Button("Send");

    // RPC
    databaseAccessSvc = GWT.create(DatabaseAccess.class);

    currentID = 0;

    audioHTML = Audio.createIfSupported();
    videoHTML = Video.createIfSupported();

    // head
    String header = "<div id='header'></div>";
    headPanel.clear();
    headPanel.add(new HTML(header));

    // title
    String title = "<div id='title'></div>";
    titlePanel.clear();
    titlePanel.add(new HTML(title));

    // build login
    loginPanel.clear();
    loginPanel.add(userLabel);
    loginMessage.setStyleName("loginMessage");
    userLabel.setStyleName("label");
    loginPanel.add(userTextBox);
    userTextBox.setStyleName("text");
    loginPanel.add(pwLabel);
    pwLabel.setStyleName("label");
    loginPanel.add(pwTextBox);
    pwTextBox.setStyleName("text");
    loginPanel.add(loginButton);
    loginButton.setStyleName("buttonLogin");
    // define button but do not add it
    logoutButton.setStyleName("buttonLogin");
    logoutButton.setVisible(false);

    // loginPanel.setStyleName("layout");
    loginPanel.setVisible(true);

    // output panels
    asrPanel.clear();
    asrPanel.setStyleName("asr");
    txtInPanel.clear();
    txtInPanel.setStyleName("txtIn");
    textPanel.clear();
    textPanel.setStyleName("txtOut");
    audioPanel.clear();
    audioPanel.setStyleName("audioOut");
    mmPanel.clear();
    mmPanel.setStyleName("mmOut");

    // content
    contentPanel.clear();
    contentPanel.setStyleName("content");
    contentPanel.add(loginPanel);
    contentPanel.add(loginMessage);
    contentPanel.add(txtInPanel);
    contentPanel.add(asrPanel);
    contentPanel.add(textPanel);
    contentPanel.add(audioPanel);
    contentPanel.add(mmPanel);

    // foot
    String foot = "<div id='foot'></div>";
    footPanel.clear();
    footPanel.add(new HTML(foot));

    // other panels to change the layout can be added if needed
    // headPanel.setStyleName("layout");
    // titlePanel.setStyleName("layout");
    // contentPanel.setStyleName("layout");
    // footPanel.setStyleName("layout");

    if (login != null) {
        if (login.equals("1")) {
            stopReload();
            loggedIn();
        } else {
            loadLoginScreen();
        }
    } else {
        loadLoginScreen();
    }

    // RootPanel.get().clear();
    RootPanel.get("head").add(headPanel);
    RootPanel.get("title").add(titlePanel);
    RootPanel.get("content").add(contentPanel);
    RootPanel.get("foot").add(footPanel);

    // handler
    // login click
    loginClickHandler = new ClickHandler() {
        public void onClick(ClickEvent event) {
            login();
        }
    };

    if (loginClickHandlerRegistration != null) {
        loginClickHandlerRegistration.removeHandler();
    }

    loginClickHandlerRegistration = loginButton.addClickHandler(loginClickHandler);

    // login keypress
    loginUserKeyPressHandler = new KeyPressHandler() {
        public void onKeyPress(KeyPressEvent event) {
            if (event.getCharCode() == KeyCodes.KEY_ENTER) {
                login();
            }
        }

    };
    if (loginUserKeyPressHandlerRegistration != null) {
        loginUserKeyPressHandlerRegistration.removeHandler();
    }
    loginUserKeyPressHandlerRegistration = userTextBox.addKeyPressHandler(loginUserKeyPressHandler);

    loginPWKeyPressHandler = new KeyPressHandler() {
        public void onKeyPress(KeyPressEvent event) {
            if (event.getCharCode() == KeyCodes.KEY_ENTER) {
                login();
            }
        }

    };
    if (loginPWKeyPressHandlerRegistration != null) {
        loginPWKeyPressHandlerRegistration.removeHandler();
    }
    loginPWKeyPressHandlerRegistration = pwTextBox.addKeyPressHandler(loginPWKeyPressHandler);

    // logout click
    logoutClickHandler = new ClickHandler() {
        public void onClick(ClickEvent event) {
            logout();
        }
    };
    if (logoutClickHandlerRegistration != null) {
        logoutClickHandlerRegistration.removeHandler();
    }
    logoutClickHandlerRegistration = logoutButton.addClickHandler(logoutClickHandler);

    // send click
    sendClickHandler = new ClickHandler() {
        public void onClick(ClickEvent event) {
            reload = false;
            getTimeStamp(textInputTextArea.getText());
            textInputTextArea.setText("");
        }
    };
    if (sendClickHandlerRegistration != null) {
        sendClickHandlerRegistration.removeHandler();
    }
    sendClickHandlerRegistration = sendButton.addClickHandler(sendClickHandler);

    // send keypress
    sendKeyPressHandler = new KeyPressHandler() {
        public void onKeyPress(KeyPressEvent event) {
            if (event.getCharCode() == KeyCodes.KEY_ENTER) {
                reload = false;
                getTimeStamp(textInputTextArea.getText());
                textInputTextArea.setText("");
            }
        }
    };
    if (sendKeyPressHandlerRegistration != null) {
        sendKeyPressHandlerRegistration.removeHandler();
    }
    sendKeyPressHandlerRegistration = textInputTextArea.addKeyPressHandler(sendKeyPressHandler);

    refreshTimer = new Timer() {
        @Override
        public void run() {
            if (reload) {
                getData();
            }
        }
    };

    // run refresh
    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);

}

From source file:es.deusto.weblab.client.experiments.logic.ui.LogicExperiment.java

License:Open Source License

private void processCommandSent(ResponseCommand responseCommand) {
    if (this.lastCommand instanceof GetCircuitCommand) {
        this.messages.stop();
        this.messages.setText("");
        final CircuitParser circuitParser = new CircuitParser();
        try {//w w  w . ja v a  2 s.c om
            this.circuit = circuitParser.parseCircuit(responseCommand.getCommandString());
        } catch (final InvalidCircuitException e) {
            this.messages.setText("Invalid Circuit received: " + e.getMessage());
            return;
        }
        this.sendSolutionButton.setEnabled(false);
        this.updateCircuitGrid();
    } else if (this.lastCommand instanceof SolveCircuitCommand) {
        this.messages.stop();

        if (responseCommand.getCommandString().startsWith("FAIL")) {
            this.solving = false;

            if (Audio.isSupported()) {
                @SuppressWarnings("unused")
                final Audio audio = Audio.createIfSupported();

            }

            AudioManager.getInstance().playBest("snd/wrong");

            this.messages.setText(i18n.wrongOneGameOver(this.points));
            this.sendSolutionButton.setEnabled(false);
        } else if (responseCommand.getCommandString().startsWith("OK")) {
            this.points++;
            turnOnLight();
            this.messages.setText(i18n.wellDone1point());

            AudioManager.getInstance().playBest("snd/applause");

            final Timer sleepTimer = new Timer() {

                @Override
                public void run() {
                    LogicExperiment.this.sendCommand(new GetCircuitCommand());
                    turnOffLight();
                }

            };
            sleepTimer.schedule(2000);
        }

    } else {
        // TODO: Unknown command!
    }
}

From source file:es.deusto.weblab.client.ui.audio.AudioManager.java

License:Open Source License

/**
 * Helper method which will play a sound if certain conditions are met:
 * Sound is enabled.//from   w  w  w.  j ava2  s  . c  o  m
 * Sound is supported.
 * File type is supported.
 * If they are not, calling this method will have no effect.
 * @param file File to play. The path should be relative to the module base URL. E.g. "/audio/foo.wav"
 * 
 * @return AudioElement being played, or null. May be used to modify the default behaviour, such
 * as enabling loop mode.
 */
public AudioElement play(String file) {
    if (this.getSoundEnabled()) {
        final Audio audio = Audio.createIfSupported();
        if (audio != null) {
            final AudioElement elem = audio.getAudioElement();
            elem.setSrc(GWT.getModuleBaseURL() + file);
            elem.play();
            return elem;
        }
    }
    return null;
}

From source file:es.deusto.weblab.client.ui.audio.AudioManager.java

License:Open Source License

/**
 * Helper method which will play a sound if certain conditions are met:
 * Sound is enabled./*  ww w  .  j  a  v  a  2  s  .  com*/
 * Sound is supported.
 * If they are not, calling this method will have no effect.
 * 
 * The file type is inferred by the web browser. If it supports OGG, it will use the ".ogg" extension. If it doesn't, but it
 * supports MP3, it will use ".mp3". It will also try WAV, and otherwise it will not work. Therefore, the audio files must be 
 * duplicated in these three formats to work. 
 * 
 * @param file File to play. The path should be relative to the module base URL. E.g. "/audio/foo" will select foo.ogg, foo.mp3 or foo.wav
 * 
 * @return AudioElement being played, or null. May be used to modify the default behaviour, such
 * as enabling loop mode.
 */
public AudioElement playBest(String file) {
    if (this.getSoundEnabled()) {
        final Audio audio = Audio.createIfSupported();
        if (audio != null) {
            final AudioElement elem = audio.getAudioElement();

            // First try probably
            if (elem.canPlayType(OGG_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".ogg");
            else if (elem.canPlayType(MP3_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".mp3");
            else if (elem.canPlayType(WAV_TYPE).equals(MediaElement.CAN_PLAY_PROBABLY))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".wav");
            // Then maybe
            else if (elem.canPlayType(OGG_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".ogg");
            else if (elem.canPlayType(MP3_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".mp3");
            else if (elem.canPlayType(WAV_TYPE).equals(MediaElement.CAN_PLAY_MAYBE))
                elem.setSrc(GWT.getModuleBaseURL() + file + ".wav");
            // Then fail
            else
                return null;

            elem.play();
            return elem;
        }
    }
    return null;
}

From source file:es.deusto.weblab.client.ui.widgets.WlTimer.java

License:Open Source License

protected void decrement() {
    if (this.time > 0) {
        --this.time;
        this.updateTimeString();
    }/* w w w.  ja  v a 2 s. c  om*/

    if (AudioManager.getInstance().getSoundEnabled()) {
        Audio timeRunningOutAudio = Audio.createIfSupported();
        if (this.time == this.timeRunningOutAudioLimit && AudioManager.getInstance() != null) {
            System.out.println(this.getParent());
            final AudioElement effect = timeRunningOutAudio.getAudioElement();
            effect.setSrc(GWT.getModuleBaseURL() + "snd/clock.wav");
            effect.setLoop(true);
            effect.play();
        } else if (this.time == 0) {
            final AudioElement effect = timeRunningOutAudio.getAudioElement();
            effect.setLoop(false);
            effect.pause();

            // TODO: Make sure that this isn't a memory leak. There does not seem to be any method that provides a 
            // "destroy" rather than "pause". Make also sure that there is no case under which a timer can not reach zero.
            // Otherwise, the sound might play forever, which is significantly annoying.
        }
    }

}

From source file:nl.mpi.tg.eg.experiment.client.service.AudioPlayer.java

License:Open Source License

private void createPlayer() throws AudioException {
    audioPlayer = Audio.createIfSupported();
    if (audioPlayer == null) {
        throw new AudioException("audio not supportered");
    }/*from  w  w w .j a  v a2s  . co  m*/
    final AudioElement audioElement = audioPlayer.getAudioElement();
    onEndedSetup(audioElement);
}