.. wxPython Phoenix documentation This file was generated by Phoenix's sphinx generator and associated tools, do not edit by hand. Copyright: (c) 2011-2020 by Total Control Software License: wxWindows License .. include:: headings.inc .. _wx.media.MediaCtrl: ========================================================================================================================================== |phoenix_title| **wx.media.MediaCtrl** ========================================================================================================================================== :ref:`wx.media.MediaCtrl` is a class for displaying various types of media, such as videos, audio files, natively through native codecs. :ref:`wx.media.MediaCtrl` uses native backends to render media, for example on Windows there is a ActiveMovie/DirectShow backend, and on Macintosh there is a QuickTime backend. |phoenix_title| Rendering media =============================== Depending upon the backend, :ref:`wx.media.MediaCtrl` can render and display pretty much any kind of media that the native system can - such as an image, mpeg video, or mp3 (without license restrictions - since it relies on native system calls that may not technically have mp3 decoding available, for example, it falls outside the realm of licensing restrictions). For general operation, all you need to do is call :meth:`~wx.media.MediaCtrl.Load` to load the file you want to render, catch the ``EVT_MEDIA_LOADED`` event, and then call :meth:`~wx.media.MediaCtrl.Play` to show the video/audio of the media in that event. More complex operations are generally more heavily dependent 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. |phoenix_title| Operation ========================= When :ref:`wx.media.MediaCtrl` plays a file, it plays until the stop position is reached (currently the end of the file/stream). Right before it hits the end of the stream, it fires off a ``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 self.Bind(wx.media.EVT_MEDIA_STOP, self.OnMediaStop, self.mediactrl) # ... def OnMediaStop(self, evt): if self.userWantsToSeek: self.mediactrl.SetPosition(someOtherPosition) evt.Veto() When :ref:`wx.media.MediaCtrl` stops, either by the ``EVT_MEDIA_STOP`` not being vetoed, or by manually calling :meth:`~wx.media.MediaCtrl.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 guaranteed 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 :ref:`wx.media.MediaCtrl` tries to keep consistent for all types of media. Note that when changing the state of the media through :meth:`~wx.media.MediaCtrl.Play` and other methods, the media may not actually be in the ``MEDIASTATE_PLAYING`` , for example. If you are relying on the media being in certain state, catch the event relevant to the state. See :ref:`wx.media.MediaEvent` for the kinds of events that you can catch. |phoenix_title| Video size ========================== By default, :ref:`wx.media.MediaCtrl` will scale the size of the video to the requested amount passed to either its constructor or :meth:`~wx.media.MediaCtrl.Create`. After calling :meth:`wx.media.MediaCtrl.Load` or performing an equivalent operation, you can subsequently obtain the "real" size of the video (if there is any) by calling :meth:`wx.media.MediaCtrl.GetBestSize` . Note that the actual result on the display will be slightly different when :meth:`wx.media.MediaCtrl.ShowPlayerControls` is activated and the actual video size will be less than specified due to the extra controls provided by the native toolkit. In addition, the backend may modify :meth:`wx.media.MediaCtrl.GetBestSize` to include the size of the extra controls - so if you want the real size of the video just disable :meth:`wx.media.MediaCtrl.ShowPlayerControls` . The idea with setting :meth:`wx.media.MediaCtrl.GetBestSize` to the size of the video is that :meth:`~wx.media.MediaCtrl.GetBestSize` is a Window-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. |phoenix_title| Player controls =============================== Normally, when you use :ref:`wx.media.MediaCtrl` it is just a window for the video to play in. However, some toolkits have their own media player interface. For example, QuickTime generally has a bar below the video with a slider. A special feature available to :ref:`wx.media.MediaCtrl`, you can use the toolkits interface instead of making your own by using the :meth:`~wx.media.MediaCtrl.ShowPlayerControls` function. There are several options for the flags parameter, with the two general flags being ``MEDIACTRLPLAYERCONTROLS_NONE`` which turns off the native interface, and ``MEDIACTRLPLAYERCONTROLS_DEFAULT`` which lets :ref:`wx.media.MediaCtrl` decide what native controls on the interface. Be sure to review the caveats outlined in :ref:`Video size ` before doing so. |phoenix_title| Choosing a backend ================================== Generally, you should almost certainly leave this part up to :ref:`wx.media.MediaCtrl` - but if you need a certain backend for a particular reason, such as QuickTime for playing .mov files, all you need to do to choose a specific backend is to pass the name of the backend class to :meth:`wx.media.MediaCtrl.Create` . The following are valid backend identifiers: - ``MEDIABACKEND_DIRECTSHOW``: Use ActiveMovie/DirectShow. Uses the native ActiveMovie (I.E. DirectShow) control. Default backend on Windows and supported by nearly all Windows versions. May display a windows media player logo while inactive. - ``MEDIABACKEND_QUICKTIME``: Use QuickTime. Mac Only. ``WARNING``: May not work correctly when embedded in a :ref:`wx.Notebook`. - ``MEDIABACKEND_GSTREAMER``, Use GStreamer. Unix Only. Requires GStreamer 0.10 along with at the very least the xvimagesink, xoverlay and gst-play modules of gstreamer to function. You need the correct modules to play the relevant files, for example the mad module to play mp3s, etc. - ``MEDIABACKEND_WMP10``, Use Windows Media Player 10 (Windows only). Works on systems with either Windows Media Player 9 or 10 installed. |phoenix_title| Creating a backend ================================== .. _MediaCtrl-styles: |styles| Window Styles ================================ Creating a backend for :ref:`wx.media.MediaCtrl` is a rather simple process. Simply derive from MediaBackendCommonBase and implement the methods you want. The methods in MediaBackend correspond to those in :ref:`wx.media.MediaCtrl` except for `MediaCtrl.CreateControl` which does the actual creation of the control, in cases where a custom control is not needed you may simply call :meth:`wx.Control.Create` . 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 ``FORCE_LINK_MODULE`` usage in the mediactrl sample). There is a rather simple example of how to create a backend in the `ActiveXContainer` documentation. ^^ This class supports the following styles: - ``wx.media.MC_NO_AUTORESIZE``: By default, the control will automatically adjust its size to exactly fit the size of a loaded video as soon as a video is loaded. If this flag is given, the control will not change its size automatically and it must be done manually (if desired) using :meth:`~wx.media.MediaCtrl.Layout`. It is strongly recommended to use this flag and handle control resizing manually (note that this style is only available in wxWidgets 3.1.6, so it is only possible to do it when using this or later version). ^^ .. seealso:: :ref:`wx.media.MediaEvent` | |class_hierarchy| Class Hierarchy ================================= .. raw:: html
Inheritance diagram for class MediaCtrl:
| |method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~wx.media.MediaCtrl.__init__` Default constructor - you ``MUST`` call :meth:`~MediaCtrl.Create` before calling any other methods of :ref:`wx.media.MediaCtrl`. :meth:`~wx.media.MediaCtrl.Create` Creates this control. :meth:`~wx.media.MediaCtrl.GetBestSize` Obtains the best size relative to the original/natural size of the video, if there is any. :meth:`~wx.media.MediaCtrl.GetPlaybackRate` Obtains the playback rate, or speed of the media. :meth:`~wx.media.MediaCtrl.GetState` Obtains the state the playback of the media is in. :meth:`~wx.media.MediaCtrl.GetVolume` Gets the volume of the media from a 0.0 to 1.0 range. :meth:`~wx.media.MediaCtrl.Length` Obtains the length - the total amount of time the media has in milliseconds. :meth:`~wx.media.MediaCtrl.Load` Loads the file that fileName refers to. :meth:`~wx.media.MediaCtrl.LoadURI` Loads the location that uri refers to. :meth:`~wx.media.MediaCtrl.LoadURIWithProxy` Loads the location that ``uri`` refers to with the proxy ``proxy`` . :meth:`~wx.media.MediaCtrl.Pause` Pauses playback of the media. :meth:`~wx.media.MediaCtrl.Play` Resumes playback of the media. :meth:`~wx.media.MediaCtrl.Seek` Seeks to a position within the media. :meth:`~wx.media.MediaCtrl.SetPlaybackRate` Sets the playback rate, or speed of the media, to that referred by `dRate`. :meth:`~wx.media.MediaCtrl.SetVolume` Sets the volume of the media from a 0.0 to 1.0 range to that referred by ``dVolume`` . :meth:`~wx.media.MediaCtrl.ShowPlayerControls` A special feature to :ref:`wx.media.MediaCtrl`. :meth:`~wx.media.MediaCtrl.Stop` Stops the media. :meth:`~wx.media.MediaCtrl.Tell` Obtains the current position in time within the media in milliseconds. ================================================================================ ================================================================================ | |property_summary| Properties Summary ===================================== ================================================================================ ================================================================================ :attr:`~wx.media.MediaCtrl.BestSize` See :meth:`~wx.media.MediaCtrl.GetBestSize` :attr:`~wx.media.MediaCtrl.PlaybackRate` See :meth:`~wx.media.MediaCtrl.GetPlaybackRate` and :meth:`~wx.media.MediaCtrl.SetPlaybackRate` :attr:`~wx.media.MediaCtrl.State` See :meth:`~wx.media.MediaCtrl.GetState` :attr:`~wx.media.MediaCtrl.Volume` See :meth:`~wx.media.MediaCtrl.GetVolume` and :meth:`~wx.media.MediaCtrl.SetVolume` ================================================================================ ================================================================================ | |api| Class API =============== .. class:: wx.media.MediaCtrl(Control) **Possible constructors**:: MediaCtrl() MediaCtrl(parent, id=-1, fileName="", pos=DefaultPosition, size=DefaultSize, style=0, szBackend="", validator=DefaultValidator, name="mediaCtrl") MediaCtrl is a class for displaying various types of media, such as videos, audio files, natively through native codecs. .. method:: __init__(self, *args, **kw) |overload| Overloaded Implementations: :html:`

