Jump to content
The simFlight Network Forums

Play Sound with FSUIPC.NET


Recommended Posts

FSUIPC can play sounds within the sim via offset 0x4200, but it's limited to .WAV files only.

If you want to try this, I can give you some example code.

The only other alternative is to have your application play the sounds using the System.Media.SoundPlayer class. This supports all types of sound files, but of course will play on the computer that your application is running on.

https://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net

Paul

Link to comment
Share on other sites

4 hours ago, Paul Henty said:

FSUIPC can play sounds within the sim via offset 0x4200, but it's limited to .WAV files only.

If you want to try this, I can give you some example code.

The only other alternative is to have your application play the sounds using the System.Media.SoundPlayer class. This supports all types of sound files, but of course will play on the computer that your application is running on.

https://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net

Paul

Thanks Paul for the reply,

Wav file is a bit heavy for storage, im looking for the nuget library that can play Ogg file in my application, however I haven't found one with free license yet. 😞

Link to comment
Share on other sites

  • 2 months later...
On 3/5/2022 at 10:50 AM, Paul Henty said:

FSUIPC can play sounds within the sim via offset 0x4200, but it's limited to .WAV files only.

If you want to try this, I can give you some example code.

The only other alternative is to have your application play the sounds using the System.Media.SoundPlayer class. This supports all types of sound files, but of course will play on the computer that your application is running on.

https://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net

Paul

Hello Paul,

Could you please give me some examples of playing wav by 0x4200 ?

Thanks

Link to comment
Share on other sites

Here are the offset declarations and two methods, one of playing sounds, one for stopping sounds.

        private Offset<byte> soundDevice = new Offset<byte>("sounds", 0x4201, true);
        private Offset<int> soundID = new Offset<int>("sounds", 0x4204, true);
        private Offset<string> soundFilePath = new Offset<string>("sounds", 0x4208, 248, true);
        private Offset<byte> soundCommand = new Offset<byte>("sounds", 0x4200, true);


        /// <summary>
        /// Plays a sound via FSUIPC
        /// </summary>
        /// <param name="filePath">The path to the WAV file. Can be a full path, or relative to the FlightSim's 'Sound' folder</param>
        /// <param name="loop">Pass true to play the sound in a continuous loop</param>
        /// <returns>The ID of the sound. Save this if you need to stop the sound later.</returns>
        private int PlaySound(string filePath, bool loop)
        { 
            int id = new object().GetHashCode();
            soundCommand.Value = (byte)(loop ? 0x2 : 0x1); // 2 = Play looped. 1 = Play Once
            soundID.Value = id;
            soundDevice.Value = 0; // 0 = Default sound card. (FSUIPC lists the devices in the [Sounds] section of its INI file.)
            soundFilePath.Value = filePath;
            FSUIPCConnection.Process("sounds");
            return id;
        }

        private void StopSound(int id)
        {
            soundCommand.Value = 3; // STOP
            soundID.Value = id;
            soundDevice.Value = 0;
            soundFilePath.Value = "";
            FSUIPCConnection.Process("sounds");
        }

To play a sound once call like this:

            //  Play door closing - once.
            int id = PlaySound("xdoor_small.wav", false);

Note that this WAV file is in the FlightSim's 'Sound' folder so I just use the file name. You can also specify a full path to anywhere on the disk if you want. Start with the drive letter (e.g C:\)

If you don't need to stop the sound from playing later you can ignore the ID number that is returned from this method.

To play a sound in a loop, call like this:

            // Play rain in loop. Save ID to stop it later.
            rainSoundID = PlaySound("precip\\xprecg4.wav", true);

This wav is in the 'precip' folder under the 'sounds' folder. The ID returned is used to stop this sound in the next example:

To stop a sound from playing:

		StopSound(rainSoundID);

 

Note that the sounds are played by FSUIPC, so if your application exits while sounds are playing they will continue to play. If you don't want that, keep a list of all your long-running and looped sounds so you can stop them before your application exits..

I don't know if this is implemented in FSUIPC7 (for MSFS).

Paul

Link to comment
Share on other sites

9 minutes ago, hkhoanguyen said:

@Paul Henty

Thanks Paul for the example. Do you know if we can control the volumn with it ? 

No, it's not possible with the 0x4200 offset. It always plays at maximum volume. All you can do it set the audio to the correct volume in the WAV files. But, you can't change it in real time.

If you need more control over the audio you'll need a dedicated library like NAudio.

Paul

Link to comment
Share on other sites

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.