Share via


Small Basic Sample: Music Player

This article is about a sample Small Basic program named Music Player.


Overview

Music Player is using a GraphicsWindow but basically text base program. Usage is to hit arrow keys as follows. And alpha numeric keys to skip files/directories. This program shows .wav, .mp3, .wma, and .m4a files only and plays them.

  • Right - next file
  • Left - previous file
  • Down - child directory
  • Up - parent directory

Source Code

Full source code of Music Player is listed below, because this program uses a lot of File operations. Topics of this program are:

  • File_StartFile subroutine uses Sound.Play() and Sound.Stop() operations to play audio files.
  • Subroutines started with File_ support to find files with arrow keys.
' Music Player
' Version 0.1
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-04-19
 
GraphicsWindow.Title = "Music Player 0.1"
Form()
While "True"
  If keyDown Then
    key = GraphicsWindow.LastKey
    If key =  "Left" Then
      File_Previous()
    ElseIf key =  "Right" Then
      File_Next()
    ElseIf key =  "Down" Then
      File_Child()
    ElseIf key =  "Up" Then
      File_Parent()
    ElseIf key =  "Escape" Then
      Program.End()
    ElseIf Text.IsSubText(an, key)  Then
      If Text.StartsWith(key, "D")  And (Text.GetLength(key) = 2) Then
        key = Text.GetSubTextToEnd(key, 2)
      ElseIf Text.StartsWith(key, "NumPad")  And (Text.GetLength(key) = 7) Then
        key = Text.GetSubTextToEnd(key, 7)
      EndIf
      File_Skip()
    EndIf
    keyDown = "False"
  Else
    Program.Delay(200)
  EndIf
EndWhile
 
Sub Form
  gw = 598
  gh = 428
  GraphicsWindow.Width = gw
  GraphicsWindow.Height = gh
  GraphicsWindow.BackgroundColor = "#333333"
  exts = "1=mp3;2=wma;3=wav;4=m4a;"
  File_Init()
  File_GetFiles()
  GraphicsWindow.BrushColor = "White"
  txtDir = Shapes.AddText(cd)
  Shapes.Move(txtDir, 10,  10)
  txtFile = Shapes.AddText(cf)
  Shapes.Move(txtFile, 10,  30)
  txtSize = Shapes.AddText(size)
  Shapes.Move(txtSize, gw -  100, 30)
  GraphicsWindow.KeyDown = OnKeyDown
EndSub
 
Sub OnKeyDown
  keyDown = "True"
EndSub
 
