#include <wx/mediactrl.h>
wxMediaCtrl uses native backends to render media, for example on Windows there is a ActiveMovie/DirectShow backend, and on Macintosh there is a QuickTime backend.
For general operation, all you need to do is call Load() to load the file you want to render, catch the EVT_MEDIA_LOADED
event, and then call Play() to show the video/audio of the media in that event.
More complex operations are generally more heavily dependant on the capabilities of the backend. For example, QuickTime cannot set the playback rate of certain streaming media - while DirectShow is slightly more flexible in that regard.
EVT_MEDIA_STOP
event to its parent window, at which point the event handler can choose to veto the event, preventing the stream from actually stopping.Example:
//connect to the media event this->Connect(wxMY_ID, wxEVT_MEDIA_STOP, (wxObjectEventFunction) (wxEventFunction)(wxMediaEventFunction) &MyFrame::OnMediaStop); //... void MyFrame::OnMediaStop(const wxMediaEvent& evt) { if(bUserWantsToSeek) { m_mediactrl->SetPosition( m_mediactrl->GetDuration() << 1 ); evt.Veto(); } }
When wxMediaCtrl stops, either by the EVT_MEDIA_STOP
not being vetoed, or by manually calling Stop(), where it actually stops is not at the beginning, rather, but at the beginning of the stream. That is, when it stops and play is called, playback is gauranteed to start at the beginning of the media. This is because some streams are not seekable, and when stop is called on them they return to the beginning, thus wxMediaCtrl tries to keep consistant for all types of media.
Note that when changing the state of the media through Play() and other methods, the media may not actually be in the wxMEDIASTATE_PLAYING
, for example. If you are relying on the media being in certain state catch the event relevant to the state. See wxMediaEvent for the kinds of events that you can catch.
The idea with setting wxMediaCtrl::GetBestSize() to the size of the video is that GetBestSize() is a wxWindow-derived function that is called when sizers on a window recalculate. What this means is that if you use sizers by default the video will show in its original size without any extra assistance needed from the user.
wxMEDIACTRLPLAYERCONTROLS_NONE
which turns off the native interface, and wxMEDIACTRLPLAYERCONTROLS_DEFAULT
which lets wxMediaCtrl decide what native controls on the interface. Be sure to review the caveats outlined in Video size before doing so.The following are valid backend identifiers:
Note that other backends such as wxMEDIABACKEND_MCI can now be found at wxCode (http://wxcode.sourceforge.net/).
You need to make sure to use the DECLARE_CLASS
and IMPLEMENT_CLASS
macros.
The only real tricky part is that you need to make sure the file in compiled in, which if there are just backends in there will not happen and you may need to use a force link hack (see http://www.wxwidgets.org/wiki/index.php/RTTI).
This is a rather simple example of how to create a backend in the wxActiveXContainer documentation.
Public Member Functions | |
wxMediaCtrl () | |
Default constructor - you MUST call Create() before calling any other methods of wxMediaCtrl. | |
wxMediaCtrl (wxWindow *parent, wxWindowID id, const wxString &fileName=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &szBackend=wxEmptyString, const wxValidator &validator=wxDefaultValidator, const wxString &name="mediaCtrl") | |
Constructor that calls Create(). | |
bool | Create (wxWindow *parent, wxWindowID id, const wxString &fileName=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &szBackend=wxEmptyString, const wxValidator &validator=wxDefaultValidator, const wxString &name="mediaCtrl") |
Creates this control. | |
wxSize | GetBestSize () const |
Obtains the best size relative to the original/natural size of the video, if there is any. | |
double | GetPlaybackrate () |
Obtains the playback rate, or speed of the media. | |
wxMediaState | GetState () |
Obtains the state the playback of the media is in -. | |
double | GetVolume () |
Gets the volume of the media from a 0.0 to 1.0 range. | |
wxFileOffset | Length () |
Obtains the length - the total amount of time the movie has in milliseconds. | |
bool | Load (const wxString &fileName) |
Loads the file that fileName refers to. | |
bool | Load (const wxURI &uri) |
Loads the location that uri refers to. | |
bool | Load (const wxURI &uri, const wxURI &proxy) |
Loads the location that uri refers to with the proxy proxy . | |
bool | LoadURI (const wxString &fileName) |
Same as Load(const wxURI& uri). | |
bool | LoadURIWithProxy (const wxString &fileName, const wxString &proxy) |
Same as Load(const wxURI& uri, const wxURI& proxy). | |
bool | Pause () |
Pauses playback of the movie. | |
bool | Play () |
Resumes playback of the movie. | |
wxFileOffset | Seek (wxFileOffset where, wxSeekMode mode=wxFromStart) |
Seeks to a position within the movie. | |
bool | SetPlaybackRate (double dRate) |
Sets the playback rate, or speed of the media, to that referred by dRate. | |
bool | SetVolume (double dVolume) |
Sets the volume of the media from a 0.0 to 1.0 range to that referred by dVolume . | |
bool | ShowPlayerControls (wxMediaCtrlPlayerControls flags=wxMEDIACTRLPLAYERCONTROLS_DEFAULT) |
A special feature to wxMediaCtrl. | |
bool | Stop () |
Stops the media. | |
wxFileOffset | Tell () |
Obtains the current position in time within the movie in milliseconds. |
wxMediaCtrl::wxMediaCtrl | ( | ) |
Default constructor - you MUST call Create() before calling any other methods of wxMediaCtrl.
wxMediaCtrl::wxMediaCtrl | ( | wxWindow * | parent, | |
wxWindowID | id, | |||
const wxString & | fileName = wxEmptyString , |
|||
const wxPoint & | pos = wxDefaultPosition , |
|||
const wxSize & | size = wxDefaultSize , |
|||
long | style = 0 , |
|||
const wxString & | szBackend = wxEmptyString , |
|||
const wxValidator & | validator = wxDefaultValidator , |
|||
const wxString & | name = "mediaCtrl" | |||
) |
Constructor that calls Create().
You may prefer to call Create() directly to check to see if wxMediaCtrl is available on the system.
parent | parent of this control. Must not be NULL. | |
id | id to use for events | |
fileName | If not empty, the path of a file to open. | |
pos | Position to put control at. | |
size | Size to put the control at and to stretch movie to. | |
style | Optional styles. | |
szBackend | Name of backend you want to use, leave blank to make wxMediaCtrl figure it out. | |
validator | validator to use. | |
name | Window name. |
bool wxMediaCtrl::Create | ( | wxWindow * | parent, | |
wxWindowID | id, | |||
const wxString & | fileName = wxEmptyString , |
|||
const wxPoint & | pos = wxDefaultPosition , |
|||
const wxSize & | size = wxDefaultSize , |
|||
long | style = 0 , |
|||
const wxString & | szBackend = wxEmptyString , |
|||
const wxValidator & | validator = wxDefaultValidator , |
|||
const wxString & | name = "mediaCtrl" | |||
) |
Creates this control.
Returns false if it can't load the movie located at fileName or it cannot load one of its native backends.
If you specify a file to open via fileName and you don't specify a backend to use, wxMediaCtrl tries each of its backends until one that can render the path referred to by fileName can be found.
parent | parent of this control. Must not be NULL. | |
id | id to use for events | |
fileName | If not empty, the path of a file to open. | |
pos | Position to put control at. | |
size | Size to put the control at and to stretch movie to. | |
style | Optional styles. | |
szBackend | Name of backend you want to use, leave blank to make wxMediaCtrl figure it out. | |
validator | validator to use. | |
name | Window name. |
wxSize wxMediaCtrl::GetBestSize | ( | ) | const |
Obtains the best size relative to the original/natural size of the video, if there is any.
See Video size for more information.
Reimplemented from wxWindow.
double wxMediaCtrl::GetPlaybackrate | ( | ) |
Obtains the playback rate, or speed of the media.
1.0
represents normal speed, while 2.0
represents twice the normal speed of the media, for example. Not supported on the GStreamer (Unix) backend.
wxMediaState wxMediaCtrl::GetState | ( | ) |
Obtains the state the playback of the media is in -.
wxMEDIASTATE_STOPPED | The movie has stopped. |
wxMEDIASTATE_PAUSED | The movie is paused. |
wxMEDIASTATE_PLAYING | The movie is currently playing. |
double wxMediaCtrl::GetVolume | ( | ) |
Gets the volume of the media from a 0.0 to 1.0 range.
wxFileOffset wxMediaCtrl::Length | ( | ) |
Obtains the length - the total amount of time the movie has in milliseconds.
Loads the location that uri
refers to with the proxy proxy
.
Not implemented on most backends so it should be called with caution. Returns false if loading fails.
bool wxMediaCtrl::Load | ( | const wxURI & | uri | ) |
Loads the location that uri refers to.
Note that this is very implementation-dependant, although HTTP URI/URLs are generally supported, for example. Returns false if loading fails.
bool wxMediaCtrl::Load | ( | const wxString & | fileName | ) |
Loads the file that fileName refers to.
Returns false if loading fails.
bool wxMediaCtrl::LoadURI | ( | const wxString & | fileName | ) |
Same as Load(const wxURI& uri).
Kept for wxPython compatibility.
Same as Load(const wxURI& uri, const wxURI& proxy).
Kept for wxPython compatibility.
bool wxMediaCtrl::Pause | ( | ) |
Pauses playback of the movie.
bool wxMediaCtrl::Play | ( | ) |
Resumes playback of the movie.
wxFileOffset wxMediaCtrl::Seek | ( | wxFileOffset | where, | |
wxSeekMode | mode = wxFromStart | |||
) |
Seeks to a position within the movie.
bool wxMediaCtrl::SetPlaybackRate | ( | double | dRate | ) |
Sets the playback rate, or speed of the media, to that referred by dRate.
1.0
represents normal speed, while 2.0
represents twice the normal speed of the media, for example. Not supported on the GStreamer (Unix) backend. Returns true if successful.
bool wxMediaCtrl::SetVolume | ( | double | dVolume | ) |
Sets the volume of the media from a 0.0 to 1.0 range to that referred by dVolume
.
1.0
represents full volume, while 0.5
represents half (50 percent) volume, for example.
bool wxMediaCtrl::ShowPlayerControls | ( | wxMediaCtrlPlayerControls | flags = wxMEDIACTRLPLAYERCONTROLS_DEFAULT |
) |
A special feature to wxMediaCtrl.
Applications using native toolkits such as QuickTime usually have a scrollbar, play button, and more provided to them by the toolkit. By default wxMediaCtrl does not do this. However, on the directshow and quicktime backends you can show or hide the native controls provided by the underlying toolkit at will using ShowPlayerControls(). Simply calling the function with default parameters tells wxMediaCtrl to use the default controls provided by the toolkit. The function takes a wxMediaCtrlPlayerControls enumeration, please see available show modes there.
For more info see Player controls.
Currently only implemented on the QuickTime and DirectShow backends. The function returns true on success.
bool wxMediaCtrl::Stop | ( | ) |
Stops the media.
See Operation for an overview of how stopping works.
wxFileOffset wxMediaCtrl::Tell | ( | ) |
Obtains the current position in time within the movie in milliseconds.
![]() |
[ top ] |