Recent

Author Topic: [SOLVED] Use one random music file  (Read 173 times)

d3crypti0n

  • New Member
  • *
  • Posts: 11
[SOLVED] Use one random music file
« on: April 21, 2021, 11:43:14 pm »
On Stackoverflow I found a code snippet to import a WAVE file in an application.
I used that code to play a song in a loop.
Is it possible to randomly pick a song out of an undefined amout of tracks?

for example now I have three songs, named after the naming scheme:
- song_01.wav
- song_02.wav
- song_03.wav
- song_0X.wav
Maybe I want to add some tracks, maybe I want to delete some.
This is my code for the music player:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.MusicPlayer();
  2. begin
  3.    RandomTrack := music; //I don't know how to randomly pick a song
  4.    PlaySound(music,0,SND_ASYNC or SND_LOOP);
  5. end;
  6.  
« Last Edit: April 24, 2021, 11:51:37 pm by d3crypti0n »

Handoko

  • Hero Member
  • *****
  • Posts: 4195
  • My goal: build my own game engine using Lazarus
Re: Use one random music file
« Reply #1 on: April 22, 2021, 05:24:38 am »
Hello d3crypti0n,
Welcome to the forum.

There are several ways to achieve what you want to do. Below are the steps to do it using the easy way:

1. Create a TStringList for storing the filenames
2. Use FindAllFiles to get the filenames and store them into the TStringList
3. Use Random to select an index from the TStringList
4. Get the filename from TStringList based on the index
5. Use PlaySound to play the filename
6. If user clicks the stop button, then exit the loop
7. If music ends and stop button hasn't clicked, repeat the loop by goto step #2
8. Free all the objects created on the memory before closing the program

How good you can use Pascal? If you're new in Lazarus/Pascal programming, here are the documentations:

1. How to use TStringList:
https://wiki.freepascal.org/TStringList-TStrings_Tutorial
https://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

2. How to use FindAllFiles:
https://wiki.freepascal.org/FindAllFiles

3. How to use Random:
https://www.freepascal.org/docs-html/rtl/system/random.html

The above steps do not support user manually adding/deleting the playlist. If you need the playlist feature, you can use a TListBox component.
https://wiki.freepascal.org/TListBox
https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tlistbox.html
« Last Edit: April 22, 2021, 05:44:59 am by Handoko »

d3crypti0n

  • New Member
  • *
  • Posts: 11
Re: Use one random music file
« Reply #2 on: April 22, 2021, 08:43:09 am »
Hello d3crypti0n,
Welcome to the forum.

There are several ways to achieve what you want to do. Below are the steps to do it using the easy way:

1. Create a TStringList for storing the filenames
2. Use FindAllFiles to get the filenames and store them into the TStringList
3. Use Random to select an index from the TStringList
4. Get the filename from TStringList based on the index
5. Use PlaySound to play the filename
6. If user clicks the stop button, then exit the loop
7. If music ends and stop button hasn't clicked, repeat the loop by goto step #2
8. Free all the objects created on the memory before closing the program

How good you can use Pascal? If you're new in Lazarus/Pascal programming, here are the documentations:

1. How to use TStringList:
https://wiki.freepascal.org/TStringList-TStrings_Tutorial
https://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

2. How to use FindAllFiles:
https://wiki.freepascal.org/FindAllFiles

3. How to use Random:
https://www.freepascal.org/docs-html/rtl/system/random.html

The above steps do not support user manually adding/deleting the playlist. If you need the playlist feature, you can use a TListBox component.
https://wiki.freepascal.org/TListBox
https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tlistbox.html

Thank you Handoko for the nice welcoming and reply to my post.
I'll take a look at it since I'm not that familiar with Pascal. If I need help again I'll commend in here with all my Code and questions, but again thank you in advance

 

TinyPortal © 2005-2018