Loading and playing a wxMediaCtrl

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Loading and playing a wxMediaCtrl

Post by forrestcupp »

I want to be able to click a wxButton and have it load a file into a wxMediaCtrl, then play it immediately, but I can't figure out how to do it. I can load the file ahead of time in my MyFrame constructor then play it when I click the button, but I can't figure out how to do them both at once in the same event.
arbcow
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Jan 21, 2008 4:53 am

Post by arbcow »

create a wrapper(class) for wxmedia, then just create a var with it, and it will play :)

DemoMediaWrapper * dmo = new DemoMediaWrapper(m_wxMediaParent,musicfile.mp3,true);
//wrapper example

DemoMediaWrapper::DemoMediaWrapper(wxWindow * parent,wxString filename,bool looping)
:wxMediaCtrl(parent , IDM_MEDIACONTROL,wxEmptyString, wxDefaultPosition, wxDefaultSize/*, 0, wxMEDIABACKEND_DIRECTSHOW*/)
{
bool result = Load(filename.data());
m_looping = looping;
}

void DemoMediaWrapper::OnMediaLoaded(wxMediaEvent & ev)
{
Play();
}

void DemoMediaWrapper::OnMediaFinished(wxMediaEvent & ev)
{
if(m_looping)
Play();
}
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

Thanks for your response. It gave me some ideas, but I'd rather try it without a wrapper. But I can't get what I'm trying to work. I'm just trying to set up a function to capture the Loaded event and I can't get it to work. My problem is that I like to use Connect instead of event tables, and all of the examples I've seen use event tables. So here's what I put in my MyFrame constructor that didn't work (keep in mind that mySound is a wxMediaCtrl that I declared in my header):

Code: Select all

	mySound = new wxMediaCtrl(panel, ID_MEDIA);
	Connect(ID_MEDIA, wxEVT_MEDIA_LOADED, wxMediaEventHandler(MyFrame::OnLoaded));
That didn't catch the media loaded event, so I tried connecting it directly to mySound which didn't work either.

Code: Select all

mySound->Connect(ID_MEDIA, wxEVT_MEDIA_LOADED, wxMediaEventHandler(MyFrame::OnLoaded));
In this one, I also tried substituting -1 for ID_MEDIA, and it didn't work either. I put a SetStatusText in my OnLoaded function just to test and see if it was ever accessed, and it isn't.

I don't know what I'm doing wrong here.
arbcow
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Jan 21, 2008 4:53 am

Post by arbcow »

Yeah i have tried it a few ways, but if you write that wrapper you can use it like your using mysound, just a small amount of code. I think theres things going on in the background that you cant see and that why its not working as expected.
Compilers
gcc / gnu make (OSX)
Visual Studio 2005 (MS Windows)
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

I appreciate your help. I really want to get this to work, but I just can't. I think I may have found a problem that is perplexing me. I don't have a wxMediaCtrl::OnMediaLoaded function. I do have the OnMediaFinished, but there's nothing for the Loaded. That must be my whole problem, but I can't understand why it wouldn't be there. Without the OnMediaLoaded function, I can't even make the wrapper you showed me.
arbcow
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Jan 21, 2008 4:53 am

Post by arbcow »

post your wrapper code, ill correct it
Compilers
gcc / gnu make (OSX)
Visual Studio 2005 (MS Windows)
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

I made a class called cuppMediaCtrl.

Here is the header file:

Code: Select all

#include "wx/wx.h"
#include "wx/mediactrl.h"

class cuppMediaCtrl : public wxMediaCtrl
{
public:
	cuppMediaCtrl(wxWindow* parent,wxString filename, bool looping);
	bool LoadFile(wxString filename);
	void SetLoop(bool loop);
	bool GetLoop();
	void OnMediaLoaded(wxMediaEvent& event);
	void OnMediaFinished(wxMediaEvent& event);

private:
	bool isLooping;
};
And here is the cpp file:

Code: Select all

#include "cuppMediaCtrl.h"


cuppMediaCtrl::cuppMediaCtrl(wxWindow *parent, wxString filename = _T(""), bool looping = false) :
wxMediaCtrl(parent, -1, filename)
{
	if(filename != _T(""))
		bool result = Load(filename.data());
	isLooping = looping;
	return;
}

bool cuppMediaCtrl::LoadFile(wxString filename)
{
	bool result = this->Load(filename.data());
	return result;
}

void cuppMediaCtrl::SetLoop(bool loop = true)
{
	isLooping = loop;
	return;
}

bool cuppMediaCtrl::GetLoop()
{
	return isLooping;
}

void cuppMediaCtrl::OnMediaLoaded(wxMediaEvent& event)
{
	Play();
}

void cuppMediaCtrl::OnMediaFinished(wxMediaEvent &event)
{
	if(isLooping)
		Play();
}
Like I said, my intellisense automatically showed the OnMediaFinished, but there wasn't an OnMediaLoaded.
C_Bastian
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Sep 29, 2005 3:47 pm

Post by C_Bastian »

You do not need a wrapper.

All you have to do is this:

in MediaDialog::CreateControls()

Code: Select all

    mediactrl=new wxMediaCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(60,60), wxNO_BORDER, wxMEDIABACKEND_WMP10); //Or whatever you need here
    sizer->Add(mediactrl,1,wxEXPAND,0);
     
    mediactrl->Connect(wxEVT_MEDIA_LOADED, wxMediaEventHandler(MediaDialog::OnLoad), NULL, this);