` **__init__** `(self)` Default constructor - you ``MUST`` call :meth:`Create` before calling any other methods of :ref:`wx.media.MediaCtrl`. :html:`

` **__init__** `(self, parent, id=-1, fileName="", pos=DefaultPosition, size=DefaultSize, style=0, szBackend="", validator=DefaultValidator, name="mediaCtrl")` Constructor that calls :meth:`Create` . You may prefer to call :meth:`Create` directly to check to see if :ref:`wx.media.MediaCtrl` is available on the system. :param `parent`: parent of this control. Must not be ``None``. :type `parent`: wx.Window :param `id`: id to use for events :type `id`: wx.WindowID :param `fileName`: If not empty, the path of a file to open. :type `fileName`: string :param `pos`: Position to put control at. :type `pos`: wx.Point :param `size`: Size to put the control at and to stretch movie to. :type `size`: wx.Size :param `style`: Optional styles. It is recommended to use ``MC_NO_AUTORESIZE``, although it is not used by default for compatibility reasons. :type `style`: long :param `szBackend`: Name of backend you want to use, leave blank to make :ref:`wx.media.MediaCtrl` figure it out. :type `szBackend`: string :param `validator`: validator to use. :type `validator`: wx.Validator :param `name`: Window name. :type `name`: string :html:`