Sub File_Child
  If 0 <  iDirs Then
    If dirs[iDirs] = ".."  Then
      File_Parent()
    Else
      If Text.EndsWith(cd, "\")  Then
        cd = cd  + dirs[iDirs]
      Else
        cd = cd  + "\" + dirs[iDirs]
      EndIf
      Shapes.SetText(txtDir, cd)
      child = ""
      File_GetFiles()
    EndIf
  EndIf
EndSub
 
Sub File_GetFiles
  ' param child - child directory
  ' param cd - current directory
  ' return nDirs - number of items in dirs array
  ' return nFiles - number of items in files array
  ' return cf - current file
  files = File.GetFiles(cd)
  n = Array.GetItemCount(files)
  For i =  1 To n
    len = Text.GetLength(files[i]) 
    ext = Text.ConvertToLowerCase(Text.GetSubTextToEnd(files[i], len -  2))
    If Array.ContainsValue(exts, ext)  Then
      For p =  len To 1 Step -1
        If Text.GetSubText(files[i], p, 1) = "\"  Then
          files[i] = Text.GetSubTextToEnd(files[i], p +  1)
          p = 1  ' exit For
        EndIf
      EndFor
    Else
      files[i] = ""  ' not image file
    EndIf
  EndFor
  nFiles = Array.GetItemCount(files)
  index = Array.GetAllIndices(files)
  dirs = File.GetDirectories(cd)
  n = Array.GetItemCount(dirs)
  For i =  1 To n
    len = Text.GetLength(dirs[i])
    For p =  len To 1 Step -1
      If Text.GetSubText(dirs[i], p, 1) = "\"  Then
        dirs[i] = Text.GetSubTextToEnd(dirs[i], p +  1)
        If child =  dirs[i] Then
          iDirs = i  ' index for child directory
        EndIf
        p = 1  ' exit For
      EndIf
    EndFor
  EndFor
  If Text.IsSubText(cd, "\")  Then
    dirs[Array.GetItemCount(dirs) + 1] = ".."
  EndIf
  nDirs = Array.GetItemCount(dirs)
  If child <> "" Then
    Ifiles = 0
    cf = "["  + dirs[iDirs] + "]"
    size = ""
  ElseIf 0 <  nFiles Then
    Ifiles = 1
    iDirs = 0
    cf = files[index[Ifiles]]
  Else
    iDirs = 1
    Ifiles = 0
    cf = "["  + dirs[iDirs] + "]"
    size = ""
  EndIf
  File_StartFile()
EndSub
 
Sub File_GetLetter
  If Text.StartsWith(cf, "[")  Then
    letter = Text.GetSubText(cf, 2, 1)
  Else
    letter = Text.GetSubText(cf, 1, 1)
  EndIf
  letter = Text.ConvertToUpperCase(letter)
EndSub
 
Sub File_Init
  Not = "False=True;True=False;"
  alpha = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z"
  an = alpha
  For i =  0 To 9
    an = an  + "|D" + i +  "|NumPad" +  i
  EndFor
  cd = Program.Directory  ' current directory
  child = ""
EndSub
 
Sub File_Next
  If 0 <  Ifiles And Ifiles < nFiles  Or iDirs = nDirs And  0 < nFiles Then
    ' file, last directory
    iDirs = 0
    Ifiles = Ifiles + 1
    cf = files[index[Ifiles]]
  ElseIf iDirs <  nDirs Then
    ' directory
    Ifiles = 0
    iDirs = iDirs  + 1
    cf = "["  + dirs[iDirs] + "]"
  ElseIf nFiles =  0 And 1 < nDirs  And iDirs = nDirs Then
    ' no files, last directory
    iDirs = 1
    cf = "["  + dirs[iDirs] + "]"
  EndIf
  File_StartFile()
EndSub
 
Sub File_Parent
  len = Text.GetLength(cd)
  pSlash = len
  For p =  len To 1 Step -1
    If Text.GetSubText(cd, p, 1) = "\"  Then
      pSlash = p
      p = 1  ' exit For
    EndIf
  EndFor
  If pSlash <  len Then
    child = Text.GetSubTextToEnd(cd, pSlash +  1)
    If Text.GetSubText(cd, pSlash -  1, 1) = ":"  Then
      cd = Text.GetSubText(cd, 1, pSlash)
    Else
      cd = Text.GetSubText(cd, 1, pSlash - 1)
    EndIf
    Shapes.SetText(txtDir, cd)
    File_GetFiles()
  EndIf
EndSub
 
Sub File_Previous
  If iDirs =  1 And 0 < nFiles  Then
    iDirs = 0
    Ifiles = nFiles
    cf = files[index[Ifiles]]
  ElseIf 1 <  Ifiles Then
    Ifiles = Ifiles - 1
    cf = files[index[Ifiles]]
  ElseIf Ifiles = 1  Or (Ifiles = 0  And iDirs = 1 And  1 < nDirs) Then
    Ifiles = 0
    iDirs = nDirs
    cf = "["  + dirs[iDirs] + "]"
  ElseIf 1 <  iDirs Or 0 < nFiles  Then
    Ifiles = 0
    iDirs = iDirs  - 1
    cf = "["  + dirs[iDirs] + "]"
  EndIf
  File_StartFile()
EndSub
 
Sub File_Skip
  ' param key - to skip
  lf = cf       ' last file
  nf = "False"  ' not found
  File_GetLetter()
  While (letter  <> key) And (Not[nf])
    File_Next()
    If lf =  cf Then
      nf = "True"
    Else
      File_GetLetter()
    EndIf
  EndWhile
EndSub
 
Sub File_StartFile
  ' param cf - current file
  GraphicsWindow.BrushColor = "#111111"
  GraphicsWindow.FillRectangle(0, 0, gw, gh)
  If Text.EndsWith(cf, "]")  Then 
    size = ""
  Else
    Sound.Stop(path)
    path = cd  + "\" + cf
    Sound.Play(path)
  EndIf
  Shapes.SetText(txtFile, cf)
  Shapes.SetText(txtSize, size)
EndSub
 

See Also