diff --git a/O21.Game/Game.fs b/O21.Game/Game.fs index 6e6140e..1b35633 100644 --- a/O21.Game/Game.fs +++ b/O21.Game/Game.fs @@ -4,6 +4,7 @@ open System open System.Collections.Concurrent open System.Threading +open System.Threading.Tasks open JetBrains.Lifetimes open O21.Game.Engine open type Raylib_CsLo.Raylib @@ -40,6 +41,7 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) = let! player = CreateMusicPlayerAsync lt (content.SoundFontPath, data.MidiFilePath) player.Initialize() state <- { state with MusicPlayer = Some player } + Task.Run(fun() -> UpdateMusicPlayer lt &player) |> ignore } |> ignore member _.Initialize(lifetime: Lifetime): unit = @@ -73,10 +75,6 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) = SetSoundVolume(effect, state.Settings.SoundVolume) PlaySound(effect) - match state.MusicPlayer with - | Some player when player.NeedsPlay() -> player.Play() - | _ -> () - state <- { state with SoundsToStartPlaying = Set.empty } member _.Draw() = diff --git a/O21.Game/Music.fs b/O21.Game/Music.fs index 1c23c47..c2863d6 100644 --- a/O21.Game/Music.fs +++ b/O21.Game/Music.fs @@ -50,3 +50,12 @@ let CreateMusicPlayerAsync (lifetime: Lifetime) (soundFontPath: string, midiFile let buffer = Array.zeroCreate(BufferSize * 2) return MusicPlayer(Buffer = buffer, Stream = audioStream, Sequencer = sequencer) } + +let UpdateMusicPlayer (lifetime: Lifetime) (player:inref) = + try + while lifetime.IsAlive do + match player with + | player when player.NeedsPlay() -> player.Play() + | _ -> () + with + | :? LifetimeCanceledException -> ()