Starting music in Form 1 and stopping in Form 2

2.1k views Asked by At

I'm looking to make a 'Music on/off' button in my application. I'm using the following code in Form 1, this is the form in which I play the music:

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

namespace WWE2K14SaveEditor
{
    public partial class Form1 : Form
    {
        Form2 frm2 = new Form2();
        public WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

        public Form1()
        {
            InitializeComponent();
        }   


        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                wplayer.URL = "music/main.mp3";
                wplayer.controls.play();
            }
            catch { }    

        }

I want the music to stop when I press a button in Form 2, I've tried the following code, but it doesn't seem to work:

Form1 frm1 = new Form1();
frm1.wplayer.controls.stop();

(This code is used in the button click event.)

This is a Windows Forms application. I'm using the WMPLib. Any help would be much appreciated, thanks.

3

There are 3 answers

1
Janne Matikainen On BEST ANSWER

Use

public static WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

then call it from Form2 with

Form1.wplayer.controls.stop();

Currently you are just instantiating new instance of Form1 in your Form2 which will have different wplayer instance than the one you have started the music.

3
Alex Humphrey On

You are creating a second instance of Form1 which has no relation to the instance playing the music.

You need to pass a reference to the Form1 instance which is playing the music to Form2, and have it call stop() through that instance.

Actually, you may as well pass the WMPLib.WindowsMediaPlayer instance on to Form2, as it will prevent Form2 having to know anything about Form1.

e.g.

public Form2(WMPLib.WindowsMediaPlayer player)
{
    // Assign player to a private member variable.
    this.player = player;
}

Then create your second form using new Form2(wplayer);, and call player.stop(); in your click handler.

0
Paul Coghill On
Form1 frm1 = new Form1();

This creates a new instance of your form, it doesn't reference the form playing the music.

You need to pass a reference to Form1 to Form2. One tactic could be to add a property to Form2 called MusicForm or something similar - then when you create form2 you can pass a reference to form1.

public partial class Form2 : Form
    {

        public Form1 MusicForm { get; set;}
        ...
        ...

Then you can modify Form1:

 public partial class Form1 : Form
    {
        Form2 frm2 = new Form2();
        frm2.MusicForm = this;

        public WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

Then call: this.MusicForm.wplayer.controls.stop();

Of course, you don't need to pass the whole form if all if all you want to pass in the WindowsMediaPlayer instead - you could also just pass that in as a property.