Jump to content

[HELP] sound functions


Recommended Posts

So I have this piece of code, when the streamAudio event is triggered, the seek/sound position is sent to the client. but my problem is, it won't set. The wiki's documentation is also wrong, it says that the setSoundPostion() function uses milliseconds, but it actually uses seconds. Any help??

Client:

nowPlaying = nil; 
  
function streamAudio(theUrl, theSeek) 
    if (nowPlaying) then 
        stopSound(nowPlaying); 
    end 
    nowPlaying = playSound(theUrl); 
    if (theSeek) then 
        setSoundPosition(nowPlaying, theSeek); 
    end 
end 
addEvent("streamAudio", true); 
addEventHandler("streamAudio", getRootElement(), streamAudio); 
  
function endStream() 
    if (nowPlaying) then 
        stopSound(nowPlaying); 
    end 
end 
addEvent("endStream", true); 
addEventHandler("endStream", getRootElement(), endStream); 

Link to comment

I did that, but the music starts from 0.

My Code:

server.lua

  
musicList = { }; 
theBaseUrl = "http://www.indigogames.net/tmhc/music/"; 
theStreamUrl = nil; 
theStreamPosition = 0; 
theStreamTimer = nil; 
  
function outputMessage(theMessage, theObject, theClass) 
    if (theClass == nil) then 
        outputChatBox("#00C3FF[TMHC]:#E0E0E0 " .. theMessage, theObject, 255, 255, 255, true); 
    elseif (theObject == nil) then 
        outputChatBox("#00C3FF[TMHC" .. theClass .. "]:#E0E0E0 " .. theMessage, getRootElement(), 255, 255, 255, true); 
    else 
        outputChatBox("#00C3FF[TMHC-" .. theClass .. "]:#E0E0E0 " .. theMessage, getRootElement(), 255, 255, 255, true); 
    end 
end 
  
function onResourceStart() 
    fetchRemote("http://www.indigogames.net/tmhc/music/index.php?mode=list", parseMusicData, "", false); 
end 
addEventHandler("onResourceStart", resourceRoot, onResourceStart); 
  
function onPlayerLogin() 
    triggerClientEvent("streamAudio", getRootElement(), theStreamUrl, theStreamPosition); 
end 
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin); 
  
function parseMusicData(responseData, errorNo) 
    if (errorNo == 0) then 
        if (string.len(responseData) > 0) then 
            musicList = fromJSON(responseData); 
            if (musicList == nil) then 
                outputMessage("#FF0000A system error occurred while parsing the music list.", getRootElement(), "Radio");    
                return false; 
            end 
        else 
            outputMessage("#FF0000A system error occurred while loading the music list.", getRootElement(), "Radio");    
            return false; 
        end 
    else 
        outputMessage("#FF0000A system error occurred while fetching the music list.", getRootElement(), "Radio");   
        return false; 
    end 
end 
  
function playRandomMusic(theTime) 
    setTimer(function() 
        local count = 0; 
        local randomInteger = math.random(0, table.getn(musicList)); 
        for theKey, theValue in pairs(musicList) do 
            if ((count == randomInteger) == false) then 
                count = count + 1; 
            else 
                beginStreaming(theValue); 
                theStreamUrl = theBaseUrl .. theValue; 
                theStreamPosition = 0; 
                theStreamTimer = setTimer(function() theStreamPosition = theStreamPosition + 1 end, 1000, 0); 
                return true; 
            end 
        end 
    end, theTime, 1); 
end 
  
function beginStreaming(theFileName) 
    for theKey, thePlayer in ipairs(getElementsByType("player")) do 
        triggerClientEvent("streamAudio", thePlayer, theBaseUrl .. theFileName); 
    end 
    fetchRemote("http://www.indigogames.net/tmhc/music/index.php?mode=list", parseMusicData, "", false); -- Get new list 
    fetchRemote("http://www.indigogames.net/tmhc/music/index.php?mode=info&filename=" .. theFileName, parseMusic, "", false); 
end 
  
function isPlayerAdmin(thePlayer) 
    if ((thePlayer) and (isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(thePlayer)), aclGetGroup("Admin")))) then 
        return true; 
    else 
        return false; 
    end 
end 
  
