Author Topic: Mini MP3 Player  (Read 5086 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Mini MP3 Player
« on: August 21, 2019, 05:52:12 pm »
Today I decided to try my hands on a mini mp3 player. I tried to see if I can make it play a whole folder but I'm not sure how to do that. I tried SHELL with SORT to make a temp .txt file of the directory. But I would have to figure out how to read that file 1 song at a time. I might try that more another time. But for now, I made this little player that plays 1 song at a time. It also can change directories. It has nothing except play. No stop, FF, rewind, pause, or anything. LOL But it could be used for something to quickly play a song or added to another game or program for a song. I'm sure many people have made stuff like this in the past, but it's my first one. :)

Code: QB64: [Select]
  1. _TITLE "Mini MP3 Player"
  2. SCREEN _NEWIMAGE(300, 300, 32)
  3. begin:
  4. PRINT "           Mini MP3 Player"
  5. PRINT "              By Ken G."
  6. PRINT "        (1) Change Directory"
  7. PRINT "        (2) Play Song"
  8. PRINT "        (3) Quit"
  9. INPUT "->", a
  10. IF a = 1 THEN GOTO directory:
  11. IF a = 2 THEN GOTO song:
  12. IF a = 3 THEN END
  13. IF a > 3 OR a < 1 OR a <> INT(a) THEN GOTO begin:
  14. directory:
  15. INPUT "Directory: ", d$
  16. PRINT "New Directory is: "; d$
  17. INPUT "Press enter to go to menu.", a$
  18. GOTO begin:
  19. song:
  20. FILES "*.mp3"
  21. INPUT "Song: ", song$
  22. s& = _SNDOPEN(song$)
  23. GOTO begin:
  24.  

« Last Edit: August 21, 2019, 05:54:10 pm by SierraKen »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Mini MP3 Player
« Reply #1 on: August 21, 2019, 06:22:46 pm »
Quote
I tried to see if I can make it play a whole folder but I'm not sure how to do that. I tried SHELL with SORT to make a temp .txt file of the directory. But I would have to figure out how to read that file 1 song at a time.

Hi Ken. Try this example, for more clearity see also to created file MyFiles.txt.
Run this in folder with some MP3 sound files.

Code: QB64: [Select]
  1. REDIM file(0) AS STRING 'create empty text array. 0 is record nr. 1!
  2. SHELL "dir *.mp3 /B > MyFiles.txt" 'create mp3 files list to file MyFiles.txt
  3.  
  4. 'now - load this files. I show you how do it.
  5. 'first for you program show for you files, which are found in current directory:
  6.  
  7. OPEN "MyFiles.txt" FOR INPUT AS #1
  8.     LINE INPUT #1, file$
  9.     PRINT file$; " is record nr."; record; "in file. I add it also to array 'file' also with this number" 'you see which MP3 file is on which position in file list MyFiles.txt
  10.     PRINT "press any key"
  11.     SLEEP
  12.     file(record) = file$ 'so we add to record 0 first file, because variable record here is zero
  13.     record = record + 1 'for next loop we needed array higher up to one
  14.     REDIM _PRESERVE file(record) AS STRING 'this do array bigger without deleting content
  15.  
  16. 'so now file(0) is first file from disk. file(3) is 4th file from disk. Try it:
  17. ready:
  18. PRINT "Input music file for playing (maximal value is"; UBOUND(file) -1; ")"
  19. INPUT rec
  20. IF rec > UBOUND(file) -1 OR rec < LBOUND(rec) THEN PRINT "Invalid record number!": GOTO ready
  21. _SNDPLAYFILE (file(rec)) 'run REC nr. record
  22.  

I'm one of those who wrote own player. It was posted on the old [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] forum. The source code is still with me if you are interested. It is 63 kilobits of source code. There was a problem with sound file names that were written with unicode characters.


Then, if you need control, if some music is now played or not, use function _SNDPLAYING for it.

If you need using all sound files in one step, use DIR *.* /B > filelist.txt. Then open this file and read last 4 characters (RIGHT$). If this are .MP3 or .WAV or .OGG, write this files to array as in my example. Is possible also playing .MID files if you use external library.
« Last Edit: August 21, 2019, 06:33:15 pm by Petr »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #2 on: August 21, 2019, 08:05:09 pm »
Thanks Petr, I understand a lot of what you did there and put it in my program. I'm going to see if I can find a way to play the whole folder though without having to type any numbers. Or maybe make it have random play on that folder. There's probably a command that says when a song is over, I will look into that. Thanks again!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #3 on: August 21, 2019, 11:16:37 pm »
Thanks to Petr's help, I was able to finish this player tonight. :) It doesn't have all the bells and whistles, but it does say the Position of the song while playing (using INT to not show so many numbers flying by LOL). You also can skip to the next song in the folder while it's playing the whole folder. The menu lets you play just 1 song or a whole directory. It also lets you change the directory to whatever you want. Although I don't suggest trying to change to the Windows 10 built-in Music folder. I know QB64 has code for that somewhere I think, but I didn't add it because I don't use that directory. If it's pretty simple I might look into it later. But please check this out. You have to know the directory name to change to, it's just 1 INPUT line that asks for the directory name. Like I said above, this is my first mp3 player I ever made. Thank you to QB64 for letting us make these.

(Note: A better version than this is below.)

Code: QB64: [Select]
  1. 'This program was made on August 21, 2019 by Ken G. with some help by Petr from the QB64.org forum.
  2. 'This program will make a temporary file called MyMusicFiles-Temp000.temp
  3. 'which is just a text file and can be opened by Notepad. It shows a list of
  4. 'the mp3 songs in that directory. The file is deleted once the music starts.
  5.  
  6. _TITLE "Mini MP3 Player"
  7. SCREEN _NEWIMAGE(400, 400, 32)
  8. begin:
  9. DIM f$(100000)
  10. record = 0
  11. rec = 0
  12. oldp = 0
  13. p = 0
  14. PRINT "                Mini MP3 Player"
  15. PRINT "                  By Ken G."
  16. PRINT "            (1) Change Directory"
  17. PRINT "            (2) Play Song"
  18. PRINT "            (3) Play Directory"
  19. PRINT "            (4) Quit"
  20. INPUT "      ->", a
  21. IF a = 1 THEN GOTO directory:
  22. IF a = 2 THEN GOTO song:
  23. IF a = 3 THEN GOTO playdir:
  24. IF a = 4 THEN END
  25. IF a > 4 OR a < 1 OR a <> INT(a) THEN GOTO begin:
  26. directory:
  27. again:
  28. INPUT "Directory: ", d$
  29. r% = _DIREXISTS(d$)
  30. IF r% <> -1 THEN PRINT "Directory doesn't exist, try again.": GOTO again:
  31. PRINT "New Directory is: "; d$
  32. INPUT "Press enter to go to menu.", a$
  33. GOTO begin:
  34. song:
  35. FILES "*.mp3"
  36. again2:
  37. INPUT "Song: ", song$
  38. fe% = _FILEEXISTS(song$)
  39. IF fe% <> -1 THEN PRINT "Filename doesn't exist, try again.": GOTO again2:
  40. s& = _SNDOPEN(song$)
  41. LOCATE 1, 1: PRINT song$
  42. LOCATE 5, 1: PRINT "Space Bar = Pause (P)lay (S)top"
  43.     a$ = INKEY$
  44.     IF a$ = CHR$(27) THEN END
  45.     IF a$ = " " THEN _SNDPAUSE s&
  46.     IF a$ = "S" OR a$ = "s" THEN _SNDSTOP s&
  47.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  48.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: GOTO begin:
  49.     oldp = p
  50.     p = _SNDGETPOS(s&)
  51.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  52.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN GOTO begin:
  53. GOTO begin:
  54. playdir:
  55. REDIM file(0) AS STRING 'create empty text array. 0 is record nr. 1!
  56. SHELL _HIDE "dir *.mp3 /B > MyMusicFiles-Temp000.temp" 'create mp3 files list.
  57. OPEN "MyMusicFiles-Temp000.temp" FOR INPUT AS #1
  58.     CLOSE #1
  59.     SHELL _HIDE "DEL MyMusicFiles-Temp000.temp"
  60.     CLS
  61.     PRINT "No mp3 songs on this folder."
  62.     PRINT
  63.     INPUT "Press Enter to go back to Menu.", menu$
  64.     GOTO begin:
  65.     LINE INPUT #1, file$
  66.     file(record) = file$ 'so we add to record 0 first file, because variable record here is zero
  67.     f$(record) = file$
  68.     PRINT f$(record)
  69.     record = record + 1 'for next loop we needed array higher up to one
  70.     REDIM _PRESERVE file(record) AS STRING 'this do array bigger without deleting content
  71. SHELL _HIDE "DEL MyMusicFiles-Temp000.temp"
  72. 'so now file(0) is first file from disk. file(3) is 4th file from disk. Try it:
  73. ready:
  74. s& = _SNDOPEN(file(rec))
  75. LOCATE 1, 1: PRINT f$(rec)
  76. LOCATE 5, 1: PRINT "Space Bar = Pause (P)lay (N)ext Song (M)enu"
  77.     a$ = INKEY$
  78.     IF a$ = CHR$(27) THEN END
  79.     IF a$ = " " THEN _SNDPAUSE s&
  80.     IF a$ = "N" OR a$ = "n" THEN _SNDSTOP s&
  81.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  82.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: rec = 0: GOTO begin:
  83.     oldp = p
  84.     p = _SNDGETPOS(s&)
  85.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  86.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN GOTO more:
  87. more:
  88. IF rec = record - 1 THEN GOTO begin:
  89. rec = rec + 1
  90. GOTO ready:
  91.  
« Last Edit: August 22, 2019, 07:54:42 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #4 on: August 21, 2019, 11:19:49 pm »
Deleted by original poster. - Not sure why this copy of the last post is here, probably on accident.
« Last Edit: August 22, 2019, 07:54:05 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #5 on: August 22, 2019, 07:51:39 pm »
I added the length of the song to compare it with the position, and also the sound rate. Plus I made it faster to switch folders by using Wiki code to automatically display what folder you are on, on the front menu page. When you change folders, you don't have to press Enter again, it just goes straight back to the Menu and shows the new folder. :) This is a pretty fun program to play with. Of course I don't need this program (I use Foobar2000 to play my music), but it feels nice to know that I finally made something that plays songs.
Here is the newest update:

Code: QB64: [Select]
  1. 'This program was made on August 21, 2019 by Ken G. with some help by Petr from the QB64.org forum.
  2. 'This program will make a temporary file called MyMusicFiles-Temp000.temp
  3. 'which is just a text file and can be opened by Notepad. It shows a list of
  4. 'the mp3 songs in that directory. The file is deleted once the music starts.
  5.  
  6. DECLARE LIBRARY 'Directory Information using KERNEL32 provided by Dav
  7.     FUNCTION CURDirectory ALIAS GetCurrentDirectoryA (BYVAL nBufferLen AS LONG, lpBuffer AS STRING)
  8.  
  9.  
  10. _TITLE "Mini MP3 Player"
  11. SCREEN _NEWIMAGE(400, 400, 32)
  12. begin:
  13. DIM f$(100000)
  14. record = 0
  15. rec = 0
  16. oldp = 0
  17. p = 0
  18. PRINT "                Mini MP3 Player"
  19. PRINT "                  By Ken G."
  20. '=== SHOW CURRENT DIRECTORY
  21. CurDir$ = SPACE$(255)
  22. Result = CURDirectory(LEN(CurDir$), CurDir$)
  23. IF Result THEN LOCATE 11, 1: PRINT "Directory: "; LEFT$(CurDir$, Result)
  24. PRINT "            (1) Change Directory"
  25. PRINT "            (2) Play Song"
  26. PRINT "            (3) Play Directory"
  27. PRINT "            (4) Quit"
  28. INPUT "      ->", a
  29. IF a = 1 THEN GOTO directory:
  30. IF a = 2 THEN GOTO song:
  31. IF a = 3 THEN GOTO playdir:
  32. IF a = 4 THEN END
  33. IF a > 4 OR a < 1 OR a <> INT(a) THEN GOTO begin:
  34. directory:
  35. again:
  36. INPUT "Directory: ", d$
  37. IF d$ = "" THEN GOTO begin:
  38. r% = _DIREXISTS(d$)
  39. IF r% <> -1 THEN
  40.     PRINT "Directory doesn't exist."
  41.     PRINT "Try again, or Enter for Menu."
  42.     GOTO again:
  43. GOTO begin:
  44. song:
  45. FILES "*.mp3"
  46. again2:
  47. INPUT "Song: ", song$
  48. IF song$ = "" THEN GOTO begin:
  49. fe% = _FILEEXISTS(song$)
  50. IF fe% <> -1 THEN
  51.     PRINT "Filename doesn't exist."
  52.     PRINT "Try again, or Enter for Menu."
  53.     GOTO again2:
  54. s& = _SNDOPEN(song$)
  55. LOCATE 1, 1: PRINT song$
  56. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  57. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  58. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (S)top (M)enu"
  59.  
  60.     a$ = INKEY$
  61.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  62.     IF a$ = " " THEN _SNDPAUSE s&
  63.     IF a$ = "S" OR a$ = "s" THEN _SNDSTOP s&
  64.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  65.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: GOTO begin:
  66.     oldp = p
  67.     p = _SNDGETPOS(s&)
  68.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  69.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN GOTO begin:
  70. GOTO begin:
  71. playdir:
  72. REDIM file(0) AS STRING 'create empty text array. 0 is record nr. 1!
  73. SHELL _HIDE "dir *.mp3 /B > MyMusicFiles-Temp000.temp" 'create mp3 files list.
  74. OPEN "MyMusicFiles-Temp000.temp" FOR INPUT AS #1
  75.     CLOSE #1
  76.     SHELL _HIDE "DEL MyMusicFiles-Temp000.temp"
  77.     CLS
  78.     PRINT "No mp3 songs on this folder."
  79.     PRINT
  80.     INPUT "Press Enter to go back to Menu.", menu$
  81.     GOTO begin:
  82.     LINE INPUT #1, file$
  83.     file(record) = file$
  84.     f$(record) = file$
  85.     PRINT f$(record)
  86.     record = record + 1 'for next loop we needed array higher up to one
  87.     REDIM _PRESERVE file(record) AS STRING 'this do array bigger without deleting content
  88. SHELL _HIDE "DEL MyMusicFiles-Temp000.temp"
  89. 'so now file(0) is first file from disk. file(3) is 4th file from disk. Try it:
  90. ready:
  91. s& = _SNDOPEN(file(rec))
  92. LOCATE 1, 1: PRINT f$(rec)
  93. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  94. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  95. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (N)ext Song (M)enu"
  96.  
  97.     a$ = INKEY$
  98.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  99.     IF a$ = " " THEN _SNDPAUSE s&
  100.     IF a$ = "N" OR a$ = "n" THEN _SNDSTOP s&
  101.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  102.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: rec = 0: GOTO begin:
  103.     oldp = p
  104.     p = _SNDGETPOS(s&)
  105.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  106.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN GOTO more:
  107. more:
  108. IF rec = record - 1 THEN GOTO begin:
  109. rec = rec + 1
  110. GOTO ready:
  111.  
  112.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #6 on: August 24, 2019, 04:40:11 pm »
Here is a much better version using B+'s folder menu when you want to play a whole folder. You choose one to change to with the mouse and then press X or Esc out to go back to the main menu and select to play the directory. Thanks B+ and Steve and Petr for all the help!

Code: QB64: [Select]
  1. 'This program was made on August 23, 2019 by Ken G. with lots of help by B+, SMcneill, and Petr from the QB64.org forum.
  2. 'This program makes 2 temporary files called MyMusicFiles.temp and DIR$INF0.INF in your default temp directory.
  3. 'If for some reason these files exist after this program is shut down, you are free to delete them.
  4.  
  5. _TITLE "Mini MP3 Player"
  6. SCREEN _NEWIMAGE(400, 400, 32)
  7. DIM SHARED tmpDir AS STRING '  establish a permanent spot for temp files
  8. IF ENVIRON$("TEMP") <> "" THEN 'Thanks to Steve McNeill use user temp files directory
  9.     tmpDir = ENVIRON$("TEMP")
  10. ELSEIF ENVIRON$("TMP") <> "" THEN
  11.     tmpDir = ENVIRON$("TMP")
  12. ELSE 'Thanks to Steve McNeill this should be very unlikely
  13.     IF _DIREXISTS("C:\temp") THEN ELSE MKDIR "C:\temp"
  14.     tmpDir = "C:\temp"
  15.  
  16. begin:
  17.     DIM f$(10000)
  18.     record = 0
  19.     rec = 0
  20.     oldp = 0
  21.     p = 0
  22.     CLS
  23.     PRINT: PRINT: PRINT
  24.     PRINT "                Mini MP3 Player"
  25.     PRINT: PRINT: PRINT
  26.     PRINT "                  By Ken G."
  27.     PRINT
  28.     LOCATE 11, 1: PRINT "Directory: "; _CWD$
  29.     PRINT: PRINT
  30.     PRINT "            (1) Change Directory"
  31.     PRINT "            (2) Play Song"
  32.     PRINT "            (3) Play Directory"
  33.     PRINT "            (4) Quit"
  34.     PRINT
  35.     PRINT
  36.     INPUT "      ->", a
  37.     IF a = 1 THEN GOSUB changedirectory:
  38.     IF a = 2 THEN GOSUB song:
  39.     IF a = 3 THEN GOSUB playdir:
  40.     IF a = 4 THEN END
  41.  
  42. 'Play One Song Here
  43.  
  44. song:
  45. FILES "*.mp3"
  46. again2:
  47. INPUT "Song: ", song$
  48. IF song$ = "" THEN RETURN
  49. fe% = _FILEEXISTS(song$)
  50. IF fe% <> -1 THEN
  51.     PRINT "Filename doesn't exist."
  52.     PRINT "Try again, or Enter for Menu."
  53.     GOTO again2:
  54. s& = _SNDOPEN(song$)
  55. LOCATE 1, 1: PRINT song$
  56. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  57. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  58. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (S)top (M)enu"
  59.  
  60.     a$ = INKEY$
  61.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  62.     IF a$ = " " THEN _SNDPAUSE s&
  63.     IF a$ = "S" OR a$ = "s" THEN _SNDSTOP s&
  64.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  65.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: RETURN
  66.     oldp = p
  67.     p = _SNDGETPOS(s&)
  68.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  69.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN RETURN
  70.  
  71. 'Play Directory Here
  72.  
  73. playdir:
  74. record = 0
  75. rec = 0
  76. REDIM file(0) AS STRING 'create empty text array. 0 is record nr. 1!
  77. SHELL _HIDE "dir *.mp3 /B > " + tmpDir + "\MyMusicFiles.temp" 'create mp3 files list.
  78. OPEN tmpDir + "\MyMusicFiles.temp" FOR INPUT AS #1
  79.     CLOSE #1
  80.     SHELL _HIDE "DEL " + tmpDir + "\MyMusicFiles.temp"
  81.     CLS
  82.     PRINT "No mp3 songs on this folder."
  83.     PRINT
  84.     INPUT "Press Enter to go back to Menu.", menu$
  85.     RETURN
  86.     LINE INPUT #1, file$
  87.     file(record) = file$
  88.     f$(record) = file$
  89.     record = record + 1 'for next loop we needed array higher up to one
  90.     REDIM _PRESERVE file(record) AS STRING 'this do array bigger without deleting content
  91. SHELL _HIDE "DEL " + tmpDir + "\MyMusicFiles.temp"
  92. 'so now file(0) is first file from disk. file(3) is 4th file from disk. Try it:
  93. ready:
  94. a$ = ""
  95. p = 0
  96. oldp = 0
  97. s& = _SNDOPEN(file(rec))
  98. LOCATE 1, 1: PRINT f$(rec)
  99. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  100. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  101. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (N)ext Song (M)enu"
  102.     a$ = INKEY$
  103.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  104.     IF a$ = " " THEN _SNDPAUSE s&
  105.     IF a$ = "N" OR a$ = "n" THEN _SNDSTOP s&
  106.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  107.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: rec = 0: RETURN
  108.     oldp = p
  109.     p = _SNDGETPOS(s&)
  110.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  111.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN EXIT DO
  112. more:
  113. IF rec = record - 1 THEN RETURN
  114. rec = rec + 1
  115. GOTO ready:
  116.  
  117.  
  118. 'Change Directory Here
  119.  
  120.  
  121. changedirectory:
  122.  
  123.  
  124. DIM mySelection&
  125.  
  126.     PRINT "Current Directory: " + _CWD$
  127.     REDIM myFiles(0) AS STRING
  128.  
  129.     loadDIR myFiles()
  130.  
  131.     mySelection& = getArrayItemNumber&(5, 2, 48, 20, myFiles())
  132.     CLS
  133.     IF mySelection& <> -1719 THEN
  134.         CHDIR myFiles(mySelection&)
  135.     ELSE
  136.         _KEYCLEAR
  137.         EXIT DO
  138.     END IF
  139.     _LIMIT 60
  140.  
  141.  
  142.  
  143. SUB loadDIR (fa() AS STRING)
  144.     DIM tmpFile AS STRING, Index%, fline$, d$
  145.     tmpFile = tmpDir + "\DIR$INF0.INF" 'aha!, not a fully pathed file to user directory but here is good!
  146.     SHELL _HIDE "DIR /a:d >" + tmpFile 'get directories  but have to do a little pruning
  147.     OPEN tmpFile FOR INPUT AS #1
  148.     Index% = -1
  149.     DO WHILE NOT EOF(1)
  150.         LINE INPUT #1, fline$
  151.         IF INSTR(fline$, "<DIR>") THEN
  152.             d$ = _TRIM$(rightOf$(fline$, "<DIR>"))
  153.             Index% = Index% + 1
  154.             REDIM _PRESERVE fa(Index%)
  155.             fa(Index%) = d$
  156.         END IF
  157.     LOOP
  158.     CLOSE #1
  159.     KILL tmpFile
  160.  
  161. FUNCTION rightOf$ (source$, of$)
  162.     IF INSTR(source$, of$) > 0 THEN rightOf$ = MID$(source$, INSTR(source$, of$) + LEN(of$))
  163.  
  164.  
  165. FUNCTION getArrayItemNumber& (locateRow, locateColumn, boxWidth, boxHeight, arr() AS STRING)
  166.  
  167.     DIM curRow AS INTEGER, curCol AS INTEGER, fg AS _UNSIGNED LONG, bg AS _UNSIGNED LONG
  168.     DIM maxWidth AS INTEGER, maxHeight AS INTEGER, page AS INTEGER, hlite AS INTEGER, mx AS INTEGER, my AS INTEGER
  169.     DIM lastMX AS INTEGER, lastMY AS INTEGER, row AS INTEGER, mb AS INTEGER
  170.     DIM lba AS LONG, uba AS LONG, choice AS LONG, kh AS LONG, index AS LONG
  171.     DIM clrStr AS STRING, b AS STRING
  172.  
  173.     'save old settings to restore at end ofsub
  174.     curRow = CSRLIN
  175.     curCol = POS(0)
  176.     fg = _DEFAULTCOLOR
  177.     bg = _BACKGROUNDCOLOR
  178.     _KEYCLEAR
  179.  
  180.     maxWidth = boxWidth '       number of characters in box
  181.     maxHeight = boxHeight - 2 ' number of lines displayed of array at one time = 1 page
  182.     lba = LBOUND(arr)
  183.     uba = UBOUND(arr)
  184.     page = 0
  185.     hlite = 0 '                 line in display ready for selection by spacebar or if no number is started, enter
  186.     clrStr$ = SPACE$(maxWidth) 'clearing a display line
  187.  
  188.     GOSUB update '              show the beginning of the array items for selection
  189.  
  190.     'signal cancel selection process, exit sub with this unlikely index to signal canel
  191.     choice = -1719 'primes 7 and 8, not likely to be a select index of an array
  192.  
  193.     DO 'until get a selection or demand exit
  194.  
  195.         'handle the key stuff
  196.         kh& = _KEYHIT
  197.         IF kh& THEN
  198.             IF kh& > 0 AND kh& < 255 THEN
  199.                 IF INSTR("0123456789", CHR$(kh&)) > 0 THEN b$ = b$ + CHR$(kh&): GOSUB update
  200.                 'IF CHR$(kh&) = "h" THEN HELP: _KEYCLEAR
  201.  
  202.                 IF CHR$(kh&) = "c" THEN b$ = "": GOSUB update
  203.                 IF kh& = 13 THEN 'enter pressed check if number is being entered?
  204.                     IF LEN(b$) THEN
  205.                         IF VAL(b$) >= lba AND VAL(b$) <= uba THEN 'we have number started
  206.                             choice = VAL(b$): EXIT DO
  207.                         ELSE 'clear b$ to show some response to enter
  208.                             b$ = "": GOSUB update 'clear the value that doesn't work
  209.                         END IF
  210.                     ELSE
  211.                         choice = hlite + page * maxHeight + lba 'must mean to select the highlighted item
  212.                     END IF
  213.                 END IF
  214.                 IF kh& = 27 THEN EXIT DO 'escape clause offered to Cancel selection process
  215.                 IF kh& = 32 THEN choice = hlite + page * maxHeight + lba 'best way to choose highlighted selection
  216.                 IF kh& = 8 THEN 'backspace to edit number
  217.                     IF LEN(b$) THEN b$ = LEFT$(b$, LEN(b$) - 1): GOSUB update
  218.                 END IF
  219.             ELSE
  220.                 SELECT CASE kh& 'choosing sections of array to display and highlighted item
  221.                     CASE 20736 'pg dn
  222.                         IF (page + 1) * maxHeight + lba <= uba THEN page = page + 1: GOSUB update
  223.                     CASE 18688 'pg up
  224.                         IF (page - 1) * maxHeight + lba >= lba THEN page = page - 1: GOSUB update
  225.                     CASE 18432 'up
  226.                         IF hlite - 1 < 0 THEN
  227.                             IF page > 0 THEN
  228.                                 page = page - 1: hlite = maxHeight - 1: GOSUB update
  229.                             END IF
  230.                         ELSE
  231.                             hlite = hlite - 1: GOSUB update
  232.                         END IF
  233.                     CASE 20480 'down
  234.                         IF (hlite + 1) + page * maxHeight + lba <= uba THEN 'ok to move up
  235.                             IF hlite + 1 > maxHeight - 1 THEN
  236.                                 page = page + 1: hlite = 0: GOSUB update
  237.                             ELSE
  238.                                 hlite = hlite + 1: GOSUB update
  239.                             END IF
  240.                         END IF
  241.                     CASE 18176 'home
  242.                         page = 0: hlite = 0: GOSUB update
  243.                     CASE 20224 ' end
  244.                         page = INT((uba - lba) / maxHeight): hlite = maxHeight - 1: GOSUB update
  245.                 END SELECT
  246.             END IF
  247.         END IF
  248.  
  249.         'handle the mouse stuff
  250.         WHILE _MOUSEINPUT
  251.             IF _MOUSEWHEEL = -1 THEN 'up?
  252.                 IF hlite - 1 < 0 THEN
  253.                     IF page > 0 THEN
  254.                         page = page - 1: hlite = maxHeight - 1: GOSUB update
  255.                     END IF
  256.                 ELSE
  257.                     hlite = hlite - 1: GOSUB update
  258.                 END IF
  259.             ELSEIF _MOUSEWHEEL = 1 THEN 'down?
  260.                 IF (hlite + 1) + page * maxHeight + lba <= uba THEN 'ok to move up
  261.                     IF hlite + 1 > maxHeight - 1 THEN
  262.                         page = page + 1: hlite = 0: GOSUB update
  263.                     ELSE
  264.                         hlite = hlite + 1: GOSUB update
  265.                     END IF
  266.                 END IF
  267.             END IF
  268.         WEND
  269.         mx = INT((_MOUSEX - locateColumn * 8) / 8) + 2: my = INT((_MOUSEY - locateRow * 16) / 16) + 2
  270.         IF _MOUSEBUTTON(1) THEN 'click contols or select array item
  271.             'clear mouse clicks
  272.             mb = _MOUSEBUTTON(1)
  273.             IF mb THEN 'clear it
  274.                 WHILE mb 'OK!
  275.                     IF _MOUSEINPUT THEN mb = _MOUSEBUTTON(1)
  276.                     _LIMIT 100
  277.                 WEND
  278.             END IF
  279.  
  280.             IF mx >= 1 AND mx <= maxWidth AND my >= 1 AND my <= maxHeight THEN
  281.                 choice = my + page * maxHeight + lba - 1 'select item clicked
  282.             ELSEIF mx >= 1 AND mx <= maxWidth AND my = 0 THEN 'page up or exit
  283.                 IF my = 0 AND (mx <= maxWidth AND mx >= maxWidth - 2) THEN 'exit sign
  284.                     EXIT DO 'escape plan for mouse click top right corner of display box
  285.                 ELSE 'PgUp bar clicked
  286.                     IF (page - 1) * maxHeight + lba >= lba THEN page = page - 1: GOSUB update
  287.                 END IF
  288.             ELSEIF mx >= 1 AND mx <= maxWidth AND my = maxHeight + 1 THEN 'page down bar clicked
  289.                 IF (page + 1) * maxHeight + lba <= uba THEN page = page + 1: GOSUB update
  290.             END IF
  291.         ELSE '   mouse over highlighting, only if mouse has moved!
  292.             IF mx >= 1 AND mx <= maxWidth AND my >= 1 AND my <= maxHeight THEN
  293.                 IF mx <> lastMX OR my <> lastMY THEN
  294.                     IF my - 1 <> hlite AND (my - 1 + page * maxHeight + lba <= uba) THEN
  295.                         hlite = my - 1
  296.                         lastMX = mx: lastMY = my
  297.                         GOSUB update
  298.                     END IF
  299.                 END IF
  300.             END IF
  301.         END IF
  302.         _LIMIT 200
  303.     LOOP UNTIL choice >= lba AND choice <= uba
  304.     getArrayItemNumber& = choice
  305.     COLOR fg, bg
  306.     'clear key presses
  307.     _KEYCLEAR
  308.     LOCATE curRow, curCol
  309.     'clear mouse clicks
  310.     mb = _MOUSEBUTTON(1)
  311.     IF mb THEN 'clear it
  312.         WHILE mb 'OK!
  313.             IF _MOUSEINPUT THEN mb = _MOUSEBUTTON(1)
  314.             _LIMIT 100
  315.         WEND
  316.     END IF
  317.     EXIT SUB
  318.  
  319.     'display of array sections and controls on screen
  320.     update:
  321.  
  322.     'fix hlite if it has dropped below last array item
  323.     WHILE hlite + page * maxHeight + lba > uba
  324.         hlite = hlite - 1
  325.     WEND
  326.  
  327.     'main display of array items at page * maxHeight (lines high)
  328.     FOR row = 0 TO maxHeight - 1
  329.         IF hlite = row THEN COLOR _RGB(200, 200, 255), _RGB32(0, 0, 88) ELSE COLOR _RGB32(0, 0, 88), _RGB(200, 200, 255)
  330.         LOCATE locateRow + row, locateColumn: PRINT clrStr$
  331.         index = row + page * maxHeight + lba
  332.         IF index >= lba AND index <= uba THEN
  333.             LOCATE locateRow + row, locateColumn
  334.             PRINT LEFT$(LTRIM$(STR$(index)) + ") " + arr(index), maxWidth)
  335.         END IF
  336.     NEXT
  337.  
  338.     'make page up and down bars to click, print PgUp / PgDn if available
  339.     COLOR _RGB32(200, 200, 255), _RGB32(0, 100, 50)
  340.     LOCATE locateRow - 1, locateColumn: PRINT SPACE$(maxWidth)
  341.     IF page <> 0 THEN LOCATE locateRow - 1, locateColumn: PRINT LEFT$(" Pg Up" + SPACE$(maxWidth), maxWidth)
  342.     LOCATE locateRow + maxHeight, locateColumn: PRINT SPACE$(maxWidth)
  343.     IF page <> INT(uba / maxHeight) THEN
  344.         LOCATE locateRow + maxHeight, locateColumn: PRINT LEFT$(" Pg Dn" + SPACE$(maxWidth), maxWidth)
  345.     END IF
  346.     'make exit sign for mouse click
  347.     COLOR _RGB32(255, 255, 255), _RGB32(200, 100, 0)
  348.     LOCATE locateRow - 1, locateColumn + maxWidth - 3
  349.     PRINT " X "
  350.  
  351.     'if a number selection has been started show it's build = b$
  352.     IF LEN(b$) THEN
  353.         COLOR _RGB(255, 255, 0), _RGB32(0, 0, 0)
  354.         LOCATE locateRow + maxHeight, locateColumn + maxWidth - LEN(b$) - 1
  355.         PRINT b$;
  356.     END IF
  357.     _DISPLAY
  358.     _LIMIT 100
  359.     RETURN
  360.  

« Last Edit: August 24, 2019, 05:17:20 pm by SierraKen »

Marked as best answer by SierraKen on August 24, 2019, 04:54:05 pm

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #7 on: August 24, 2019, 08:53:54 pm »
This version seems stable and runs good. This starts out in the Windows Default Music Folder. And you can use B+'s menu to pick a folder of songs you wish to listen to. It also lets you come back to the Windows Default Music Folder if you leave it.

Code: QB64: [Select]
  1. 'This program was made on August 23, 2019 by Ken G. with lots of help by B+, SMcneill, and Petr from the QB64.org forum.
  2. 'This program makes 2 temporary files called MyMusicFiles.temp and DIR$INF0.INF in your default temp directory.
  3. 'If for some reason these files exist after this program is shut down, you are free to delete them.
  4.  
  5. _TITLE "Mini MP3 Player"
  6. SCREEN _NEWIMAGE(400, 400, 32)
  7. DIM SHARED tmpDir AS STRING '  establish a permanent spot for temp files
  8. IF ENVIRON$("TEMP") <> "" THEN 'Thanks to Steve McNeill use user temp files directory
  9.     tmpDir = ENVIRON$("TEMP")
  10. ELSEIF ENVIRON$("TMP") <> "" THEN
  11.     tmpDir = ENVIRON$("TMP")
  12. ELSE 'Thanks to Steve McNeill this should be very unlikely
  13.     IF _DIREXISTS("C:\temp") THEN ELSE MKDIR "C:\temp"
  14.     tmpDir = "C:\temp"
  15.  
  16. GOSUB defaultdir: 'Set default folder to Windows Music Default Folder
  17.  
  18. begin:
  19.     DIM f$(10000)
  20.     record = 0
  21.     rec = 0
  22.     oldp = 0
  23.     p = 0
  24.     CLS
  25.     PRINT: PRINT: PRINT
  26.     PRINT "                Mini MP3 Player"
  27.     PRINT: PRINT: PRINT
  28.     PRINT "                  By Ken G."
  29.     PRINT
  30.     LOCATE 11, 1: PRINT "Directory: "; _CWD$
  31.     PRINT: PRINT
  32.     PRINT "(1) Change Directory"
  33.     PRINT "(2) Change Back To Windows Default Music Directory"
  34.     PRINT "(3) Play Entire Directory"
  35.     PRINT "(4) Play One Song"
  36.     PRINT "(5) Quit"
  37.     PRINT
  38.     PRINT
  39.     INPUT "->", a
  40.     IF a = 1 THEN GOSUB changedirectory:
  41.     IF a = 2 THEN GOSUB defaultdir:
  42.     IF a = 3 THEN GOSUB playdir:
  43.     IF a = 4 THEN GOSUB song:
  44.     IF a = 5 THEN END
  45.  
  46. defaultdir:
  47. CHDIR _DIR$("music")
  48.  
  49. 'Play One Song Here
  50.  
  51. song:
  52. again2:
  53. PRINT "Press only Enter to go back to Menu."
  54. INPUT "Song Name: ", song$
  55. IF song$ = "" THEN RETURN
  56. fe% = _FILEEXISTS(song$)
  57. IF fe% <> -1 THEN
  58.     PRINT "Filename doesn't exist."
  59.     PRINT "Try again, or Enter for Menu."
  60.     GOTO again2:
  61. s& = _SNDOPEN(song$)
  62. LOCATE 1, 1: PRINT song$
  63. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  64. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  65. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (S)top (M)enu"
  66.  
  67.     a$ = INKEY$
  68.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  69.     IF a$ = " " THEN _SNDPAUSE s&
  70.     IF a$ = "S" OR a$ = "s" THEN _SNDSTOP s&
  71.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  72.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: RETURN
  73.     oldp = p
  74.     p = _SNDGETPOS(s&)
  75.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  76.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN RETURN
  77.  
  78. 'Play Directory Here
  79.  
  80. playdir:
  81. record = 0
  82. rec = 0
  83. REDIM file(0) AS STRING 'create empty text array. 0 is record nr. 1!
  84. SHELL _HIDE "dir *.mp3 /B > " + tmpDir + "\MyMusicFiles.temp" 'create mp3 files list.
  85. OPEN tmpDir + "\MyMusicFiles.temp" FOR INPUT AS #1
  86.     CLOSE #1
  87.     SHELL _HIDE "DEL " + tmpDir + "\MyMusicFiles.temp"
  88.     CLS
  89.     PRINT "No mp3 songs on this folder."
  90.     PRINT
  91.     INPUT "Press Enter to go back to Menu.", menu$
  92.     RETURN
  93.     LINE INPUT #1, file$
  94.     file(record) = file$
  95.     f$(record) = file$
  96.     record = record + 1 'for next loop we needed array higher up to one
  97.     REDIM _PRESERVE file(record) AS STRING 'this do array bigger without deleting content
  98. SHELL _HIDE "DEL " + tmpDir + "\MyMusicFiles.temp"
  99. 'so now file(0) is first file from disk. file(3) is 4th file from disk. Try it:
  100. ready:
  101. a$ = ""
  102. p = 0
  103. oldp = 0
  104. s& = _SNDOPEN(file(rec))
  105. LOCATE 1, 1: PRINT f$(rec)
  106. LOCATE 2, 1: PRINT "Sound Rate: "; _SNDRATE
  107. LOCATE 4, 1: PRINT "Length: "; INT(_SNDLEN(s&))
  108. LOCATE 6, 1: PRINT "Space Bar = Pause (P)lay (N)ext Song (M)enu"
  109.     a$ = INKEY$
  110.     IF a$ = CHR$(27) THEN _SNDSTOP s&: END
  111.     IF a$ = " " THEN _SNDPAUSE s&
  112.     IF a$ = "N" OR a$ = "n" THEN _SNDSTOP s&
  113.     IF a$ = "P" OR a$ = "p" THEN _SNDPLAY s&
  114.     IF a$ = "M" OR a$ = "m" THEN _SNDSTOP s&: rec = 0: RETURN
  115.     oldp = p
  116.     p = _SNDGETPOS(s&)
  117.     LOCATE 3, 1: PRINT "Position: "; INT(p)
  118.     IF INT(oldp) > INT(p) AND INT(p) = 0 THEN EXIT DO
  119. more:
  120. IF rec = record - 1 THEN RETURN
  121. rec = rec + 1
  122. GOTO ready:
  123.  
  124.  
  125. 'Change Directory Here
  126.  
  127.  
  128. changedirectory:
  129.  
  130.  
  131. DIM mySelection&
  132.  
  133.  
  134.     PRINT "Current Directory: " + _CWD$
  135.     REDIM myFiles(0) AS STRING
  136.  
  137.     loadDIR myFiles()
  138.  
  139.     mySelection& = getArrayItemNumber&(5, 2, 48, 20, myFiles())
  140.     CLS
  141.     IF mySelection& <> -1719 THEN
  142.         CHDIR myFiles(mySelection&)
  143.     ELSE
  144.         _KEYCLEAR
  145.         EXIT DO
  146.     END IF
  147.     _LIMIT 60
  148.  
  149.  
  150.  
  151. SUB loadDIR (fa() AS STRING)
  152.     DIM tmpFile AS STRING, Index%, fline$, d$
  153.     tmpFile = tmpDir + "\DIR$INF0.INF" 'aha!, not a fully pathed file to user directory but here is good!
  154.     SHELL _HIDE "DIR /a:d >" + tmpFile 'get directories  but have to do a little pruning
  155.     OPEN tmpFile FOR INPUT AS #1
  156.     Index% = -1
  157.     DO WHILE NOT EOF(1)
  158.         LINE INPUT #1, fline$
  159.         IF INSTR(fline$, "<DIR>") THEN
  160.             d$ = _TRIM$(rightOf$(fline$, "<DIR>"))
  161.             Index% = Index% + 1
  162.             REDIM _PRESERVE fa(Index%)
  163.             fa(Index%) = d$
  164.         END IF
  165.     LOOP
  166.     CLOSE #1
  167.     KILL tmpFile
  168.  
  169. FUNCTION rightOf$ (source$, of$)
  170.     IF INSTR(source$, of$) > 0 THEN rightOf$ = MID$(source$, INSTR(source$, of$) + LEN(of$))
  171.  
  172.  
  173. FUNCTION getArrayItemNumber& (locateRow, locateColumn, boxWidth, boxHeight, arr() AS STRING)
  174.  
  175.     DIM curRow AS INTEGER, curCol AS INTEGER, fg AS _UNSIGNED LONG, bg AS _UNSIGNED LONG
  176.     DIM maxWidth AS INTEGER, maxHeight AS INTEGER, page AS INTEGER, hlite AS INTEGER, mx AS INTEGER, my AS INTEGER
  177.     DIM lastMX AS INTEGER, lastMY AS INTEGER, row AS INTEGER, mb AS INTEGER
  178.     DIM lba AS LONG, uba AS LONG, choice AS LONG, kh AS LONG, index AS LONG
  179.     DIM clrStr AS STRING, b AS STRING
  180.  
  181.     'save old settings to restore at end ofsub
  182.     curRow = CSRLIN
  183.     curCol = POS(0)
  184.     fg = _DEFAULTCOLOR
  185.     bg = _BACKGROUNDCOLOR
  186.     _KEYCLEAR
  187.  
  188.     maxWidth = boxWidth '       number of characters in box
  189.     maxHeight = boxHeight - 2 ' number of lines displayed of array at one time = 1 page
  190.     lba = LBOUND(arr)
  191.     uba = UBOUND(arr)
  192.     page = 0
  193.     hlite = 0 '                 line in display ready for selection by spacebar or if no number is started, enter
  194.     clrStr$ = SPACE$(maxWidth) 'clearing a display line
  195.  
  196.     GOSUB update '              show the beginning of the array items for selection
  197.  
  198.     'signal cancel selection process, exit sub with this unlikely index to signal canel
  199.     choice = -1719 'primes 7 and 8, not likely to be a select index of an array
  200.  
  201.     DO 'until get a selection or demand exit
  202.  
  203.         'handle the key stuff
  204.         kh& = _KEYHIT
  205.         IF kh& THEN
  206.             IF kh& > 0 AND kh& < 255 THEN
  207.                 IF INSTR("0123456789", CHR$(kh&)) > 0 THEN b$ = b$ + CHR$(kh&): GOSUB update
  208.                 'IF CHR$(kh&) = "h" THEN HELP: _KEYCLEAR
  209.  
  210.                 IF CHR$(kh&) = "c" THEN b$ = "": GOSUB update
  211.                 IF kh& = 13 THEN 'enter pressed check if number is being entered?
  212.                     IF LEN(b$) THEN
  213.                         IF VAL(b$) >= lba AND VAL(b$) <= uba THEN 'we have number started
  214.                             choice = VAL(b$): EXIT DO
  215.                         ELSE 'clear b$ to show some response to enter
  216.                             b$ = "": GOSUB update 'clear the value that doesn't work
  217.                         END IF
  218.                     ELSE
  219.                         choice = hlite + page * maxHeight + lba 'must mean to select the highlighted item
  220.                     END IF
  221.                 END IF
  222.                 IF kh& = 27 THEN EXIT DO 'escape clause offered to Cancel selection process
  223.                 IF kh& = 32 THEN choice = hlite + page * maxHeight + lba 'best way to choose highlighted selection
  224.                 IF kh& = 8 THEN 'backspace to edit number
  225.                     IF LEN(b$) THEN b$ = LEFT$(b$, LEN(b$) - 1): GOSUB update
  226.                 END IF
  227.             ELSE
  228.                 SELECT CASE kh& 'choosing sections of array to display and highlighted item
  229.                     CASE 20736 'pg dn
  230.                         IF (page + 1) * maxHeight + lba <= uba THEN page = page + 1: GOSUB update
  231.                     CASE 18688 'pg up
  232.                         IF (page - 1) * maxHeight + lba >= lba THEN page = page - 1: GOSUB update
  233.                     CASE 18432 'up
  234.                         IF hlite - 1 < 0 THEN
  235.                             IF page > 0 THEN
  236.                                 page = page - 1: hlite = maxHeight - 1: GOSUB update
  237.                             END IF
  238.                         ELSE
  239.                             hlite = hlite - 1: GOSUB update
  240.                         END IF
  241.                     CASE 20480 'down
  242.                         IF (hlite + 1) + page * maxHeight + lba <= uba THEN 'ok to move up
  243.                             IF hlite + 1 > maxHeight - 1 THEN
  244.                                 page = page + 1: hlite = 0: GOSUB update
  245.                             ELSE
  246.                                 hlite = hlite + 1: GOSUB update
  247.                             END IF
  248.                         END IF
  249.                     CASE 18176 'home
  250.                         page = 0: hlite = 0: GOSUB update
  251.                     CASE 20224 ' end
  252.                         page = INT((uba - lba) / maxHeight): hlite = maxHeight - 1: GOSUB update
  253.                 END SELECT
  254.             END IF
  255.         END IF
  256.  
  257.         'handle the mouse stuff
  258.         WHILE _MOUSEINPUT
  259.             IF _MOUSEWHEEL = -1 THEN 'up?
  260.                 IF hlite - 1 < 0 THEN
  261.                     IF page > 0 THEN
  262.                         page = page - 1: hlite = maxHeight - 1: GOSUB update
  263.                     END IF
  264.                 ELSE
  265.                     hlite = hlite - 1: GOSUB update
  266.                 END IF
  267.             ELSEIF _MOUSEWHEEL = 1 THEN 'down?
  268.                 IF (hlite + 1) + page * maxHeight + lba <= uba THEN 'ok to move up
  269.                     IF hlite + 1 > maxHeight - 1 THEN
  270.                         page = page + 1: hlite = 0: GOSUB update
  271.                     ELSE
  272.                         hlite = hlite + 1: GOSUB update
  273.                     END IF
  274.                 END IF
  275.             END IF
  276.         WEND
  277.         mx = INT((_MOUSEX - locateColumn * 8) / 8) + 2: my = INT((_MOUSEY - locateRow * 16) / 16) + 2
  278.         IF _MOUSEBUTTON(1) THEN 'click contols or select array item
  279.             'clear mouse clicks
  280.             mb = _MOUSEBUTTON(1)
  281.             IF mb THEN 'clear it
  282.                 WHILE mb 'OK!
  283.                     IF _MOUSEINPUT THEN mb = _MOUSEBUTTON(1)
  284.                     _LIMIT 100
  285.                 WEND
  286.             END IF
  287.  
  288.             IF mx >= 1 AND mx <= maxWidth AND my >= 1 AND my <= maxHeight THEN
  289.                 choice = my + page * maxHeight + lba - 1 'select item clicked
  290.             ELSEIF mx >= 1 AND mx <= maxWidth AND my = 0 THEN 'page up or exit
  291.                 IF my = 0 AND (mx <= maxWidth AND mx >= maxWidth - 2) THEN 'exit sign
  292.                     EXIT DO 'escape plan for mouse click top right corner of display box
  293.                 ELSE 'PgUp bar clicked
  294.                     IF (page - 1) * maxHeight + lba >= lba THEN page = page - 1: GOSUB update
  295.                 END IF
  296.             ELSEIF mx >= 1 AND mx <= maxWidth AND my = maxHeight + 1 THEN 'page down bar clicked
  297.                 IF (page + 1) * maxHeight + lba <= uba THEN page = page + 1: GOSUB update
  298.             END IF
  299.         ELSE '   mouse over highlighting, only if mouse has moved!
  300.             IF mx >= 1 AND mx <= maxWidth AND my >= 1 AND my <= maxHeight THEN
  301.                 IF mx <> lastMX OR my <> lastMY THEN
  302.                     IF my - 1 <> hlite AND (my - 1 + page * maxHeight + lba <= uba) THEN
  303.                         hlite = my - 1
  304.                         lastMX = mx: lastMY = my
  305.                         GOSUB update
  306.                     END IF
  307.                 END IF
  308.             END IF
  309.         END IF
  310.         _LIMIT 200
  311.     LOOP UNTIL choice >= lba AND choice <= uba
  312.     getArrayItemNumber& = choice
  313.     COLOR fg, bg
  314.     'clear key presses
  315.     _KEYCLEAR
  316.     LOCATE curRow, curCol
  317.     'clear mouse clicks
  318.     mb = _MOUSEBUTTON(1)
  319.     IF mb THEN 'clear it
  320.         WHILE mb 'OK!
  321.             IF _MOUSEINPUT THEN mb = _MOUSEBUTTON(1)
  322.             '_LIMIT 100
  323.         WEND
  324.     END IF
  325.     EXIT SUB
  326.  
  327.     'display of array sections and controls on screen
  328.     update:
  329.  
  330.     'fix hlite if it has dropped below last array item
  331.     WHILE hlite + page * maxHeight + lba > uba
  332.         hlite = hlite - 1
  333.     WEND
  334.  
  335.     'main display of array items at page * maxHeight (lines high)
  336.     FOR row = 0 TO maxHeight - 1
  337.         IF hlite = row THEN COLOR _RGB(200, 200, 255), _RGB32(0, 0, 88) ELSE COLOR _RGB32(0, 0, 88), _RGB(200, 200, 255)
  338.         LOCATE locateRow + row, locateColumn: PRINT clrStr$
  339.         index = row + page * maxHeight + lba
  340.         IF index >= lba AND index <= uba THEN
  341.             LOCATE locateRow + row, locateColumn
  342.             PRINT LEFT$(LTRIM$(STR$(index)) + ") " + arr(index), maxWidth)
  343.         END IF
  344.     NEXT
  345.  
  346.     'make page up and down bars to click, print PgUp / PgDn if available
  347.     COLOR _RGB32(200, 200, 255), _RGB32(0, 100, 50)
  348.     LOCATE locateRow - 1, locateColumn: PRINT SPACE$(maxWidth)
  349.     IF page <> 0 THEN LOCATE locateRow - 1, locateColumn: PRINT LEFT$(" Pg Up" + SPACE$(maxWidth), maxWidth)
  350.     LOCATE locateRow + maxHeight, locateColumn: PRINT SPACE$(maxWidth)
  351.     IF page <> INT(uba / maxHeight) THEN
  352.         LOCATE locateRow + maxHeight, locateColumn: PRINT LEFT$(" Pg Dn" + SPACE$(maxWidth), maxWidth)
  353.     END IF
  354.     'make exit sign for mouse click
  355.     COLOR _RGB32(255, 255, 255), _RGB32(200, 100, 0)
  356.     LOCATE locateRow - 1, locateColumn + maxWidth - 3
  357.     PRINT " X "
  358.  
  359.     'if a number selection has been started show it's build = b$
  360.     IF LEN(b$) THEN
  361.         COLOR _RGB(255, 255, 0), _RGB32(0, 0, 0)
  362.         LOCATE locateRow + maxHeight, locateColumn + maxWidth - LEN(b$) - 1
  363.         PRINT b$;
  364.     END IF
  365.     _DISPLAY
  366.     '_LIMIT 100
  367.     RETURN
  368.  
« Last Edit: August 24, 2019, 09:00:30 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Mini MP3 Player
« Reply #8 on: August 24, 2019, 10:10:28 pm »
Hi Ken,

Glad you cleared all the comments in changedDirectory GOSUB.

When I tested code, I could not read the files names that had squiggles in middle, so I don't know what to type.

We could set up the code to use same array display for files as we did for Directories so you just find the directory then press f to select file from it, no typing out file name needed, the code is in the best answer for Tiny Navigator!

I was shocked to learn your mouse scroll wheel was not working!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #9 on: August 24, 2019, 10:44:28 pm »
That sounds great! Would be awesome to add the file scrolling as well.
Yeah, as I say in the other thread, the wheel does work, just differently than what I'm used to. Sorry about that.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Mini MP3 Player
« Reply #10 on: August 24, 2019, 10:49:58 pm »
I do see one problem with the wheel, I'll put it in the other thread.