I'm developing a WinForms customized version of Pac-Man for a Software Engineering project and I reached the point where I have to implement music and sound effects. I used the MVC architectural pattern, so almost every entity in the game has: a model, a view and a controller (for example, I have a MainMenuModel, a MainMenuView and a MainMenuController).

In order to play music and sounds I decided to use the System.Windows.MediaPlayer class (because the System.Media.SoundPlayer one cannot play two sounds simultaneously). More specifically, I put in every controller the related MediaPlayer objects. So, for example, in the MainMenuController I put: the MenuBackgroundMusicPlayer, the MenuItemSelectionSoundPlayer and the MenuItemOpeningSoundPlayer.

This solution works fine, but I would like to know if it is considered bad design to have multiple instances of MediaPlayer spread all over the controllers, instead of having a single static centralized MediaPlayer instance that handles everything. The problem with having a single instance is that I don't know then how to play two or more sounds simultaneously, because a single MediaPlayer object can handle a single media at a time.

  • \$\begingroup\$ If you're set on engineering your own solution then ignore this, but if you have the liberty of using external tools, look into FMod \$\endgroup\$
    – Natalo77
    May 26 '20 at 8:40
  • 1
    \$\begingroup\$ Unfortunately, the use of external tools is prohibited. \$\endgroup\$ May 26 '20 at 8:45
  • \$\begingroup\$ MVC is an uncommon pattern in game development in general. So you are already pretty far away from the usual design choices in game development. \$\endgroup\$
    – Philipp
    May 26 '20 at 10:41
  • \$\begingroup\$ I think MVC is kind of overkill anyway when not working with webapplications or clients with different operation systems. Both of these features I would not combine with Forms. \$\endgroup\$
    – Gener4tor
    May 26 '20 at 13:10

The design pattern of "having a single static centralized instance" is called a "Singleton". See description here

There are some classes from microsoft who are supposed to be used as a singelton for resource and caching reasons. Like HttpClient

For System.Windows.Media.MediaPlayer there seems to be no limitation of this kind. In this case I dont see any reason to use it as a singleton. For a more detailed discussion of the pros and cons of singletons look e.g. here

Its considered bad design to use a singleton without very good reason. This is because singletons are hard to unittest and limit your design possibilities in the future.

  • \$\begingroup\$ Indeed, I considered using the Singleton pattern, but a unique instance cannot handle everything as I want. Alternatively, what about gathering all the instances into a collection (array or list) of MediaPlayers, declared in a single class shared by all the controllers? Could it be a better solution? Or is it better to declare each MediaPlayer where it is effectively used? \$\endgroup\$ May 26 '20 at 12:13
  • \$\begingroup\$ @LordFenerSSJ: Dont create stuctures for no reason. If you dont need a collection, why putting effort into building one? This just produces extra work and adds to the complexity and the possible bugs. If you need a list in the future you can still build one (but you perhaps can already access all of them via the controls) \$\endgroup\$
    – Gener4tor
    May 26 '20 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.