Jump to content

[QUESTION] getSoundMetaTags


botshara

Recommended Posts

Hello, Im making radio script and I want get stream title from radios, but problem is that I get very big perfomance lags when I start my radio.
Im using onClientRender Handler and getSoundMetaTags function to get stream title. Without this function script using only ~2% CPU but with this function (getSoundMetaTags) script using ~20% CPU.

There is any alternative using onClientRender Handler and dont get perfomance lags ?

Link to post
addEventHandler ( "onClientRender", root,
function ( )
	if isPedInVehicle ( localPlayer ) then
		local vehicle = getPedOccupiedVehicle ( localPlayer )		
		if not vehicle then return end
		
		local radioID = getElementData ( vehicle, "radioID" )

		if radioID and radioID >= 0 then
			
			local sound = getSoundsBlyat[vehicle]
			if isElement(sound) and sound and getSoundMetaTags ( sound ) then
				song = getSoundMetaTags ( sound )["stream_title"]
          end
and so on .......

 

Link to post

The reason why getSoundMetaTags is slow is that it has to read a piece of the stream to get the desired data.
As a fix, you should call getSoundMetaTags only once or, if you need to update it from time to time, use a timer.

E.g.:

local streamTitle = ""
setTimer(
  function()
    streamTitle = getSoundMetaTags(sound).streamTitle
  end, 5000, 1
) -- Update every 5 seconds

addEventHandler("onClientRender", root,
  function()
    -- Do whatever you want to do with 'streamTitle'
  end
)

 

  • Like 1
Link to post

What about this (untested)

addEventHandler("onClientVehicleEnter", getRootElement(),
function(player, seat)
  if player == localPlayer then
	 local radioID = getElementData (source, "radioID" )
	 if radioID and radioID >= 0 then
	  local sound = getSoundsBlyat[source]
	  if sound and isElement(sound) then
		 local meta = getSoundMetaTags(sound)
		 title = meta.title
	  end 
	end
  end 
end
)

-- "onClientPlayerRadioSwitch"
addEventHandler("onClientPlayerRadioSwitch", getLocalPlayer(), 
function (station)
  if isPedInVehicle ( localPlayer ) then
	if station ~= 0 then
	   local vehicle = getPedOccupiedVehicle ( localPlayer )
	    -- use the same code here and make sure you keep title (global variable)
	end 
  end	
end
)


addEventHandler ( "onClientRender", root,
function ( )
  if title then
	-- draw sound title here
  end
and 

 

Edited by Walid
  • Like 1
Link to post

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...