A Simple VLC Controller (Windows only)

Share your advanced PureBasic knowledge/code with the community.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

A Simple VLC Controller (Windows only)

Post by TI-994A »

Here's a small utility that uses the Windows API SendMessage() function to control the basic playback functionality of VLC.

Assumes that VLC is installed in the default x86 Program Files folder; please amend if different:

Code: Select all

;===========================================================
;   Simple VLC Controller (through Windows API functions)
;   
;   tested on Windows 8.1 & 10 with PureBasic v5.50 (x64)
;
;   by TI-994A - free to use, improve, share...
;
;   30th August 2016
;===========================================================

Global hVLC

Procedure findWindow(hWnd, lParam)
  Protected winTitle.s{513}
  GetWindowText_(hWnd, @winTitle, 512)
  If FindString(winTitle, "vlc media player", 
                0, #PB_String_NoCase)
    hVLC = hWnd
  EndIf
  ProcedureReturn #True
EndProcedure

Procedure findVLC()
  EnumWindows_(@findWindow(), 0)  
EndProcedure

Procedure nextVLC()
  SendMessage_(hVLC, #WM_KEYDOWN, #VK_N, 0)
  Delay(250)
  SendMessage_(hVLC, #WM_KEYUP, #VK_N, 0)  
EndProcedure

Procedure previousVLC()
  SendMessage_(hVLC, #WM_KEYDOWN, #VK_P, 0)
  Delay(250)
  SendMessage_(hVLC, #WM_KEYUP, #VK_P, 0)  
EndProcedure

Procedure pauseVLC()
  SendMessage_(hVLC, #WM_KEYDOWN, #VK_SPACE, 0)
  Delay(250)
  SendMessage_(hVLC, #WM_KEYUP, #VK_SPACE, 0)  
EndProcedure

Procedure stopVLC()
  SendMessage_(hVLC, #WM_KEYDOWN, #VK_S, 0)
  Delay(250)
  SendMessage_(hVLC, #WM_KEYUP, #VK_S, 0)  
EndProcedure

;multiple video files selectable (from the same folder)
fileName.s = OpenFileRequester("Select Media File:", "", "All files (*.*)|*.*", 0, 
                               #PB_Requester_MultiSelection)
If fileName <> ""
  
  filePath.s = GetPathPart(fileName)
  
  While filename
    multiFile$ + " " + GetFilePart(fileName)
    fileName = NextSelectedFileName()
  Wend
  
  ;assumes that VLC is installed in the default folder - please amend if different
  RunProgram("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe", multiFile$, filePath)

  ;allow VLC time to start
  Delay(2000)
  
  ;find VLC window
  findVLC()
  
  If hVLC
    wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
    window = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 375, 50, "VLC Controller", wFlags)
    vlcPause = ButtonGadget(#PB_Any, 10, 10, 100, 30, "PLAY/PAUSE")
    vlcStop = ButtonGadget(#PB_Any, 120, 10, 75, 30, "STOP")
    vlcNext = ButtonGadget(#PB_Any, 205, 10, 75, 30, "NEXT")
    vlcPrev = ButtonGadget(#PB_Any, 290, 10, 75, 30, "PREVIOUS")
    BindGadgetEvent(vlcStop, @stopVLC())
    BindGadgetEvent(vlcPause, @pauseVLC())
    BindGadgetEvent(vlcPrev, @previousVLC())
    BindGadgetEvent(vlcNext, @nextVLC())
    While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend 
  Else
    MessageRequester("VLC Controller", "VLC window not found.")
  EndIf
  
EndIf
Might come in handy. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: A Simple VLC Controller (Windows only)

Post by Joris »

Nice.

And if I understand your code well, it's also a nice example on how to control programs by there default keystroke-commands.

Thanks.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: A Simple VLC Controller (Windows only)

Post by TI-994A »

Joris wrote:...it's also a nice example on how to control programs by there default keystroke-commands.
Exactly; much simpler than trying to identify individual UI controls.

Thank you for your feedback, Joris.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
RamRat
User
User
Posts: 20
Joined: Mon Nov 14, 2016 9:58 am
Location: Oz

Re: A Simple VLC Controller (Windows only)

Post by RamRat »

Sorry but i'm a bit slow and retarded....


The code works fine with one word filenames like "Frequency.S01E01.Pilot" but if I select "Frequency.S01E02.Signal and Noise"
it wont work, something to do with spaces between the words in the filename causing vlc to spit the dummy?

I just checked and when run by itself vlc opens the file "Frequency.S01E02.Signal and Noise" just fine so is the filename not
being passed to vlc properly?

File reading failed:
VLC could not open the file "C:\Frequency.S01E02.Signal" (Bad file descriptor).
Your input can't be opened:
VLC is unable to open the MRL 'file:///C:/Frequency.S01E02.Signal'. Check the log for details.
File reading failed:
VLC could not open the file "C:\and" (Bad file descriptor).
Your input can't be opened:
VLC is unable to open the MRL 'file:///C:/and'. Check the log for details.
File reading failed:
VLC could not open the file "C:\Noise.mkv" (Bad file descriptor).
Your input can't be opened:
VLC is unable to open the MRL 'file:///C:/Noise.mkv'. Check the log for details.
:shock:
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A Simple VLC Controller (Windows only)

Post by infratec »

Hi,

try

Code: Select all

#DQUOTES
around the filename parameters.

Bernd
RamRat
User
User
Posts: 20
Joined: Mon Nov 14, 2016 9:58 am
Location: Oz

Re: A Simple VLC Controller (Windows only)

Post by RamRat »

Um I did this -> RunProgram("C:\Program Files\VideoLAN\VLC\vlc.exe", #DQUOTE$ + multiFile$ + #DQUOTE$, filePath)


but vlc still returns with....

File reading failed:
VLC could not open the file "C:\ Frequency.S01E02.Signal and Noise.mkv" (Bad file descriptor).
Your input can't be opened:
VLC is unable to open the MRL 'file:///C:/%20Frequency.S01E02.Signal%20and%20Noise.mkv'. Check the log for details.
:shock:
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A Simple VLC Controller (Windows only)

Post by infratec »

:mrgreen:

Not around all parameters, arround all filename parameters.
So that each filename is included in #DQUOTES$.
Else the program can not decide if it is an own parameter or is it a new one separated with a space.

To test if it could work call vlc directly with:

VLC "Film 1.mp4" "Film 2.avi"

Bernd
Post Reply