1. The space forums use a separate registration system from the main virtual world. To comment on these forums, you need to create a separate forum account.
    Dismiss Notice
  2. Hey there, we have two Skype groups for space early users - if you're creating content, or need help, feel free to join in either the the beginners or advanced users groups.
    Dismiss Notice

Quick Example for Music Stream URL changer

Discussion in 'HOWTOs' started by ZareShadows, Jul 4, 2017.

  1. ZareShadows

    ZareShadows Administrator Staff Member Moderator

    Here is a quick example LUA code on how to change a stream URL via inWorld in space. Feel free to modify use anyway you like. I have also attached unity package with UI.
    Code:
    --------------------------------------------------------------
    -- Music URL changer using Adam's Network sharding functions.
    -- Modified by ZareShadows for Stream URL changer.
    -- This function gets called when user clicks on Play.
    -- Do remember to add callURL() on the button press event.
    --------------------------------------------------------------
    
    function callURL()
        local urlif = Space.Host.GetReference("URLBar");
        local Stream = Space.Host.GetReference("JukeBox");
        local url = urlif.UIInputField.Text;
        Space.Network.SetShardProperty("StreamURL", url);
        Space.Network.SendNetworkMessage("URLBox", {uri = url});
        IsPlaying()
        Stream.Radio.PlayMP3Stream(url) -- This will set the URL to whatever was input using UIInputField  
        Stream.Radio.Play()
    end
    ------------------------------------------------
    -- If the music is playing change to Stop button
    ------------------------------------------------
    function IsPlaying()
        local bPlay = Space.Host.GetReference("Play");
        local bStop = Space.Host.GetReference("Stop");
        bPlay.Active = false;
        bStop.Active = true;
    end
    -------------------------------------------------------------------
    -- This is called when Stream URL is changed to update all clients.
    -------------------------------------------------------------------
    function onURLUpdate(url)
        local urlif = Space.Host.GetReference("URLBar");
        local url = Space.Network.GetShardProperty("StreamURL");
        local Stream = Space.Host.GetReference("JukeBox");
        urlif.UIInputField.Text = url;
        IsPlaying()
        Stream.Radio.PlayMP3Stream(url)
        Stream.Radio.Play()
    end
    --------------------------------------------------------------------
    -- Listening to when URL was Changed.
    --------------------------------------------------------------------
    Space.Network.SubscribeToNetwork("URLBox", onURLUpdate);
    
    --------------------------------------------------------------------
    -- Try and load URL if user joined after the music started playing.
    --------------------------------------------------------------------
    local urlif2 = Space.Host.GetReference("URLBar");
    local url2 = Space.Network.GetShardProperty("StreamURL");
    local Stream2 = Space.Host.GetReference("JukeBox");
    
    if url2 != nil then
        urlif2.UIInputField.Text = url2;
        IsPlaying()
        Stream2.Radio.PlayMP3Stream(url2)
        Stream2.Radio.Play()
    end
    -----------------------------------------------------------------------------------------
    -- This is called when user presses on STOP Stream button to stop music for all  clients.
    -----------------------------------------------------------------------------------------
    function callStop()
        local Stream = Space.Host.GetReference("JukeBox");
        local bPlay = Space.Host.GetReference("Play");
        local bStop = Space.Host.GetReference("Stop");
        Stream.Radio.Stop()
        Space.Network.SendNetworkMessage("StStream", {stop = "stop"});
        bPlay.Active = true;
        bStop.Active = false;
    end
    --------------------------------------------------------------------------------------
    -- Listening for other client calling the stop function and stopping stream.
    --------------------------------------------------------------------------------------
    function onStop()
        local Stream = Space.Host.GetReference("JukeBox");
        local bPlay = Space.Host.GetReference("Play");
        local bStop = Space.Host.GetReference("Stop");
        Stream.Radio.Stop()
        bPlay.Active = true;
        bStop.Active = false;
    end
    Space.Network.SubscribeToNetwork("StStream", onStop);
    ---------------------------------------------------------------------------------------------------
    -- End of the code. Feel free to Modify improve use it anyway you like.
    -- Hope you enjoy coding in space and do let us know for any bugs or feedbacks on forums.sine.space
    ---------------------------------------------------------------------------------------------------
    Known bugs,
    1. Sharding doesn't seem to work.
    2. iSplaying is not being called properly via network messages; seems like an API bug still testing.
     

    Attached Files:

    AshaSekayi likes this.
  2. ZareShadows

    ZareShadows Administrator Staff Member Moderator

    Just a note here. The Stream.Radio.Stop() doesn't seem to work properly. Till its fixed it needs to be used like this . Thanks to Tyler for the work around.
    Code:
    --- To stop Stream -- 
    radio.Stop();
    radio.PlayMP3Stream("");
    radio.Play();
    
    -- and to play stream -- 
    radio.Stop();
    radio.PlayMP3Stream(url);
    radio.Play();
       
    
    
     

Share This Page