ravi.joshi53's picture

Play an audio in background


I want to play an audio in background for infinite time using DragonOgg, which should simply start after initializing the game and continue to play (repeatedly) till the user close the game.

Is it advisable to use an infinite loop inside OpenTk onLoad() method? How, if yes?



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
winterhell's picture

Use onLoad() only for initializing the start assets and dont block there. If you dont finish the function you wont be able to run and render the actual game.
I havent used dragonogg, but there is probably either a
-flag that you can set for the track to loop, fire-and-forget
-callback event handle that lets you know the track has finished playing, so you can start it again
- .IsPlaying() method that lets you know if the track is still playing. You can check this like once per frame, or spawn a new thread with an endless loop that checks for playback and then Thread.Sleep(1)

ravi.joshi53's picture

Hi winterhell,

Based on Fiddler's earlier suggestion, I moved towards Cgen.Audio. As per your comment, please see the implementation below-

using Cgen.Audio;
Sound guitarSound;
protected override void OnLoad(EventArgs e)
    guitarSound = new Sound("Guitar Sample", "GuitarSample.ogg");
protected override void OnUpdateFrame(FrameEventArgs e)
    if (guitarSound.GetStatus() != SoundChannel.Status.Playing)
   //Rest of the code starts after this

It works perfectly. Thanks.

winterhell's picture

You don't actually need Thread.Sleep(1) in there. That line is useful only if the check was in an endless loop in a separate thread.

Anyway I'm glad could help.

ravi.joshi53's picture

Thanks a lot winterhell .... I have removed the sleep.