From Multi Theft Auto: Wiki
Jump to: navigation, search

This function returns a specific browser's volume.


float getBrowserVolume ( browser webBrowser )

OOP Syntax Help! I don't understand this!

Method: browser:getVolume(...)
Counterpart: setBrowserVolume

Required Arguments

  • webBrowser: A browser element


Returns a specific browser's volume, or false if the browser element passed to the function is invalid.


Creates a browser in which the volume can be controlled by pressing the page-up & page-down keys

--In order to render the browser on the full screen, we need to know the dimensions.
local screenWidth, screenHeight = guiGetScreenSize()
--Let's create a new browser in remote mode.
local webBrowser = createBrowser(screenWidth, screenHeight, false, false)

-- How much we increase/decrease the volume by each time
local volumeStep = 0.1 

-- The min/max value for the browser volume. Change these if you like, but note that the minimum value is 0 and the maximum value is 1.
local MIN_VOLUME = 0
local MAX_VOLUME = 1
--Function to render the browser.
function webBrowserRender()
	--Render the browser on the full size of the screen.
	dxDrawImage(0, 0, screenWidth, screenHeight, webBrowser, 0, 0, 0, tocolor(255,255,255,255), true)

--The event onClientBrowserCreated will be triggered, after the browser has been initialized.
--After this event has been triggered, we will be able to load our URL and start drawing.
addEventHandler("onClientBrowserCreated", webBrowser, 
		--After the browser has been initialized, we can load
		loadBrowserURL(webBrowser, "")
		--Now we can start to render the browser.
		addEventHandler("onClientRender", root, webBrowserRender)

-- Now we create a function attached to an onClientKey event handler to catch user input.
-- Here we can utilize the getBrowserVolume function to lower or increase the volume of the browser based on the clients input
function handleVolumeKeys(button, press)
    if (press) then -- Is key pressed?
        if (button == "pgup") then
		local volume = getBrowserVolume(webBrowser) + volumeStep -- Get the current browser volume and add our volume step
		if (volume) > MAX_VOLUME then return end -- Check if the current volume is bigger than max value
		setBrowserVolume(webBrowser, volume) -- Set the browser volume			
	elseif (button == "pgdn") then -- Same as previous condition except we subtract the volume step and make sure the volume value doesn't fall below minimum.
		local volume = getBrowserVolume(webBrowser) - volumeStep
		if (volume) < MIN_VOLUME then return end
		setBrowserVolume(webBrowser, volume) 		
addEventHandler("onClientKey", root, handleVolumeKeys)

See Also

GUI Functions