wxRuby Documentation Home

# Wx::MediaCtrl

MediaCtrl is a class for displaying types of media, such as videos,
audio files, natively through native codecs. 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.

## See also

MediaEvent

## Derived from

Control

## Methods

- Rendering media
- Operation
- Video size
- Player controls
- Choosing a backend
- Creating a backend
- MediaCtrl.new
- MediaCtrl#create
- MediaCtrl#get_best_size
- MediaCtrl#get_playback_rate
- MediaCtrl#get_volume
- MediaCtrl#get_state
- MediaCtrl#is_paused
- MediaCtrl#is_playing
- MediaCtrl#is_stopped
- MediaCtrl#length
- MediaCtrl#load
- MediaCtrl#load
- MediaCtrl#load
- MediaCtrl#load_uri
- MediaCtrl#load_uri_with_proxy
- MediaCtrl#pause
- MediaCtrl#play
- MediaCtrl#seek
- MediaCtrl#set_playback_rate
- MediaCtrl#set_volume
- MediaCtrl#show_player_controls
- MediaCtrl#stop
- MediaCtrl#tell

### Rendering media

Depending upon the backend, 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
MediaCtrl#load to load the file
you want to render, catch the EVT_MEDIA_LOADED event,
and then call MediaCtrl#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.

### Operation

When 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:

1. connect to the media event
   evt_media_stop mediactrl, :on_media_stop

def on_media_stop(evt) if user_wants_to_seek mediactrl.position = mediactrl.duration << 1 evt.veto end end

When MediaCtrl stops, either by the EVT_MEDIA_STOP not being vetoed, or
by manually calling 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 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 MediaCtrl 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 MEDIASTATE_PLAYING, for example. If you are relying on the media
being in certain state catch the event relevant to the state. See
MediaEvent for the kinds of events that you can catch.

### Video size

By default, MediaCtrl will scale the size of the video to the requested
amount passed to its constructor. After calling load
or performing an equivalent operation, you can subsequently obtain the
“real” size of the video (if there is any) by calling
get_best_size. Note that the actual result on
the display will be slightly different when
show_player_controls is activated and
the actual video size will be less then specified due to the extra
controls provided by the native toolkit. In addition, the backend may
modify get_best_size() to include the size of the extra controls – so if
you want the real size of the video just disable
show_player_controls.

The idea with setting `get_best_size` to the size of the video is that
`get_best_size` is a function derived from Window 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 it’s
original size without any extra assistance needed from the user.

### Player controls

Normally, when you use 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 MediaCtrl, you can use the
toolkit’s interface instead of making your own by using the
show_player_controls method.
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 MediaCtrl
decide what native controls on the interface.
h3(#MediaCtrl_wxmediactrl). MediaCtrl.new

MediaCtrl new(%(arg-type)Window% parent, Integer id, String fileName = "", Point pos = DEFAULT_POSITION, Size size = DEFAULT_SIZE, Integer style = 0, String backend = "", Validator validator = DEFAULT_VALIDATOR, String name = PanelNameStr )

Creates this control. Returns `false` if it can’t load the movie
located at `fileName` or it cannot load one of its native backends. Note
that the `backend` argument is ignore.

If you specify a file to open via `fileName` and you don’t specify a
backend to use, MediaCtrl 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.
- backend Not used
- validator validator to use.
- name Window name.

### MediaCtrl#get_best_size

Size get_best_size()

Obtains the best size relative to the original/natural size of the
video, if there is any. See Video size for more
information.

### MediaCtrl#get_playback_rate

Float get_playbackrate()

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.
Returns 0 on failure.

### MediaCtrl#get_volume

double get_volume()

Gets the volume of the media from a 0.0 to 1.0 range. Note that due to rounding
and other errors this may not be the exact value sent to SetVolume.

### MediaCtrl#get_state

Integer get_state()

Obtains the state the playback of the media is in; the return value will
be one of the following constants:

Wx::MEDIASTATE_STOPPED | The movie has stopped.
Wx::MEDIASTATE_PAUSED | The movie is paused.
Wx::MEDIASTATE_PLAYING | The movie is currently playing.

### MediaCtrl#is_paused

Boolean is_paused() Returns `true` if media playback is currently paused, else `false`.

### MediaCtrl#is_playing

Boolean is_playing()

Returns `true` if the media is currently playing, else `false`.

### MediaCtrl#is_stopped

Boolean is_stopped()

Returns `true` if media playback is currently stopped, else `false`.

### MediaCtrl#length

Integer length()

Obtains the length – the total amount of time the movie has in milliseconds.

### MediaCtrl#load

Boolean load(%(arg-type)String% fileName)

Loads the file that `fileName` refers to. Returns false if loading fails.

### MediaCtrl#load

Boolean load(%(arg-type)URI% 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.

### MediaCtrl#load

Boolean load(%(arg-type)URI% uri, 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.

### MediaCtrl#load_uri

Boolean load_uri(%(arg-type)URI% uri)

Same as Load. Kept for Python compatability.

### MediaCtrl#load_uri_with_proxy

Boolean load_uri_with_proxy(%(arg-type)URI% uri, URI proxy)

Same as Load. Kept for Python compatability.

### MediaCtrl#pause

Boolean pause()

Pauses playback of the movie.

### MediaCtrl#play

Boolean play()

Resumes playback of the movie.

### MediaCtrl#seek

Integer seek(%(arg-type)FileOffset% where, SeekMode mode)

Seeks to a position within the movie.

### MediaCtrl#set_playback_rate

Boolean set_playback_rate(%(arg-type)Float% 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.

### MediaCtrl#set_volume

Boolean set_volume(%(arg-type)Float% 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. Note that this 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.

### MediaCtrl#show_player_controls

Boolean show_player_controls(%(arg-type)MediaCtrlPlayerControls% flags = MEDIACTRLPLAYERCONTROLS_DEFAULT)

A special feature to MediaCtrl. Applications using native toolkits such as
QuickTime usually have a scrollbar, play button, and more provided to
them by the toolkit. By default 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 ShowPlayerControls. Simply
calling the function with default parameters tells MediaCtrl to use the
default controls provided by the toolkit. The function takes a
`MediaCtrlPlayerControls` enumeration as follows:

MEDIACTRLPLAYERCONTROLS_NONE | No controls. return MediaCtrl to it’s default state.
MEDIACTRLPLAYERCONTROLS_STEP | Step controls like fastfoward, step one frame etc.
MEDIACTRLPLAYERCONTROLS_VOLUME | Volume controls like the speaker icon, volume slider, etc.
MEDIACTRLPLAYERCONTROLS_DEFAULT | Default controls for the toolkit. Currently a typedef for MEDIACTRLPLAYERCONTROLS_STEP and MEDIACTRLPLAYERCONTROLS_VOLUME.

For more see Player controls. Currently
only implemented on the QuickTime and DirectShow backends. The function
returns true on success.

### MediaCtrl#stop

Boolean stop()

Stops the media.

See Operation for an overview of how
stopping works.

### MediaCtrl#tell

Integer tell()

Obtains the current position in time within the movie in milliseconds.

[This page automatically generated from the Textile source at 2023-06-09 00:45:28 +0000]