locked
Creating own media player using WMP Mobile RRS feed

  • Question

  • Hi,

    I am creating a simple media player which is going to play media file using Windows Media Player. I have been able to create one in C# using the Interopt.WMPLib.dll and it is working fine. Now how can I create such a media player using C++.

    Here is my C# code. I would like to do the same thing in C++ but am not sure on how to do it.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using WMPLib;
    
    
    namespace TPlayer
    {
        public partial class MusicBoyMain : Form
        {
            WMPLib.WindowsMediaPlayer Player = new WindowsMediaPlayer();
    
            public MusicBoyMain()
            {
                InitializeComponent();
            }
    
            private void menuItem1_Click(object sender, EventArgs e)
            {
                DialogResult DlgResult;
                DlgResult = MessageBox.Show("Do you want to close MusicBoy Media Player?", "Close MediaBoy", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (DlgResult == DialogResult.Yes)
                {
                    Application.Exit();
                }
            }
    
            private void OpenButton_Click(object sender, EventArgs e)
            {
                DialogResult DlgResult;
                DlgResult = MediaFileDialogue.ShowDialog();
                if (DlgResult == DialogResult.OK)
                {
                    MediaToost.Caption = "Playing selected media";
                    MediaToost.InitialDuration = 5;
                    MediaToost.Text = "Now playing: " + MediaFileDialogue.FileName;
                    MediaToost.Visible = true;
    
                    Player.URL = MediaFileDialogue.FileName;
                    Player.settings.volume = 100;
                    Player.controls.play();
                }
            }
    
            private void PlayButton_Click(object sender, EventArgs e)
            {
                 if (PlayButton.Text == "Play")
                {
                    Player.settings.volume = 100;
                    Player.controls.play();
                    PlayButton.Text = "Pause";
                }
                else
                {
                    Player.controls.play();
                    PlayButton.Text = "Play";
                }
            }
    
            private void StopButton_Click(object sender, EventArgs e)
            {
                Player.controls.stop();
            }
        }
    }


    Thank you,
    CED
    • Edited by C.E.D Wednesday, June 24, 2009 7:03 AM
    Wednesday, June 24, 2009 7:01 AM

Answers

  • //.h file code:
    
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::ComponentModel;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Windows::Forms;
    using namespace System::Runtime::InteropServices;
    using namespace WMPLib;
    
    
    namespace TPlayer
    {
    	public ref class MusicBoyMain : Form
    	{
    	private:
    		WMPLib::WindowsMediaPlayer ^Player;
    
    	public:
    		MusicBoyMain();
    
    	private:
    		void menuItem1_Click(System::Object ^sender, EventArgs ^e);
    
    		void OpenButton_Click(System::Object ^sender, EventArgs ^e);
    
    		void PlayButton_Click(System::Object ^sender, EventArgs ^e);
    
    		void StopButton_Click(System::Object ^sender, EventArgs ^e);
    
    	private:
    		void InitializeInstanceFields()
    		{
    			static bool initialized = false;
    			if ( ! initialized)
    			{
    				Player = gcnew WindowsMediaPlayer();
    
    				initialized = true;
    			}
    		}
    	};
    }
    
    //.cpp file code:
    
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::ComponentModel;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Windows::Forms;
    using namespace System::Runtime::InteropServices;
    using namespace WMPLib;
    namespace TPlayer
    {
    
    	MusicBoyMain::MusicBoyMain()
    	{
    		InitializeInstanceFields();
    		InitializeComponent();
    	}
    
    	void MusicBoyMain::menuItem1_Click(System::Object ^sender, EventArgs ^e)
    	{
    		System::Windows::Forms::DialogResult DlgResult;
    		DlgResult = MessageBox::Show("Do you want to close MusicBoy Media Player?", "Close MediaBoy", MessageBoxButtons::YesNo, MessageBoxIcon::Question, MessageBoxDefaultButton::Button2);
    		if (DlgResult == System::Windows::Forms::DialogResult::Yes)
    		{
    			Application::Exit();
    		}
    	}
    
    	void MusicBoyMain::OpenButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		System::Windows::Forms::DialogResult DlgResult;
    		DlgResult = MediaFileDialogue->ShowDialog();
    		if (DlgResult == System::Windows::Forms::DialogResult::OK)
    		{
    			MediaToost->Caption = "Playing selected media";
    			MediaToost->InitialDuration = 5;
    			MediaToost->Text = "Now playing: " + MediaFileDialogue::FileName;
    			MediaToost->Visible = true;
    	
    			Player->URL = MediaFileDialogue::FileName;
    			Player->settings->volume = 100;
    			Player->controls->play();
    		}
    	}
    
    	void MusicBoyMain::PlayButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		 if (PlayButton->Text == "Play")
    		{
    			Player->settings->volume = 100;
    			Player->controls->play();
    			PlayButton->Text = "Pause";
    		}
    		else
    		{
    			Player->controls->play();
    			PlayButton->Text = "Play";
    		}
    	}
    
    	void MusicBoyMain::StopButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		Player->controls->stop();
    	}
    }
    I have converted using the tool. Please test it and let me know if you face any issues.

    Regards,
    Amit Rote
    Wednesday, June 24, 2009 1:42 PM

