Jump to content

[HELP] car radio [Engine off]


ScofieldX

Recommended Posts

I have this small problem.

It involves carradio that's the resource, Now here's my problem The car radio streams good when the engine is running, but when I turn the engine off, to save gas, the radio turns off, I'm not a scripter when it comes to .lua But if anyone can help me out with how to make the radio still play even when the engine is turned off that would be fantastic.

Link to comment

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) 

Edited by Guest
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...