C# Tutorial – Play Multiple sound files in Windows Form Application

In this tutorial we will demonstrate how to play multiple audio files in Windows Forms with C# programming. Audio is very important to games development, a must have good music and sound effects and they should be blended into the game so the player doesn’t notice them but knows they are there. In the past tutorials we used the Sound Player class to play simple sound effects in games but we haven’t covered a tutorial where you can play the background sound and a sound effect in a game yet. The sound player is very useful but its not practical for games development as the main audio player. For example you cannot play two sound files simultaneously during run time. The sound player class will stop the other sound from playing before loading and playing a new sound file.  This is where we can use the Media Player windows form component to ensure have steady music playing in the background and we can mix it up with the sound player class to play any sound effects we want to play in it.

Lesson Objectives –

  1. Import the media player component to windows forms toolbox
  2. Use the media player component to Load MP3 files during the run time
  3. Use sound player and media player together
  4. Use buttons to control the play and stop functionalities of the media player
  5. Learn how to repeat sound files or music in the media player with C#

Video

Download the sound files for this project

Full Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Media;

namespace Play_Mutiple_Sounds_MOO_ICT
{
    public partial class Form1 : Form
    {

        SoundPlayer backgroundMusic;
        SoundPlayer laserSound;
        bool bgPlaying = false;

        public Form1()
        {
            InitializeComponent();

            // set up the media player

            MXP.URL = @"bg_music_mediaplayer.mp3";
            MXP.settings.playCount = 9999; // repeating the music when it ends
            MXP.Ctlcontrols.stop();
            MXP.Visible = false;


        }

        private void PlayLaserSound(object sender, EventArgs e)
        {
            laserSound = new SoundPlayer(@"laserSound.wav");
            laserSound.Play();
        }

        private void PlayBackgroundSoundPlayer(object sender, EventArgs e)
        {
            backgroundMusic = new SoundPlayer(@"bg_music.wav");
            backgroundMusic.PlayLooping();
            MXP.Ctlcontrols.stop(); // stop the media player
            bgPlaying = true; // set playing to true
        }

        private void PlayMediaPlayer(object sender, EventArgs e)
        {
            if (bgPlaying == true)
            {
                backgroundMusic.Stop();
                bgPlaying = false;
            }

            MXP.Ctlcontrols.play();
        }
    }
}

 

 




Comments are closed.