All replies

  • //.h file code:
    
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::ComponentModel;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Windows::Forms;
    using namespace System::Runtime::InteropServices;
    using namespace WMPLib;
    
    
    namespace TPlayer
    {
    	public ref class MusicBoyMain : Form
    	{
    	private:
    		WMPLib::WindowsMediaPlayer ^Player;
    
    	public:
    		MusicBoyMain();
    
    	private:
    		void menuItem1_Click(System::Object ^sender, EventArgs ^e);
    
    		void OpenButton_Click(System::Object ^sender, EventArgs ^e);
    
    		void PlayButton_Click(System::Object ^sender, EventArgs ^e);
    
    		void StopButton_Click(System::Object ^sender, EventArgs ^e);
    
    	private:
    		void InitializeInstanceFields()
    		{
    			static bool initialized = false;
    			if ( ! initialized)
    			{
    				Player = gcnew WindowsMediaPlayer();
    
    				initialized = true;
    			}
    		}
    	};
    }
    
    //.cpp file code:
    
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::ComponentModel;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Text;
    using namespace System::Windows::Forms;
    using namespace System::Runtime::InteropServices;
    using namespace WMPLib;
    namespace TPlayer
    {
    
    	MusicBoyMain::MusicBoyMain()
    	{
    		InitializeInstanceFields();
    		InitializeComponent();
    	}
    
    	void MusicBoyMain::menuItem1_Click(System::Object ^sender, EventArgs ^e)
    	{
    		System::Windows::Forms::DialogResult DlgResult;
    		DlgResult = MessageBox::Show("Do you want to close MusicBoy Media Player?", "Close MediaBoy", MessageBoxButtons::YesNo, MessageBoxIcon::Question, MessageBoxDefaultButton::Button2);
    		if (DlgResult == System::Windows::Forms::DialogResult::Yes)
    		{
    			Application::Exit();
    		}
    	}
    
    	void MusicBoyMain::OpenButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		System::Windows::Forms::DialogResult DlgResult;
    		DlgResult = MediaFileDialogue->ShowDialog();
    		if (DlgResult == System::Windows::Forms::DialogResult::OK)
    		{
    			MediaToost->Caption = "Playing selected media";
    			MediaToost->InitialDuration = 5;
    			MediaToost->Text = "Now playing: " + MediaFileDialogue::FileName;
    			MediaToost->Visible = true;
    	
    			Player->URL = MediaFileDialogue::FileName;
    			Player->settings->volume = 100;
    			Player->controls->play();
    		}
    	}
    
    	void MusicBoyMain::PlayButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		 if (PlayButton->Text == "Play")
    		{
    			Player->settings->volume = 100;
    			Player->controls->play();
    			PlayButton->Text = "Pause";
    		}
    		else
    		{
    			Player->controls->play();
    			PlayButton->Text = "Play";
    		}
    	}
    
    	void MusicBoyMain::StopButton_Click(System::Object ^sender, EventArgs ^e)
    	{
    		Player->controls->stop();
    	}
    }
    I have converted using the tool. Please test it and let me know if you face any issues.

    Regards,
    Amit Rote
    Wednesday, June 24, 2009 1:42 PM
  • Hi Amit,

    Thank you. I have not gone through the code yet, but I will and let you know.

    Thank you,
    CED
    Wednesday, June 24, 2009 4:09 PM
  • Hello CED,

    Please let me know that whether your above C# code will run in WM 5.0 SDK in VS 2008.
    I am working on this SDK to develop a media player. I cannot find a way to start with.
    Please help me out.

    I encountered the following error, when I compiled the code in VS 2008, WM 5.0

    Error    1    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    24    25    ExcoMediaApp
    Error    2    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    27    17    ExcoMediaApp
    Error    3    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    28    17    ExcoMediaApp
    Error    4    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    29    17    ExcoMediaApp
    Error    5    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    29    53    ExcoMediaApp
    Error    6    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    30    17    ExcoMediaApp
    Error    7    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    32    30    ExcoMediaApp


    Thanks and Regards,
    RD
    Friday, July 17, 2009 12:39 PM
  • Hi RD,

    It should work.

    I dont think it will create any problem.

    R u facing any problem then please provide some info related to the same.

    Regards,
    Amit R
    Friday, July 17, 2009 1:04 PM
  • Hello Amit,

    Thanks for your reply. I have tried the C# version of the code posted by CED on VS 2008 and WM 5.0 SDK. This gives the following error,

    Error    1    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    24    25    ExcoMediaApp
    Error    2    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    27    17    ExcoMediaApp
    Error    3    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    28    17    ExcoMediaApp
    Error    4    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    29    17    ExcoMediaApp
    Error    5    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    29    53    ExcoMediaApp
    Error    6    The name 'MediaToost' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    30    17    ExcoMediaApp
    Error    7    The name 'MediaFileDialogue' does not exist in the current context    C:\Documents and Settings\Rajib\My Documents\Visual Studio 2008\Projects\SmartDeviceProject3\SmartDeviceProject3\Form4.cs    32    30    ExcoMediaApp

    Please help me out. I have included WMPLib dll file in to my project.

    Kind Regards
    RD
     
    Friday, July 17, 2009 7:17 PM
  • Hi RD,

    I have not used the code by Amit till now as the requirement for Media Player is not needed as of now. But I have tested the C# code and it is working properly.

    As for the MediaToost error, it is a actually a Toast component. It can be added from the tools like from where you add textbox, labels etc. In there you will find Toast Notification, add it and give its name as MediaToost. As for the MediaFileDialogue it is a OpenDialogue component from where you can select and open files.

    One more thing RD I have tried the above code in VS '05 and WM 6 and 6.5 in both emulator and device. If you still get any error please post them and I will try to solve them.

    Thank you,
    CED
    • Edited by C.E.D Monday, July 20, 2009 11:51 AM Added: The version and sdk in which I wrote teh C# code
    Monday, July 20, 2009 11:50 AM
  • Hi CED,

    Thanks for your reply. I hope I have resolved all the bugs.
    Let me try with the other issues.

    Regards,
    RD
    Tuesday, July 28, 2009 11:06 AM
  • thanks for this code i have been trying to do this for the last week.

    i am trying the code you posted but i dont see how to add WM player to the form to see the movie being player.  am i missing somthing?

    I am unclear about the toast notification.  can I use the regular notification control?


    Christian - developer
    Thursday, May 20, 2010 7:34 PM