Directory

← Back

SoundPlayer

Vaadin component for playing mp3 sounds.

Author

Rating

Popularity

100+

With SoundPlayer component you can play sounds in the users browser. Sounds must be in MP3 format. Component API features: load, stop, play, pause and a listener callback interface for player state changes. SoundPlayer is based on gwt-sound and SoundManager2 client-side implementations and requires Flash 8 plugin or newer to be installed. Also install gwt-sound.jar to your project.

Sample code

// Play a resource file
SoundPlayer player = new SoundPlayer();
mainWindow.addComponent(player);
player.play(new ClassResource("intro.mp3", this));
// Play a publicly available MP3
SoundPlayer player = new SoundPlayer();
mainWindow.addComponent(player);
player.play("http://www.a1sounddownload.com/freesounds5/alarmring.mp3");
package org.vaadin.soundplayer;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.ClassResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class SoundPlayerSampleApplication extends Application {

    private static final List<String[]> SONGS = new ArrayList<String[]>();
    static {
        SONGS
                .add(new String[] {
                        "1. Beastie Boys / Now Get Busy",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Beastie%20Boys%20-%20Now%20Get%20Busy.mp3" });
        SONGS
                .add(new String[] {
                        "2. David Byrne / My Fair Lady",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/David%20Byrne%20-%20My%20Fair%20Lady.mp3" });
        SONGS
                .add(new String[] {
                        "3. Zap Mama / Wadidyusay?",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Zap%20Mama%20-%20Wadidyusay.mp3" });
        SONGS
                .add(new String[] {
                        "4. My Morning Jacket / One Big Holiday",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/My%20Morning%20Jacket%20-%20One%20Big%20Holiday.mp3" });
        SONGS
                .add(new String[] {
                        "5. Spoon / Revenge!",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Spoon%20-%20Revenge!.mp3" });

        // TODO: Add all from http://creativecommons.org/wired/
        SONGS
                .add(new String[] {
                        "6. Gilberto Gil / Oslodum",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Gilberto%20Gil%20-%20Oslodum.mp3" });
        SONGS
                .add(new String[] {
                        "7. Dan the Automator / Relaxation Spa Treatment",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Dan%20The%20Automator%20-%20Relaxation%20Spa%20Treatment.mp3" });
        SONGS
                .add(new String[] {
                        "8. Thievery Corporation / DC 3000",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Thievery%20Corporation%20-%20Dc%203000.mp3" });
        SONGS
                .add(new String[] {
                        "9. Le Tigre / Fake French",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Le%20Tigre%20-%20Fake%20French.mp3" });
        SONGS
                .add(new String[] {
                        "10. Paul Westerberg / Looking Up in Heaven",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Paul%20Westerberg%20-%20Looking%20Up%20In%20Heaven.mp3" });
        SONGS
                .add(new String[] {
                        "11. Chuck D with Fine Arts Militia / No Meaning No",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Chuck%20D%20-%20No%20Meaning%20No%20(feat.%20Fine%20Arts%20Militia).mp3" });
        SONGS
                .add(new String[] {
                        "12. The Rapture / Sister Saviour (Blackstrobe Remix)",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/The%20Rapture%20-%20Sister%20Saviour%20(Blackstrobe%20Remix).mp3" });
        SONGS
                .add(new String[] {
                        "13. Cornelius / Wataridori 2",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Cornelius%20-%20Wataridori%202.mp3" });
        SONGS
                .add(new String[] {
                        "14. Danger Mouse & Jemini / What U Sittin' On? (starring Cee Lo and Tha Alkaholiks)",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/DJ%20Danger%20Mouse%20-%20What%20U%20Sittin%27%20On%20(feat.%20Jemini,%20Cee%20Lo%20And%20Tha%20Alkaholiks).mp3" });
        SONGS
                .add(new String[] {
                        "15. DJ Dolores / Oslodum 2004 (includes (cc) sample of 'Oslodum' by Gilberto Gil)",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/DJ%20Dolores%20-%20Oslodum%202004.mp3" });
        SONGS
                .add(new String[] {
                        "16. Matmos / Action at a Distance",
                        "http://mirrors.creativecommons.org/ccmixter/contrib/Wired/Matmos%20-%20Action%20At%20A%20Distance.mp3" });

    }

    private Button play;
    private SoundPlayer player;
    private Button stop;
    private Label title;
    private ListSelect songs;
    private Button pause;

    @Override
    public void init() {

        // Window
        Window w = new Window("Soundplayer Sample");
        ((VerticalLayout) w.getContent()).setSpacing(true);
        addWindow(w);

        // Player
        player = new SoundPlayer();
        player.addListener(new SoundPlayer.StateChangeListener() {

            public void playerStateUpdated(SoundPlayer player) {
                if (player.getSound() != null && songs.getValue() == null) {
                    // Intro playing
                    title.setValue(player.getSound().substring(
                            player.getSound().lastIndexOf('/') + 1));
                } else {
                    // Set the title of song
                    title.setValue(songs.getItemCaption(player.getSound()));
                }
                play.setEnabled(!player.isPlaying());
                pause.setEnabled(player.isPlaying());
                stop.setEnabled(player.isPlaying() || player.isPaused());
            }
        });
        w.addComponent(player);

        // Song list
        songs = new ListSelect("The WIRED CD: Rip. Sample. Mash. Share.");
        w.addComponent(songs);
        songs.setNullSelectionAllowed(false);
        songs.setImmediate(true);
        songs.addListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                if (songs.getValue() != null) {
                    player.play((String) songs.getValue());
                }
            }
        });
        addSongs();

        // Buttons in horizontal order
        HorizontalLayout btns = new HorizontalLayout();
        btns.setSpacing(true);
        w.addComponent(btns);

        // Play button
        play = new Button("play", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if (player.getSound() != null) {
                    player.play();
                }
            }
        });
        btns.addComponent(play);

        // pause button
        pause = new Button("pause", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if (player.getSound() != null) {
                    player.pause();
                }
            }
        });
        btns.addComponent(pause);

        // Stop button
        stop = new Button("stop", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                player.stop();
            }
        });
        btns.addComponent(stop);

        // Title label
        title = new Label("n/a");
        title.setCaption("Currently Playing:");
        w.addComponent(title);

        // Info
        Label info = new Label(
                "<p><i>Music by WIRED magazine.<br /> More info at <a href=\"http://creativecommons.org/wired/\">http://creativecommons.org/wired/</a></i></p>",
                Label.CONTENT_RAW);
        w.addComponent(info);

        // Play the intro sound
        player.play(new ClassResource("intro.mp3", this));
    }

    private void addSongs() {
        songs.removeAllItems();
        for (String[] song : SONGS) {
            songs.addItem(song[1]);
            songs.setItemC

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Released
2010-01-22
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.2+
Browser
Firefox
Safari

SoundPlayer - Vaadin Add-on Directory

Vaadin component for playing mp3 sounds. SoundPlayer - Vaadin Add-on Directory
With SoundPlayer component you can play sounds in the users browser. Sounds must be in MP3 format. Component API features: load, stop, play, pause and a listener callback interface for player state changes. SoundPlayer is based on gwt-sound and SoundManager2 client-side implementations and requires Flash 8 plugin or newer to be installed. Also install gwt-sound.jar to your project.
Discussion Forum
SoundManager2 Home
Online Demo
Download gwt-sound.jar

SoundPlayer version 1.1.1
null

Online