Here's the tip end of what I'm talking about.
function updateCarRadio()
local state = getElementData(localPlayer, "streams")
if (state == "0") then
setRadioChannel(0)
-- kill all radio channels
for _, value in pairs(soundElementsOutside) do
stopSound(value)
end
soundElementsOutside = {}
-- kill the mp3 player
if mp3Sound then
stopSound(mp3Sound)
mp3Sound = nil
mp3Station = nil
end
else
-- repopulate all radio channels
local vehicles = getElementsByType("vehicle")
for _, theVehicle in ipairs(vehicles) do
if (isElementStreamedIn(theVehicle)) then
spawnSound(theVehicle)
end
end
end
end
addEvent("accounts:settings:updateCarRadio", false)
addEventHandler("accounts:settings:updateCarRadio", getRootElement(), updateCarRadio)
--
local mp3Window = nil
local width, height = mp3_w, mp3_h
function destroyMP3Window()
if mp3Window then
destroyElement(mp3Window)
mp3Window = nil
end
end
addEventHandler('onClientChangeChar', root,
function()
if mp3Sound then
stopSound(mp3Sound)
mp3Sound = nil
mp3Station = nil
end
destroyMP3Window()
end)
function showMP3GUI()
destroyMP3Window()
mp3Window = guiCreateStaticImage(mp3_x, mp3_y, mp3_w, mp3_h, ":resources/window_body.png", false)
local grid = guiCreateGridList(10, 10, width - 20, height - 50, false, mp3Window)
local col = guiGridListAddColumn(grid, 'Station', 0.85)
for k, v in ipairs(streams) do
if k ~= 0 then
local row = guiGridListAddRow(grid)
guiGridListSetItemText(grid, row, col, v[1], false, false)
guiGridListSetItemData(grid, row, col, tostring(k))
end
end
addEventHandler('onClientGUIClick', grid,
function()
local item = guiGridListGetSelectedItem(grid)
if item ~= -1 then
local station = tonumber(guiGridListGetItemData(grid, item, col))
if mp3Sound then
stopSound(mp3Sound)
mp3Sound = nil
end
mp3Sound = playSound(streams[station][2], true)
mp3Station = station
end
end, false)
local off = guiCreateButton(10, height - 35, 85, 25, 'Turn Off', false, mp3Window)
addEventHandler('onClientGUIClick', off,
function()
if mp3Sound then
stopSound(mp3Sound)
mp3Sound = nil
mp3Station = nil
end
end, false)
local close = guiCreateButton(105, height - 35, 85, 25, 'Close', false, mp3Window)
addEventHandler('onClientGUIClick', close, destroyMP3Window, false)
end
addEvent('realism:mp3:off', true)
addEventHandler('realism:mp3:off', root,
function()
if mp3Sound then
stopSound(mp3Sound)
mp3Sound = nil
mp3Station = nil
end
end)