Sebastian
Last edited by C_Bastian on Thu Jan 24, 2008 3:09 pm, edited 1 time in total.
Programming today is a race between software engineers stirring to build bigger and better idiot-proof programs, and the universe is trying to produce bigger and better idiots. So far, the universe is winning. [Rich Cook]
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

Well, that was the whole problem to begin with. wxEVT_MEDIA_LOADED isn't working for me. I was trying that before arbcow told me to make a wrapper. I dont even have an OnMediaLoaded function in my wxMediaCtrl class like I should.

I don't have any problem Connecting to wxEVT_MEDIA_FINISHED. I have done that for looping without any trouble and it works. And I do have an OnMediaFinished function in my wxMediaCtrl class.

I am using wxWidgets 2.8.6 from wxPack 2.8.6. I wonder if they made a mistake and left that function out of this version.
C_Bastian
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Sep 29, 2005 3:47 pm

Post by C_Bastian »

Did you notice the differences in our Connect() call?

In my example an event of the wxMediaCtrl is connected to a function of the dialog which is the parent of the wxMediaCtrl.

So my function is of a MediaDialog::OnLoad type.

Your code did not work for me, too.

I'm using wxWidgets 2.8.4 and my Connect() works fine. I'll have a closer look at it later.

Sebastian
Programming today is a race between software engineers stirring to build bigger and better idiot-proof programs, and the universe is trying to produce bigger and better idiots. So far, the universe is winning. [Rich Cook]
C_Bastian
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Sep 29, 2005 3:47 pm

Post by C_Bastian »

forrestcupp wrote:Well, that was the whole problem to begin with. wxEVT_MEDIA_LOADED isn't working for me. I was trying that before arbcow told me to make a wrapper. I dont even have an OnMediaLoaded function in my wxMediaCtrl class like I should.
in src/common/mediactrlcmn.cpp you'll find: DEFINE_EVENT_TYPE(wxEVT_MEDIA_LOADED)

But there is no OnMediaLoaded function in wxMediaCtrl, because there is no event table. That's why you have to connect it to a function manually.
Programming today is a race between software engineers stirring to build bigger and better idiot-proof programs, and the universe is trying to produce bigger and better idiots. So far, the universe is winning. [Rich Cook]
arbcow
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Jan 21, 2008 4:53 am

Post by arbcow »

#pragma once
#include "wx/wx.h"
#include "wx/mediactrl.h"

class cuppMediaCtrl : public wxMediaCtrl
{
typedef wxMediaCtrl Inherited;
public:
cuppMediaCtrl(wxWindow* parent,wxString filename, bool looping);
bool LoadFile(wxString filename);
void SetLoop(bool loop);
bool GetLoop();
void OnMediaLoaded(wxMediaEvent& event);
void OnMediaFinished(wxMediaEvent& event);

protected:
enum MenuIds {
IDM_MEDIACONTROL = wxID_HIGHEST + 100
};

private:
bool isLooping;

DECLARE_EVENT_TABLE()
};


And here is the cpp file:
wxwidgets:
#include "cuppMediaCtrl.h"


BEGIN_EVENT_TABLE(cuppMediaCtrl, Inherited)
EVT_MEDIA_LOADED(IDM_MEDIACONTROL,OnMediaLoaded)
EVT_MEDIA_STOP(IDM_MEDIACONTROL,OnMediaFinished)
END_EVENT_TABLE()


cuppMediaCtrl::cuppMediaCtrl(wxWindow *parent, wxString filename = _T(""), bool looping = false) :
wxMediaCtrl(parent, IDM_MEDIACONTROL, wxEmptyString, wxDefaultPosition, wxDefaultSize)
{
if(filename != _T(""))
bool result = Load(filename.data());
isLooping = looping;
return;
}

bool cuppMediaCtrl::LoadFile(wxString filename)
{
bool result = this->Load(filename.data());
return result;
}

void cuppMediaCtrl::OnMediaLoaded(wxMediaEvent& event)
{
Play();
}

void cuppMediaCtrl::OnMediaFinished(wxMediaEvent &event)
{
if(isLooping)
Play();
}
Compilers
gcc / gnu make (OSX)
Visual Studio 2005 (MS Windows)
forrestcupp
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Dec 28, 2006 5:12 pm
Location: Indiana, US

Post by forrestcupp »

C_Bastian wrote:Did you notice the differences in our Connect() call?

In my example an event of the wxMediaCtrl is connected to a function of the dialog which is the parent of the wxMediaCtrl.

So my function is of a MediaDialog::OnLoad type.
What is MediaDialog? Is that the name of a class you created, or what? Is it a frame class you made, or is it something built into wxWidgets?
C_Bastian
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Sep 29, 2005 3:47 pm

Post by C_Bastian »

Oh, sorry, I just copied some lines of code from one of my applications. It is just the dialog where the wxMediaCtrl is located in. Let's say, the parent window of the wxMediaCtrl.
Programming today is a race between software engineers stirring to build bigger and better idiot-proof programs, and the universe is trying to produce bigger and better idiots. So far, the universe is winning. [Rich Cook]
Post Reply