Hey
I have been trying to make a chatmachine, and i wanted to make it play music when the user asked for it, but the problem is how can i make the music stop again? Without closing the player, but by closing with the program..
I used this code

System.Diagnostics.Process.Start("C:Moves Like Jagger - Maroon 5 featuring Christina Aguilera - YouTube.mp3");

But how can i make it stop when the user writes stop..?

Recommended Answers

All 10 Replies

Process.Start returns a Process object. Use that to kill the process.

Likely on your system there is also a Windows Media Player class.

Don't really get that with killing it, can not find it..

I know that there are a wmp class, but I can not get that to work..

Process p = System.Diagnostics.Process.Start("C:Moves Like Jagger - Maroon 5 featuring Christina Aguilera - YouTube.mp3");

// when you want it to stop
p.Kill();

Thanks
But it have a problem..
the error is "Cannot process request because the process has exited."

else if (sætning.ToLower().Contains("musik"))
                    {
                        Console.WriteLine("Min musiksmag er stor, fordi jeg kan sådan set lide alt med lidt gang i den.");
                        Console.WriteLine("Hvad kan du lide af musik?");
                        Console.ReadLine();
                        Console.WriteLine("Ikke dårlig musiksmag, gør det noget jeg spiller noget musik?");
                        sætning4 = Console.ReadLine();
                        if (sætning4.ToLower().Contains("nej") || sætning4.ToLower().Contains("no"))
                        {
                            System.Diagnostics.Process p = System.Diagnostics.Process.Start("C:Moves Like Jagger - Maroon 5 featuring Christina Aguilera - YouTube.mp3");
                            Console.WriteLine("For at stoppe musikken skal du skrive stop");
                            sætning5 = Console.ReadLine();
                            if (sætning5 == "stop")
                            {
                                p.Kill();
                            }
                        }
                    }

It is in danish..

try

if (sætning5 == "stop" && !p.HasExited)
{
   p.Kill();
}

nothing happens.. wmp keeps playing..

That is probably because the process is actually a process spawned by Explorer that in turn executes WMP player. Therefore your p does not point to the WMP process.
Try using the command line arguments for media player in your command like this.

string filename = "C:Moves Like Jagger - Maroon 5 featuring Christina Aguilera - YouTube.mp3";
var commandArgs = string.Format(@"/play /close ""{0}""", filename);
System.Diagnostics.Process p = System.Diagnostics.Process.Start("Mplayer2.exe", commandArgs);

When what do i write to stop it again..

When what do i write to stop it again..

p.kill();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.