User Tools

Site Tools


Sidebar

Multimedia

k23:k23.15:start

23.15 MediaView (gb.media.form)

This class implements a media player with simple control and can be created.

To create a new MediaView:

  Dim hMediaView As MediaView
  hMediaView = New MediaView ( Parent As Container ) [ As "EventName" ]

Alternatively, you can drag the MediaView control from the 'Media' folder in the IDE onto the form if you have previously added the gb.media.form component in the project properties.

BILD

Figure 23.15.1: MediaView as a video player

23.15.1 Properties

The MediaView class has these properties

PropertyData typeDescription
BorderBooleanThe control gets a border if the value is set to True or returns the value.
DurationFloatReturns the playing time of the medium in seconds. Note: This value remains zero until the file has been played. Simply setting the URL property does not display the media duration!
MutedBooleanThe sound is muted if the value is set to True or returns the value.
NextURLStringReturns or sets the path or the next URL of the media file to be played.
PositionFloatReturns or sets the current position within the medium in seconds.
ShowControlBooleanThe control is displayed if the value is set to True or returns the value.
SpeedFloatReturns the playback speed of the medium or sets the value. Since version 3.12.
StateIntegerReturns the status of the media player. Since version 3.19.
URLStringReturns or sets the path or URL of the media file to be played.
VolumeFloatSets the volume of the audio track to a value between 0 and 1 or returns the volume value.

Table 23.15.1.1 : Properties of the MediaView class

23.15.2 Methods

The MediaView class has these methods:

MethodDescription
Pause()The playback of the media file is paused.
Play()Playback of the media file is started.
Stop()The playback of the media file is stopped/finished.

Table 23.15.2.1 : Methods of the MediaView class

Example:

  MediaView1.URL = Media.URL("/tmp/media.mpg")
  Print MediaView1.Duration
    MediaView1.Play()
    MediaView1.Pause()
  Print MediaView1.Duration

Output in the IDE console (unit seconds):

  0
  342

23.15.2.1 Events

The MediaView class has this relevant event:

EventDescription
AboutToFinish ( )This event is triggered when the current media playback is about to end. It is triggered approximately two seconds before the end of playback of the media file.

Table 23.15.2.1.1 : Events of the MediaView class

23.15.2.2 VideoPlayer project

The project is configured so that you can play video files in different video formats.

The source code is short and is given here in full and then commented on:

  [1] ' Gambas class file
  [2]
  [3] Private $sFileName As String
  [4]
  [5] Public Sub Form_Open()
  [6]     MediaView1.Volume = 0.7
  [7]     FMain.Width = 629
  [8] End
  [9]
  [10] Public Sub btnOpenVideo_Click()
  [11]
  [12]     Dim sFilter As String
  [13]     Dim aFilter As String[]
  [14]
  [15]     Dialog.Title = ("Select a video file")
  [16]     sFilter = "*.avi;*.mp4;*.mpeg;*.mpg;*.webm;*.ogv;*.mkv"
  [17]     Dialog.Filter = [sFilter, ("Video files"), "*", ("All files")]
  [18]     Dialog.FilterIndex = 0
  [19]
  [20]     If Dialog.OpenFile(False) Then Return
  [21]
  [22]     $sFileName = File.Name(Dialog.Path)
  [23]     FMain.Caption = ("Video: ") & $sFileName
  [24]
  [25]     aFilter = ["avi", "mp4", "mpeg", "mpg", "webm", "ogv", "mkv"]
  [26]     If aFilter.Exist(File.Ext(Dialog.Path)) Then
  [27]        MediaView1.URL = Media.URL(Dialog.Path)
  [28]     Endif
  [29]
  [30] End
  [31]
  [32] Public Sub MediaView1_AboutToFinish()
  [33]     Wait 2
  [34]     FMain.Caption = ("The video « ") & $sFileName & (" » has been played")
  [35] End
  [36]
  [37] Public Sub Form_Close()
  [38]
  [39]     Dim hWindow As Window
  [40]
  [41] '-- Close all open windows
  [42]     For Each hWindow In Windows
  [43]         hWindow.Close()
  [44]     Next
  [45]
  [46] End

Comments

  • In lines 10 to 30, a video file is selected in a file selection dialogue. A filter ensures that only certain video formats are available for selection.
  • A second filter in line 25 ensures that only certain video formats can be played.
  • In line 25, the path of a local video file is converted into a URL using the (static) function Media.URL(…) and then assigned to the MediaView.URL property in line 27.
  • The MediaView control provides a toogle button and a button for playing, pausing and stopping the video.
  • If you want to play a corrupt video file - created after a faulty format conversion, for example - you will receive a (fatal) error message. At present, you cannot intercept such an error in the source code.

In the download area you will find a project archive with the above example.

Download

Chapter & Projects

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k23/k23.15/start.txt · Last modified: 11.01.2024 by emma

Page Tools