Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I have been trying to add media player to my mfc dialog and invoke the same to play URL. I have been unsuccessful.
OS - Windows 10
VS Studio - VS 2013 professional

My objective is to embed the media player in my application and pass a file name to to start playing.

Steps done to add media player.

1. Create a mfc application dialog project with activex control enabled.
The project creates the following files
video.cpp
videoDlg.cpp
video.h
videoDlg.h

2. Go to resource manager
Go to video.rc - Dialog
Double click IDD_VIDEO_DIALOG

3. Right click and insert activex control on the dialog window.
Choose the windows media player from the list and press OK.

4. Right click on the media player on the dialog and click
class wizard -> Choose mfc class from type lib (Only this gives windows media player as an option
From the available libraries choose "windows media player <1.0>
Then choose "ImediaPlayer2" from the interfaces and add it to generated classes.
The above step creates the following
CMediaPlayer2 class and CMediaPlayer.h header file.
Then I add variable name vidctrl of type COcx1
This creates two files ocx1.h and ocx1.cpp

The I add vidctrl.open() in my init code. It doesnt find the open function.
So I copy the functions from the CMediaPlayer2.h to ocx1.h

Then I try to call vidctrl.open() again and it builds.
When I run it a message window pops up saying "member not found"
When I step through I find that message window pops when the vidctrl.open() is called.


I can send the project files as a zip for this.
The problem I am facing is how to connect the variable to the interface functions?
Any help would be very much appreciated.
Thanks & Regards,
Rakesh
Posted

Hi
Here is the steps to start working with Media Player on a MFC Dialog based application

1: Create a new dialog based MFC application

2: Open dialog designer and remove default label control that's created by VS template

3: Right click in dialog and select insert activex control

4: Select and insert Windows Media Player control to dialog

5: Resize and reposition control as you whish

6: Right click to control and select Add Variable

7: Enter m_MediaPlayer (or whatever you want) as the control variable without changing any other property

8: This will add a ocx1.h and a ocx1.cpp to the project , and #include "ocx1.h" is also added to main dialog

9: Open stdafx.h and add this #include <wmp.h> include directive to it

10: Open header file of Dialog class that contains Media Player object and add this CComPtr<IWMPPlayer> m_pWMPPlayer; variable to somewhere in it

11: Now open CPP file of dialog class and goto OnInitDialog() and add following code
C++
LPUNKNOWN pUnknown = m_MediaPlayerControl.GetControlUnknown();
HRESULT hr = pUnknown->QueryInterface(__uuidof(IWMPPlayer), (void**)&m_pWMPPlayer);
if(SUCCEEDED(hr))
{
	CComBSTR strMovie = _T("Full Path To Movie File");
	m_pWMPPlayer->put_URL(strMovie); 
}


12: Build and see result


If you want to implement more functionality of Media Player then
Take a look at available interfaces of Media Player at MSDN site Interfaces (Windows Media Player)[^]
 
Share this answer
 
v2
Comments
Member 11380615 10-Jan-16 1:25am    
Hello Serkan Onat,
Thank You so much for the solution posted by you. I should be able to test it in the next few days and will update you on the results.
Regards,
Rakesh
Member 11380615 17-Jan-16 23:13pm    
Hello Serkan Onat,
I could implement what you said and got the player a play a clip.
I need to handle event from the media player and be able to play and stop from with in the code.
I have been trying to add a handler and be able to get handles to play and stop and other functionalities. I didnt work.
HRESULT hr1 = pUnknown->QueryInterface(&spConnectionContainer);
if (SUCCEEDED(hr))
{
hr = spConnectionContainer->FindConnectionPoint(__uuidof(IWMPEvents), &m_spConnectionPoint);
if (FAILED(hr))
{
// If not, try the _WMPOCXEvents interface, which uses IDispatch.
hr = spConnectionContainer->FindConnectionPoint(__uuidof(_WMPOCXEvents), &m_spConnectionPoint);
}
}

if (SUCCEEDED(hr))
{
hr = m_spConnectionPoint->Advise(this, &m_dwAdviseCookie);
}

The this in Advise doesnt compile.

Could you give some direction as to how to play, pause and stop from within the code Also create a handler where I could get the play, pause, stop and other media player control events.
Regards,
Rakesh
Serkan Onat 18-Jan-16 3:24am    
You would better do following ,because CWnd container already doing that

Goto dialog designer , select media player control
And then its events will be listed under properties pane (you need to click control events icon)
Then you can select an event and add it to your project
Visual Studio will automatically add callback function to parent window class just like the MFC way
Then you can add your code to implement event procedure


Member 11380615 18-Jan-16 6:30am    
I do not see the events listed. Probably I am on a wrong understanding of what the dialog designer is. When you say dialog designer I do the following.
Resource View -> ProjectName.rc -> Dialog -> IDD_VIDEO_PLAYER(xxxxxxx)
I double click on it and I am presented with the dialog designer. It contains the windows media player active X control. I select it but I dont see any properties pane with event listed. What I see is the properties window in the bottom right which has items like "open state" "play state" "url" "windowless Video".
I have wrongly understood on what you mean by dialog designer.
Regards,
Rakesh
Serkan Onat 18-Jan-16 6:54am    
Like i said in previous message you need to click Control Events icon to see event list
see image :

http://i.hizliresim.com/a2YOYd.png
I am a little bit perplexed by this:

I can insert the control fine, but from there on the instructions don't work for me. The following:

6: Right click to control and select Add Variable

7: Enter m_MediaPlayer (or whatever you want) as the control variable without changing any other property

8: This will add a ocx1.h and a ocx1.cpp to the project , and #include "ocx1.h" is also added to main dialog
.

is unclear.

a) Right click on the control or the dialog itself?
b) What type is m_MediaPlayer ?
c) the ocx1 header and source files do not get added in mine.

When I follow the above steps, all that is added to my files is a variable m_MediaPlayer of type COCX.
Type COCX is subsequently unrecognised by the compiler.

Please help.

BW, Rez.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900