hkhoanguyen Posted March 5, 2022 Report Posted March 5, 2022 Hello, I would like to implement a functionality that can play sound (mp3, ogg, etc) within the sim with FSUIPC.NET, could you please tell me if it is possible or suggest me some ideas ? Thanks
Paul Henty Posted March 5, 2022 Report Posted March 5, 2022 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
hkhoanguyen Posted March 5, 2022 Author Report Posted March 5, 2022 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. 😞
Paul Henty Posted March 5, 2022 Report Posted March 5, 2022 NAudio is free (MIT licence) and you can extend it to play ogg files with the additional NAudio.Vorbis library. Both are available on NuGet. https://github.com/naudio/NAudio https://github.com/naudio/Vorbis Paul
hkhoanguyen Posted May 14, 2022 Author Report Posted May 14, 2022 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
Paul Henty Posted May 14, 2022 Report Posted May 14, 2022 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
hkhoanguyen Posted May 16, 2022 Author Report Posted May 16, 2022 @Paul Henty Thanks Paul for the example. Do you know if we can control the volumn with it ?
Paul Henty Posted May 16, 2022 Report Posted May 16, 2022 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now