Click here to Skip to main content
15,120,883 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)[^]
   
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
Member 11380615 18-Jan-16 6:35am
   
Hello Serkan Onat,
I think I got the even handler list. When I right click on the media player control it gives me the option of adding the even handler. I will add from here and try and test it. It appears different from what you meant.
Regards,
Rakesg
Member 11380615 18-Jan-16 7:05am
   
Thanks,
I got the event.
Regards,
Rakesh
Member 11380615 19-Jan-16 3:13am
   
Hello Serkan Onat,
I have been trying to get the media player to play in full screen. I get a runtime exception for hrFull )below in code) which says E_UNEXPECTED catastrophic failure. I see this when I put a break point other wise the code runs fine though it doesnt go to full screen. By right clicking on the running media player and selecting zoom full screen I am able to play in full screen.
My objective is to have a button clicking which should play full screen.

The code below is what I am using. This code is in InitDialog.

LPUNKNOWN pUnknown = m_MediaPlayer.GetControlUnknown();
//LPUNKNOWN pUnknown1 = m_MediaPlayer1.GetControlUnknown();


HRESULT hr = pUnknown->QueryInterface(__uuidof(IWMPPlayer), (void**)&m_pWMPPlayer);
//HRESULT hr1 = pUnknown1->QueryInterface(__uuidof(IWMPPlayer), (void**)&m_pWMPPlayer1);


if (SUCCEEDED(hr))
{
CComBSTR strMovie = _T("C:\\movie\\Hero 2004.mp4");
m_pWMPPlayer->put_URL(strMovie);


VARIANT_BOOL bSc = VARIANT_TRUE;
HRESULT hrFull;

hrFull = m_pWMPPlayer->put_fullScreen(bSc);

}

Look forward to your direction on what may be done for full screen.
Regards,
Rakesh
Serkan Onat 19-Jan-16 16:57pm
   
I tried what you said and it worked fine here (Windows 10 X64) Visual Studio 2010
if you have issues with old version then ,you can try IWMPPlayer2 interface instead

in addition check the version dependencies of WMP interfaces at MSDN site from the link i provided at first message

P.S : i personally did not use WMP in applications (as hosted), i prefer LibVLC instead , and i always suggest the use of LibVLC when asked
Member 11380615 20-Jan-16 2:29am
   
Hello Serkan Onat,
I did put this question in codeproject. It was suggested that I should try to put it in full screen only when it is playing. Wait for the play event and then put full screen. I did that and it worked.
Thanks for the suggestion.
Will try with IWMPPlayer2
Will try LibVLC as well.
Regards,
Rakesh
Member 13999828 10-Oct-18 4:59am
   
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.
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.
   

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