none
How to play multiple sounds simultaniously using WMP without interference RRS feed

  • Question

  • Hi, i've been working on a TicTacToe game that i want to have background music and win/loss sounds.

    I start playing the background music with:

    string mp4path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BackgroundMusic.mp4");
            string applause = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "applause.mp4");
            string boo = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Boo.mp4");
    
            public Form1()
            {
                InitializeComponent();
                if (!File.Exists(mp4path))
                {
                    File.WriteAllBytes(mp4path, TicTacToe.Properties.Resources.Music);
                }
                WMP1.URL = mp4path;
                
                if (!File.Exists(applause))
                {
                    File.WriteAllBytes(applause, TicTacToe.Properties.Resources.Applause);
                }
                if (!File.Exists(boo))
                {
                    File.WriteAllBytes(boo, TicTacToe.Properties.Resources.Boo);
                }
                WMP2.settings.autoStart = false;
            }

    which starts playing because of me just assigning a value to WMP1.URL(it defaults with autoStart enabled i guess).
    whenever i win or lose the match, i state that:

    if (winner == true)//idc who wins or loses i just want to play the sound
                {
                    if (AIBtn.Text == "Enabled")
                    {
                        WMP2.URL = boo;
                        WMP2.Ctlcontrols.play();
                    }

    i DO NOT tell WMP2 to play the background music, i tell it to play boo.mp4.
    what happens when i start WMP2 is that the background music starts a new instance of its self while keeping the old instance.
    i do not know if it plays the background music on WMP2 or if WMP1 is playing 2 instances of its self.
    it also does not play the boo sound at all.

    I posted a .rar here:
    https://gofile.io/?c=sDUVa5

    To be clear, i want it to play background music on WMP1 and only use WMP2 for an applause/boo on a win/loss.
    i've only been doing programming for 3 months so if you do know a solution, could you make it sound as idiot friendly as possible?

    Saturday, January 18, 2020 1:05 PM

All replies

  • This example plays two songs at the same time independently of each other when you click the button the song starts.

    You don't show how you declare the two media players see below. Watch spelling and the upper or lower case of the path string etc.

    To make the example add a new form to a project with two buttons and then copy the code below. Change the code project and form name etc as required.

    using System;
    using System.Windows.Forms;
    
    namespace Test_C
    {
        public partial class Form2 : Form
        {
    
            WMPLib.WindowsMediaPlayer player1 = new WMPLib.WindowsMediaPlayer();
            WMPLib.WindowsMediaPlayer player2 = new WMPLib.WindowsMediaPlayer();
    
            string mp4path = @"C:\test sounds\W_HWY.WAV";
            string boo = @"c:\test sounds\W_LL484.wav";
    
            public Form2()
            {
                InitializeComponent();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                player1.URL = mp4path;
    
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                player2.URL = boo;
    
            }
        }
    }
    

    Saturday, January 18, 2020 2:04 PM
  • i was using two media players from the toolbox.

    ive now stated them as

    WMPLib.WindowsMediaPlayer WMP1 = new WMPLib.WindowsMediaPlayer();
            WMPLib.WindowsMediaPlayer WMP2 = new WMPLib.WindowsMediaPlayer()

    this makes no difference.

    idk if i did something wrong in my question but everything you described i already have, even better, all except for the two lines of code at the top of this comment, my program had all of that already

    my problem is that when it's supposed to play boo.mp4 it only starts a new instance of background music while still playing the first one
    also i dont want it to start on a button press.
    the background music auto starts which is okay, but then(when anyone gets 3 in a row) it starts again AND keeps playing its first instance so i hear 2 instances of the song even though its supposed to keep playing the 1 instance and play the boo sound.

    im never anywhere telling it to start a new instance of the background music but it keeps doing just that, without actually playing boo or me telling it to do so.


    • Edited by Zerioto Sunday, January 19, 2020 11:10 AM
    Sunday, January 19, 2020 11:09 AM
  • i was using two media players from the toolbox.

    ive now stated them as

    WMPLib.WindowsMediaPlayer WMP1 = new WMPLib.WindowsMediaPlayer();
            WMPLib.WindowsMediaPlayer WMP2 = new WMPLib.WindowsMediaPlayer()

    this makes no difference.

    idk if i did something wrong in my question but everything you described i already have, even better, all except for the two lines of code at the top of this comment, my program had all of that already

    my problem is that when it's supposed to play boo.mp4 it only starts a new instance of background music while still playing the first one
    also i dont want it to start on a button press.
    the background music auto starts which is okay, but then(when anyone gets 3 in a row) it starts again AND keeps playing its first instance so i hear 2 instances of the song even though its supposed to keep playing the 1 instance and play the boo sound.

    im never anywhere telling it to start a new instance of the background music but it keeps doing just that, without actually playing boo or me telling it to do so.


    I dont hear anything unreasonable to expect from your project.

    You just need to debug your code it sounds like?

    Put break points in the events where you do something with ths songs. When the execution stops at a break point use intellisense to look at the values of the song paths, the controls, etc. Single step execution one line at a time looking at the values and watching where the code goes.

    You may be triggering other events you dont realize. etc. Try to step through and check the code does what you think. Set break points in all the events and check things work as you expect.

    Remove things from your code one at a time and then test. When the problem stops the last thing you removed contains the problem.

    Make sure your project is set to break on clr errors in the Debug Exception Settings. You might have errors you don't realize that are being skipped over.

    Make sure you understand what each thing does. Make another one form example of single items and be sure you know how they work and different settings and options work.

    If you are still stuck you need to provide more detaile information for the forum members to help.

    Try to make a simple one form example of the problem you can post. Make the question one simple thing to describe. That is the hardest thing just communicating the problem. When making a working example of the problem one often finds the solution.

    :)

    Sunday, January 19, 2020 4:35 PM
  • Hi Zerioto,

    Thank you for posting here.

    I have a question to confirm. Are you sure this code has been executed? Is the condition of an "if" statement is "false" causing this code to not be executed at all?

    if (winner == true)//idc who wins or loses i just want to play the sound
    {
          if (AIBtn.Text == "Enabled")
          {
                WMP2.URL = boo;
                WMP2.Ctlcontrols.play();
          }
    }

    You can use breakpoints to check it, because until now I'm not sure what went wrong.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 20, 2020 7:19 AM