public void Stop()
        {
            if (CurrentSound == null || CurrentSound.Sound == null)
            {
                return;
            }

            // Get stopped position
            var stPair = SoundFileTimePairs.Find(s => s.SoundFile == CurrentSound);

            stoppedPositionStation = stPair.StartTime + CurrentSound.PlayPosition();
            lastPlayedTime         = DateTime.Now;
            lastPlayedSoundIndex   = SoundFileTimePairs.IndexOf(stPair);
            stoppedPositionSound   = CurrentSound.PlayPosition();

            // Set name in wheel to just the station name
            WheelCategoryItem radioWheelItem = corrWheelCat.ItemList[0];

            radioWheelItem.Name = Name;

            //CurrentSound.StopSound();
            CurrentSoundIsPaused = true;
            CurrentSound         = null;

            Function.Call(Hash.SET_AUDIO_FLAG, "DisableFlightMusic", false);
            Function.Call(Hash.SET_AUDIO_FLAG, "DisableWantedMusic", false);
        }
        public void Play()
        {
            if (!hasPlayedOnce)
            {
                CurrentSound = SoundFileTimePairs[0].SoundFile;
                CurrentSound.PlaySound(false, false, true);
                CurrentSound.Sound.PlayPosition = CurrentSound.GetRandomPlayPosition();
                CurrentSound.Sound.Paused       = false;
                UpdateRadioLengthWithCurrentSound();
                hasPlayedOnce = true;

                UpdateWheelInfo();
            }
            else
            {
                if (!allSoundsPlayedOnce &&
                    lastPlayedSoundIndex == SoundFileTimePairs.Count - 1)
                {
                    allSoundsPlayedOnce = true;
                }

                ResumeContinuity();
            }

            Function.Call(Hash.SET_AUDIO_FLAG, "DisableFlightMusic", true);
            Function.Call(Hash.SET_AUDIO_FLAG, "DisableWantedMusic", true);
        }
        public void HandleRadioWheelExtraControls()
        {
            if (WheelVars.CurrentRadioWheel.Visible)
            {
                if (RadioStation.CurrentPlaying != null)
                {
                    ControlSkipTrack  = GTAFunction.UsingGamepad() ? Config.GP_Skip_Track : Config.KB_Skip_Track;
                    ControlVolumeUp   = GTAFunction.UsingGamepad() ? Config.GP_Volume_Up : Config.KB_Volume_Up;
                    ControlVolumeDown = GTAFunction.UsingGamepad() ? Config.GP_Volume_Down : Config.KB_Volume_Down;
                    ControlNextWheel  = GTAFunction.UsingGamepad() ? GTA.Control.VehicleAccelerate : GTA.Control.WeaponWheelPrev;
                    ControlPrevWheel  = GTAFunction.UsingGamepad() ? GTA.Control.VehicleBrake : GTA.Control.WeaponWheelNext;

                    if (Config.DisplayHelpText)
                    {
                        GTAFunction.DisplayHelpTextThisFrame(
                            GTAFunction.InputString(ControlSkipTrack) +
                            " : Skip Track\n" +
                            GTAFunction.InputString(ControlVolumeUp) + " " +
                            GTAFunction.InputString(ControlVolumeDown) +
                            " : Volume: " +
                            Math.Round(SoundFile.SoundEngine.SoundVolume * 100, 0) + "%\n" +
                            GTAFunction.InputString(ControlNextWheel) + " " +
                            GTAFunction.InputString(ControlPrevWheel) +
                            " : Next / Prev Wheel\n", false, false);
                    }

                    if (Game.IsControlJustPressed(2, ControlSkipTrack))
                    {
                        RadioStation.CurrentPlaying.PlayNextSong();
                    }
                    else if (Game.IsControlJustPressed(2, ControlVolumeUp))
                    {
                        SoundFile.StepVolume(0.05f, 2);
                    }
                    else if (Game.IsControlJustPressed(2, ControlVolumeDown))
                    {
                        SoundFile.StepVolume(-0.05f, 2);
                    }
                }
            }
            if (RadioStation.CurrentPlaying != null)
            {
                Game.DisableControlThisFrame(2, GTA.Control.VehicleNextRadio);
                Game.DisableControlThisFrame(2, GTA.Control.VehicleNextRadioTrack);
                Game.DisableControlThisFrame(2, GTA.Control.VehiclePrevRadio);
                Game.DisableControlThisFrame(2, GTA.Control.VehiclePrevRadioTrack);

                RadioNativeFunctions.SetVanillaRadioOff();
            }
        }
 private void OnAbort(object sender, EventArgs e)
 {
     Game.TimeScale = 1f;
     SoundFile.DisposeSoundEngine();
     Function.Call(Hash.CLEAR_TIMECYCLE_MODIFIER);
     Function.Call(Hash.SET_AUDIO_FLAG, "DisableFlightMusic", false);
     Function.Call(Hash.SET_AUDIO_FLAG, "DisableWantedMusic", false);
     if (Function.Call <bool>(Hash.IS_AUDIO_SCENE_ACTIVE, "DEATH_SCENE"))
     {
         Function.Call(Hash.STOP_AUDIO_SCENE, "DEATH_SCENE");
         Function.Call(Hash.STOP_AUDIO_SCENE, "FADE_OUT_WORLD_250MS_SCENE");
     }
     if (Function.Call <bool>(Hash.IS_AUDIO_SCENE_ACTIVE, "MP_JOB_CHANGE_RADIO_MUTE"))
     {
         Function.Call(Hash.STOP_AUDIO_SCENE, "MP_JOB_CHANGE_RADIO_MUTE");
     }
 }
        private void PlayNextSound()
        {
            int currentSoundIndex = 0;

            if (CurrentSound != null)
            {
                currentSoundIndex = SoundFileTimePairs.IndexOf(SoundFileTimePairs.Find(s => s.SoundFile == CurrentSound));
                CurrentSound.StopSound();
            }

            // Set next in list
            currentSoundIndex = currentSoundIndex < SoundFileTimePairs.Count - 1 ? currentSoundIndex + 1 : 0;

            CurrentSound = SoundFileTimePairs[currentSoundIndex].SoundFile;
            CurrentSound.PlaySound(true);
            UpdateRadioLengthWithCurrentSound();
            UpdateWheelInfo();
            UpdateTrackUpdateTimer();
        }
        private void ResumeContinuity()
        {
            uint elapsed = (uint)(DateTime.Now - lastPlayedTime).TotalMilliseconds;

            var lastPlayedSound = SoundFileTimePairs[lastPlayedSoundIndex].SoundFile;

            //UI.ShowSubtitle("allSoundsPlayed: " + allSoundsPlayedOnce +
            //    "\nelapsed ms: " + elapsed +
            //    "\nRemaining playtime: " + (lastPlayedSound.Length - stoppedPositionSound));

            if (allSoundsPlayedOnce)
            {
                uint newPlayPos = GetTimeFromPrevious(stoppedPositionStation, TotalLength, elapsed);
                var  stPair     = SoundFileTimePairs.LastOrDefault(s => newPlayPos >= s.StartTime);
                CurrentSound = stPair != default(SoundFileTimePair) ? stPair.SoundFile : SoundFileTimePairs[0].SoundFile;
                CurrentSound.PlaySound(true, false, true);
                UpdateRadioLengthWithCurrentSound();
                CurrentSound.Sound.PlayPosition = Math.Max(0, newPlayPos - stPair.StartTime);
                CurrentSoundIsPaused            = false;
            }
            else if (elapsed < lastPlayedSound.Length - stoppedPositionSound)
            {
                CurrentSound = SoundFileTimePairs[lastPlayedSoundIndex].SoundFile;
                CurrentSound.PlaySound(true, false, true);
                CurrentSound.Sound.PlayPosition = Math.Min(CurrentSound.Length - 1, stoppedPositionSound + elapsed);
                CurrentSoundIsPaused            = false;
                UpdateRadioLengthWithCurrentSound();
            }
            else
            {
                CurrentSound = lastPlayedSoundIndex != SoundFileTimePairs.Count - 1 ?
                               SoundFileTimePairs[lastPlayedSoundIndex + 1].SoundFile : SoundFileTimePairs[0].SoundFile;
                CurrentSound.PlaySound(true);
                UpdateRadioLengthWithCurrentSound();
            }

            UpdateWheelInfo();
            if (CurrentSound.HasTrackList)
            {
                UpdateTrackUpdateTimer();
            }
        }
        void OnTick(object sender, EventArgs e)
        {
            if (!loaded)
            {
                if (!Game.Player.CanControlCharacter || Game.IsLoading)
                {
                    return;
                }

                if (loadDelayTimer == null)
                {
                    loadDelayTimer = DateTime.Now.AddMilliseconds(Config.LoadStartDelay);
                }

                Decorators.DEntity = Game.Player.Character;

                if (loadDelayTimer < DateTime.Now || Decorators.ScriptHasLoadedOnce)
                {
                    if (Config.DisplayHelpText)
                    {
                        UI.ShowSubtitle("Loading Custom Radios...");
                    }

                    SetupRadio();
                    SetupEvents();

                    // Allow playing MP audio sounds and scenes
                    Function.Call(Hash.SET_AUDIO_FLAG, "LoadMPData", true);

                    RadioNativeFunctions.DashboardScaleform = new ScaleformHelper.Scaleform("dashboard", true);

                    if (!Decorators.ScriptHasLoadedOnce)
                    {
                        Decorators.Init(Game.Player.Character);
                    }

                    loaded = true;

                    if (Config.DisplayHelpText)
                    {
                        UI.ShowSubtitle("Custom Radios Loaded");
                    }

                    if (Config.CustomWheelAsDefault && WheelVars.RadioWheels.Count > 0)
                    {
                        lastRadioWasCustom     = true;
                        canResumeCustomStation = true;
                    }
                }

                return; // Return if loaded is still not true
            }

            if (WheelVars.RadioWheels.Count == 0)
            {
                return;
            }

            if (GTAFunction.HasCheatStringJustBeenEntered("radio_reload"))
            {
                Config.LoadINI();
                Config.UpdateWheelsVisuals();
                Config.ReloadStationINIs();
                Config.RescanForTracklists();
                UI.ShowSubtitle("Custom Radio INIs reloaded:\n- settings.ini\n- station.ini files\n- Scanned for tracklists");
                Wait(150);
            }

            if (VanillaOrCustomRadioWheelIsVisible())
            {
                if (GTAFunction.UsingGamepad() && Game.IsControlJustPressed(2, Config.GP_Toggle))
                {
                    HandleRadioWheelToggle();
                }

                if (lastRadioWasCustom)
                {
                    WheelVars.CurrentRadioWheel.Visible = true;
                }

                lastPlayedOnFoot = Game.Player.Character.IsInVehicle() ? false : true;
            }

            if (Game.IsControlJustReleased(2, GTA.Control.VehicleRadioWheel))
            {
                if (WheelVars.CurrentRadioWheel.Visible)
                {
                    WheelVars.CurrentRadioWheel.Visible = false;
                }
            }

            Wheel.ControlTransitions(Config.EnableWheelSlowmotion);
            WheelVars.RadioWheels.ForEach(w => w.ProcessSelectorWheel());
            HandleRadioWheelQueue();
            SoundFile.ManageSoundEngine();
            RadioStation.ManageStations();
            HandleRadioWheelExtraControls();
            HandleQueuedStationActions();
            HandleEnterExitVehicles();
            UpdateDashboardInfo();
            HandleGamePause();
            GeneralEvents.Update();
        }
 public SoundFileTimePair(SoundFile sFile, uint time)
 {
     SoundFile = sFile;
     StartTime = time;
 }