I've been looking for a way to disable the radio as smoothly as possible (so no sound cut-off or infinite loops anywhere), this is what Daniel Hartung (from Discord) and I came up with:
Client-side (trigger this in your server-side playerEnterVehicle event, has to be triggered server-side cause the client-side variant triggers too early before the player is in the vehicle)
const localPlayer = mp.players.local;
let radioKillTimer = null;
mp.game.audio.setUserRadioControlEnabled(false);
mp.events.add("disableVehicleRadio", () => {
if(radioKillTimer == null){
radioKillTimer = setInterval(() => {
if(localPlayer.isSittingInAnyVehicle()){
mp.game.audio.setRadioToStationName("OFF");
clearInterval(radioKillTimer);
radioKillTimer = null;
return;
}
}, 100);
}
});
The custom client event checks whether a timer to kill the radio already exists, if not it creates one (interval 100ms) that runs until the local player entity reports the fact that they're sitting in a vehicle. That's when the radio is set to off and the timer is killed and it's ensured that the radio station is only set once (preventing jittery sound).