Table of Contents

23.11 MediaPlayer projects

This chapter presents two tried and tested MediaPlayer projects. The first programme allows you to play audio files in selected formats, while the second programme presents a video player for common video formats. Both projects use the MediaPlayer class.

23.11.1 Project 1 - Audio player

The Audio Player project has the following functionality:

AUDIO

Figure 23.11.1.1: Audio player programme interface

The media tags are only displayed if this information is available in the data stream. In the tag event, the information is read into the cMetaData collection and later displayed in a special window area in the ShowMetaData() procedure:

Private cMetaData As New Collection
 
Public Sub hMPlayer_Tag(tagList As MediaTagList)
 
    Dim sTag As String
 
'-- Read selected metadata and save in a collection
    For Each sTag In tagList.Tags
        If sTag = "title" Or sTag = "artist" Or sTag = "album" Then
           If Not cMetaData.Exist(sTag) Then
              cMetaData[Upper(sTag)] = tagList[sTag]
           Endif
        Endif
    Next
 
 End

The visualisation of the dynamics of the piece of music enhances the program visually and is implemented in a DrawingArea via these procedures:

Private Sub ShowVisualisation()
 
    Dim mcVisualisation As MediaControl
    Dim aVType As String[] = ["libvisual_infinite", "monoscope", "libvisual_jess",
                             "libvisual_jakdaw", "goom", "libvisual_oinksie", "libvisual_bumpscope",
                             "spacescope", "spectrascope", "synaescope", "wavescope"]
 
'-- Try because distros may not support it
    Try mcVisualisation = New MediaControl(hMPlayer, aVType[4])
 
    If Not Error Then
       hMPlayer.Video.Output = New MediaControl(hMPlayer, "ximagesink")
       hMPlayer.SetWindow(dwaVisualisation)
       hMPlayer.Video.Visualisation = mcVisualisation
       sErrorVisualisation = ""
    Else
      sErrorVisualisation = "No visualisation available!"
      dwaVisualisation.Refresh()
    Endif
 
End
 
Public Sub dwaVisualisation_Draw()
 
  If Not sErrorVisualisation Then
     hImage = Image.Load(Application.Path &/ "icons/ft.jpg")
     hImage = hImage.Stretch(dwaVisualisation.W, dwaVisualisation.H)
     Paint.DrawImage(hImage, 0, 0, dwaVisualisation.W, dwaVisualisation.H)
  Else
     Paint.DrawText(sErrorVisualisation, 0, 20)
  Endif
 
End

You can define the type of visualisation of the dynamics statically from a set of types:

'-- Try because distros may not support it
    Try mcVisualisation = New MediaControl(hMPlayer, aVType[4])

23.11.2 Project 2 - Video player

The video player project has the following functionality:

v1 v2

Figure 23.11.2.1: Video player programme interface

There is a special feature in the video player presented here when displaying a video. Regardless of the original format, the video is always automatically rotated so that it is displayed correctly. The need to rotate the video was demonstrated, for example, when recording video from a mobile phone.

After a selection dialogue, the output of the Video-MediaPlayer is connected to the MediaContainer in the private procedure Start() and this is used instead of the standard output. The flip method used is 'automatic' from the set of methods, which always resulted in a correct display:

Private Enum none = 0, clockwise, rotate_180, counterclockwise, horizontal_flip, vertical_flip,
             upper_left_diagonal, upper_right_diagonal, automatic
 
Private MContainer As MediaContainer
Private MCFlip As MediaControl
Private MCSink As MediaControl
 
Public Sub btnFileOpen_Click()
 
    Dialog.Title = ("Select a video file ...")
    Dialog.Filter = ["*.avi;*.mp4;*.webm;*.mov;*.mkv;*.3gp;*.ts", "Video files", "*", "All files"]
    Dialog.Path = Desktop.GetDirectory("VIDEOS") & "/"
    Dialog.ShowHidden = False
 
    If Dialog.OpenFile(False) Then Return
 
    Me.Text = "Video: " & File.Name(Dialog.Path)
 
    Start()
 
End
 
Private Sub Start()
 
    MPlayer = Null
 
'-- The media-objects have to be newly generated before playing because of videoflip,
'-- which only works correctly one time after initialization.
    MPlayer = New MediaPlayer As "MPlayer"
    MContainer = New MediaContainer(MPlayer)
    MCFlip = New MediaControl(MContainer, "videoflip")
    MCFlip["method"] = automatic
    MCSink = New MediaControl(MContainer, "xvimagesink")
    MCFlip.LinkTo(MCSink)
    MContainer.AddInput(MCFlip, "sink")
 
'-- Connect output of mediaPlayer with MediaContainer and use it instead of the default output
    MPlayer.Video.Output = MContainer
 
    MPlayer.URL = Media.URL(Dialog.Path)
    MCSink.SetWindow(DrawingArea1)
    Slider1.Value = 0
 
    Try MPlayer.Play()
    If Error Then
       Message.Error("An error occured.\nCan't play selected video file.")
       Return
    Else
       ...
    Endif
 
End

Both projects are available as source code archives in the download area.

Download