` .. method:: Create(self, parent, id=-1, fileName="", pos=DefaultPosition, size=DefaultSize, style=0, szBackend="", validator=DefaultValidator, name="mediaCtrl") Creates this control. Returns ``False`` if it can't load the media located at `fileName` or it can't create a backend. If you specify a file to open via `fileName` and you don't specify a backend to use, :ref:`wx.media.MediaCtrl` tries each of its backends until one that can render the path referred to by `fileName` can be found. :param `parent`: parent of this control. Must not be ``None``. :type `parent`: wx.Window :param `id`: id to use for events :type `id`: wx.WindowID :param `fileName`: If not empty, the path of a file to open. :type `fileName`: string :param `pos`: Position to put control at. :type `pos`: wx.Point :param `size`: Size to put the control at and to stretch movie to. :type `size`: wx.Size :param `style`: Optional styles. It is recommended to use ``MC_NO_AUTORESIZE``, although it is not used by default for compatibility reasons. :type `style`: long :param `szBackend`: Name of backend you want to use, leave blank to make :ref:`wx.media.MediaCtrl` figure it out. :type `szBackend`: string :param `validator`: validator to use. :type `validator`: wx.Validator :param `name`: Window name. :type `name`: string :rtype: `bool` .. method:: GetBestSize(self) Obtains the best size relative to the original/natural size of the video, if there is any. See :ref:`Video size ` for more information. :rtype: `Size` .. method:: GetPlaybackRate(self) 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. :rtype: `float` :returns: zero on failure. .. method:: GetState(self) Obtains the state the playback of the media is in. ================================== ================================== ``wx.media.MEDIASTATE_STOPPED`` The media has stopped. ``wx.media.MEDIASTATE_PAUSED`` The media is paused. ``wx.media.MEDIASTATE_PLAYING`` The media is currently playing. ================================== ================================== | :rtype: :ref:`wx.media.MediaState` .. method:: GetVolume(self) Gets the volume of the media from a 0.0 to 1.0 range. :rtype: `float` .. note:: Due to rounding and other errors the value returned may not be the exact value sent to :meth:`SetVolume` . .. method:: Length(self) Obtains the length - the total amount of time the media has in milliseconds. :rtype: `wx.FileOffset` .. method:: Load(self, fileName) Loads the file that fileName refers to. Returns ``False`` if loading fails. :param `fileName`: :type `fileName`: string :rtype: `bool` .. method:: LoadURI(self, uri) Loads the location that uri refers to. Note that this is very implementation-dependent, although ``HTTP`` URI/URLs are generally supported, for example. Returns ``False`` if loading fails. :param `uri`: :type `uri`: string :rtype: `bool` .. method:: LoadURIWithProxy(self, uri, proxy) 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. :param `uri`: :type `uri`: string :param `proxy`: :type `proxy`: string :rtype: `bool` .. method:: Pause(self) Pauses playback of the media. :rtype: `bool` .. method:: Play(self) Resumes playback of the media. :rtype: `bool` .. method:: Seek(self, where, mode=FromStart) Seeks to a position within the media. :param `where`: :type `where`: wx.FileOffset :param `mode`: :type `mode`: wx.SeekMode :rtype: `wx.FileOffset` .. todo:: Document the SeekMode parameter `mode`, and perhaps also the FileOffset and SeekMode themselves. .. method:: SetPlaybackRate(self, 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. :param `dRate`: :type `dRate`: float :rtype: `bool` .. method:: SetVolume(self, 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. :param `dVolume`: :type `dVolume`: float :rtype: `bool` .. note:: The volume may not be exact due to conversion and rounding errors, although setting the volume to full or none is always exact. Returns ``True`` if successful. .. method:: ShowPlayerControls(self, flags=MEDIACTRLPLAYERCONTROLS_DEFAULT) A special feature to :ref:`wx.media.MediaCtrl`. Applications using native toolkits such as QuickTime usually have a scrollbar, play button, and more provided to them by the toolkit. By default :ref:`wx.media.MediaCtrl` 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 :meth:`ShowPlayerControls` . Simply calling the function with default parameters tells :ref:`wx.media.MediaCtrl` to use the default controls provided by the toolkit. The function takes a MediaCtrlPlayerControls enumeration, please see available show modes there. For more info see :ref:`Player controls `. Currently only implemented on the QuickTime and DirectShow backends. The function returns ``True`` on success. :param `flags`: :type `flags`: wx.media.MediaCtrlPlayerControls :rtype: `bool` .. method:: Stop(self) Stops the media. See `Operation` for an overview of how stopping works. :rtype: `bool` .. method:: Tell(self) Obtains the current position in time within the media in milliseconds. :rtype: `wx.FileOffset` .. attribute:: BestSize See :meth:`~wx.media.MediaCtrl.GetBestSize` .. attribute:: PlaybackRate See :meth:`~wx.media.MediaCtrl.GetPlaybackRate` and :meth:`~wx.media.MediaCtrl.SetPlaybackRate` .. attribute:: State See :meth:`~wx.media.MediaCtrl.GetState` .. attribute:: Volume See :meth:`~wx.media.MediaCtrl.GetVolume` and :meth:`~wx.media.MediaCtrl.SetVolume`