[Tutorial] Adding custom sounds to your plugin (well… sort of)

Discussion in 'Resources' started by Quantum64, Mar 26, 2014.

Thread Status:
Not open for further replies.
  1. [​IMG]
    Yes, I have installed a JDK.
     
  2. Offline

    Quantum64

  3. Quantum64 : That problem is fixed, but still lots of errors. And somehow it stopped working my possibility to push to a private GitLab. Is there no possible way to use this without Maven installed? Like add the dependencies manually?
     
  4. Hmm, everything works for me, i can build with maven, the server starts, i can access the webpage however it doesn't play sounds when i call the methods in the class 'SoundEffectsManager'. I get no errors either. I am also using the play.ogg file you provided.

    EDIT: I do get an error, when I open the webpage I get this message:
    (These are not when I call the play methods, this is when I open the webpage)

    http://gyazo.com/4750bc7ecd7e54886696c4401907bd0e
    Then this one:
    http://gyazo.com/ce9c33b1aa72d4cc9475fcbe96d03133
     
  5. Offline

    Quantum64

    That is because of an unsuccessful connection to Websocket. Be sure you did WebsocketServer.start();

    No, the dependencies would never go into the final Jar that way.
     
  6. I meant that I can't push the a private GitLab server (the repository) since I've installed Maven.
     
  7. Quantum64 I fixed it now and it's working. Serveral questions:
    * Can I send the player who connected to the Webserver send a message? (Succesfully connected to the Webserver!)
    * org.java_websocket.WebSocketImpl@4e35a37a has disconnected from the AudioServer!
    Is this correct? As it doesn't show the player's name.


    EDIT addition:
    * Is there a way to:
    ¤ Stop/pauze a playing music file
    ¤ Go to a specific second of a music file
    Both are in the audio library if I saw it correctly, but I don't know how to implement.
     
  8. Offline

    Quantum64

    rcth
    Yes you can! Simply change
    Code:
     @Override
        public void onMessage(WebSocket conn, String message) {
            Bukkit.getLogger().info("Recieve Websocket packet - " + conn + ":" + message);
            if (message.split(":")[0].equalsIgnoreCase("name")) {
                WebsocketSessionManager.getSessionManager().addSessionUsername(conn.getRemoteSocketAddress().getAddress().getHostAddress(), message.split(":")[1]);
            }
        }
    
    to
    Code:
     @Override
        public void onMessage(WebSocket conn, String message) {
            Bukkit.getLogger().info("Recieve Websocket packet - " + conn + ":" + message);
            if (message.split(":")[0].equalsIgnoreCase("name")) {
                WebsocketSessionManager.getSessionManager().addSessionUsername(conn.getRemoteSocketAddress().getAddress().getHostAddress(), message.split(":")[1]);
            Bukkit.getPlayer(message.split(":")[1]).sendMessage("Your message here");
            }
        }
    
    To stop/skip in an audio file, implement more messages, such as a stop message, and check for it it in Websocket.js BEFORE the audio file is played. (So you would create a class with howler and on receiving the stop message you would use audio.stop();) All howler features such as skipping are documented here, and you can trigger all of them with custom messages. https://github.com/goldfire/howler.js/
     
  9. Quantum64 So, something like this in Websocket.js needs to be changed:
    PHP:
    ws.onmessage = function (evt) {
        var 
    sound = new Howl({
            
    urls: ['sounds/' evt.data '.ogg']
        }).
    play();
    };
    Or something else? I don't see where it checks the message in that file through. (Sorry, I'm not the best with JS)
     
  10. Offline

    Quantum64

    rcth
    if(evt.data == 'something') {
    //Do something here
    }
     
  11. Quantum64
    So, first check all possibilities I want with evt.data (pauze, play, stop, sync) and if non of these are sent, play the filename? Seems good. Thanks.
     
  12. Offline

    Quantum64

  13. Offline

    jordanzilla02

    Quantum64 You must've spent forever on this.
     
    Quantum64 likes this.
  14. Quantum64 Thanks. One thing that came to my mind, for the sync command (to go to a specific second of a sound), I need a second argument (the seconds, so it's like /audio sync PLAYER AUDIOFILE SECONDS), possible or not? Or does it need a lot of changing?
     
  15. Offline

    Quantum64

    rcth
    I would take the args and combine them into something like "arg1:arg2", send the message as one string, then in the Javascript split along the ":" and profit!
     
  16. Quantum64 Seems like a good idea, thanks.
     
  17. Offline

    jf11

    Hello Quantum64 ,

    I have a question i will be add a command to stop the music from a player i have now this
    Code:java
    1. public void stopData(WebsocketSession session){
    2. Collection<WebSocket> con = connections();
    3. synchronized (con) {
    4. for (WebSocket c : con)
    5. if (c.getRemoteSocketAddress().getAddress().getHostAddress().equalsIgnoreCase(session.getHost())) {
    6. Bukkit.getLogger().info("Stopped send package ");
    7. c.close();


    But it close the connection how do you fix that the music stop?
     
  18. Offline

    Quantum64

    jf11 I gave directions on how to do this a few posts up.
     
  19. Offline

    Quantum64

    CMG Looks like you tried to play a sound to a player that wasn't connected to the soundserver.
     
  20. I was connected though, it told me i was connected in the web browser
     
  21. Offline

    Quantum64

  22. I did, i used exactly the same but my session id is random, and my name is obviously my in game name, i think ive found the problem though when i connect via local host or my local ip, it prints that my name is null in the console. No idea why.

    Quantum64 Nevermind, got it working eventually; i just messed with each class and added != null checks and remade the java script and web-socket files. Thanks so much for your help anyway :D

    EDIT: One last question though, how come i can connect via localhost, but i cant connect via my actual external ip address, (the servers ip address) it just doesnt work.

    EDIT: Forget that also, just needed to portforward the ports (8887 and 8080)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 7, 2015
  23. Offline

    Quantum64

    Strange, as your session's username was null. SessionId was something I was going to implement to prevent people from listening to other people's sounds by typing the link, so if the link's session ID didn't match the server's it would reject the connection, but I figured this tutorial was complicated enough as is.
     
  24. Offline

    Mickerd

    How will i make a stop command? Can anyone give a Example?
     
  25. Offline

    Quantum64

    Checkout post 40
     
  26. Offline

    indyetoile

    Quantum64
    Thank you for this great tutorial. However, I don't seem to figure out how I'm able to stop the sounds, or end the session. I checked out post 40 already, but still. Could you please be a bit more detailed about this?

    And, how does the Hive automatically detect the player, without needing to put the player name in the URL?

    Thank you.
     
  27. Offline

    Quantum64

    As you can see by the hive's script:
    http://paste.ubuntu.com/7799702/
    (found here http://hivemc.com/media/js/blockparty.main.new.js)
    They use a custom json server to track which server the player is in, (I am assuming) by the IP address. If you would like to try and code that go for it.

    Now change the Javascript file to the following:

    Make sure you declare your sound class wide so you can access it later, so add this at the top of the file:
    PHP:
    var sound null;
    Replace
    PHP:
        var sound = new Howl({
            
    urls: ['sounds/' evt.data '.ogg']
        }).
    play();
    with
    PHP:
        sound = new Howl({
            
    urls: ['sounds/' evt.data '.ogg']
        }).
    play();
    Add this to your onMessage in the js file:
    PHP:
    if (evt.data == "stop") {
        if(
    sound != null) {
            
    sound.stop();
        }
    }
    Then uses this to stop the sound:
    PHP:
    WebsocketServer.s.sendData(WebsocketSessionManager.getSessionManager().getSessionByName(p.getName()), "stop");
    To end a session use the endSessionByName(String playerName) in your session manager.

    I am slightly disappointed in myself for just giving you the code, and you for not trying to do it yourself.
     
    indyetoile likes this.
Thread Status:
Not open for further replies.

Share This Page