function streamMusic(theSource, theCommand, theUrl) 
    if ((isGuestAccount(getPlayerAccount(source)) == false) and (isPlayerAdmin(source) == true)) then 
        -- FIx this if the server can do it! 
        theStreamUrl = theBaseUrl .. theValue; 
        theStreamPosition = 0; 
        theStreamTimer = setTimer(function() theStreamPosition = theStreamPosition + 1 end, 1000, 0); 
        for theKey, thePlayer in ipairs(getElementsByType("player")) do 
            triggerClientEvent("streamAudio", thePlayer, theUrl); 
        end 
        outputMessage(getPlayerName(theSource) .. " is now streaming!", getRootElement(), "Radio"); 
    end 
end 
addCommandHandler("stream", streamMusic); 
  
function endStreaming(theSource, theCommand) 
    if ((isGuestAccount(getPlayerAccount(source)) == false) and (isPlayerAdmin(source) == true)) then 
        for theKey, thePlayer in ipairs(getElementsByType("player")) do 
            triggerClientEvent("endStream", thePlayer); 
        end 
        outputMessage(getPlayerName(theSource) .. "#FFFF00 has ended the radio stream!", getRootElement(), "Radio"); 
    end 
end 
addCommandHandler("endStream", endStreaming); 
  
function refreshMusicList(theSource, theCommand) 
    if ((isGuestAccount(getPlayerAccount(theSource)) == false) and (isPlayerAdmin(theSource) == true)) then 
        outputMessage(getPlayerName(theSource) .. " is refreshing the music list!", getRootElement(), "Radio"); 
        fetchRemote("http://www.indigogames.net/tmhc/music/index.php?mode=list", parseMusicData, "", false); 
    end 
end 
addCommandHandler("refreshMusicList", refreshMusicList); 
  
function startStream(theSource, theCommand) 
    if ((isGuestAccount(getPlayerAccount(theSource)) == false) and (isPlayerAdmin(theSource) == true)) then 
        outputMessage(getPlayerName(theSource) .. " has started the radio stream!", getRootElement(), "Radio"); 
        playRandomMusic(50); 
    end 
end 
addCommandHandler("startStream", startStream); 
  
function parseMusic(responseData, errorNo) 
    local musicData = fromJSON(responseData); 
    if (musicData) then 
        outputMessage("Now playing: " .. musicData["title"] .. " by " .. musicData["artist"] .. "!", getRootElement(), "Radio"); 
        outputMessage("The next song will be played in " .. musicData["length_long"] .. "!", getRootElement(), "Radio"); 
        playRandomMusic(tonumber(musicData["length"]) * 1000 + 3000); 
    else 
        playRandomMusic(tonumber(musicData["length"]) * 1000 + 3000); 
    end 
end 
  

client.lua

  
nowPlaying = nil; 
  
function streamAudio(theUrl, theSeek) 
    if (nowPlaying) then 
        stopSound(nowPlaying); 
    end 
    nowPlaying = playSound(theUrl); 
    if (theSeek) then 
        setSoundPosition(nowPlaying, theSeek * 1000); 
    end 
end 
addEvent("streamAudio", true); 
addEventHandler("streamAudio", getRootElement(), streamAudio); 
  
function endStream() 
    if (nowPlaying) then 
        stopSound(nowPlaying); 
    end 
end 
addEvent("endStream", true); 
addEventHandler("endStream", getRootElement(), endStream); 
  

Link to comment

Help anyone?

This works, but not in the streamAudio Event:

  
function setSongPos(cmd, seconds) 
    local ssp = setSoundPosition(nowPlaying, seconds) 
    if ssp then 
        outputChatBox("Sound is now playing from: "..seconds.." Seconds!") 
    else 
        outputChatBox("An error has occured. Please make shore there are atleast") 
        outputChatBox(seconds.." Seconds in this song!") 
    end 
end 
addCommandHandler("seek", setSongPos) 
  

Link to comment
You can't set streamed audio to a certain position, as they are being streamed live and is not being recorded.

It works when I do it with the command though.

EDIT:

Whatever, got it working with:

  
thePosition = 0; 
function updatePosition(theSeek) 
    thePosition = theSeek; 
end 
addEvent("updateStreamPosition", true); 
addEventHandler("updateStreamPosition", getRootElement(), updatePosition); 
  
function resume() 
    if (nowPlaying) then 
        outputMessage("Resuming!", "Radio"); 
        setSoundPosition(nowPlaying, thePosition); 
    end 
end 
addCommandHandler("resume", resume); 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...