music playlist

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
Samuele
Posts: 21
Joined: Mon Oct 11, 2021 7:05 pm

music playlist

Post by Samuele » Wed Oct 13, 2021 7:25 pm

hi, how can i make a code that plays a song after the other, for example it played "song1" and i want that when "song1" is finished he starts playing "song2" ecc. ? thanks! :D

xAction
Posts: 80
Joined: Sun Oct 03, 2021 4:14 am

Re: music playlist

Post by xAction » Thu Oct 14, 2021 12:28 am

you need some local variables at the top of our script
Put this stuff in a button script for testing purposes:

Code: Select all

local lastTime=-1 -- you'll use this to check if the first song has reached the end of its time
local currentPlayer  -- could be a number or it could be the name of the player object, you decide how you want to do it
local delayCheck -- use this to set lastTime to the curenTime of the active player object, don't check every second or the numbers will be the same
local songList 

Code: Select all

on MouseUp
ask file "which song?"  --// get a music file
if it is not empty then
 PlaySong it
--// lets get the rest of the files in that folder where the first song is
buildSongList it
end if
end MouseUp
[/code]

--// this will build a list of the songs in the folder with full paths

Code: Select all

on buildSongList filePath 
set itemDel to "/"
put item 1 to -2 of filePath into tFolder
set the folder to tFolder
put files() into tFiles
repeat for each line tSong  in tFiles
put cr &  tFolder & "/" & tSong   after songList
end repeat
sort songList
filter songList without empty
end buildSongList

Code: Select all

on playSong filePath
--// now set up athe music player and make it play
put "MyMusicPlayer" into currentPlayer  ---//whatever you want to name it, could be dynamic
if exists (player currentPlayer  ) is false then create player  currentPlayer  
set the left of player  currentPlayer   to the right of me
set the playLoudness of player  "currentPlayer   to 80
set the showControls of player  currentPlayer   to true
set the fileName of of player  currentPlayer   to filePath
set the currentTime of player  currentPlayer   to 0
start player  currentPlayer  
put -1 into lastTime 
idlePlay   --// now the looping to check if the song has stalled begins
end playSong
[code]

you'll want an "idle" script running every second or so to check if the first song has reached the end

[code]on idlePlay
if delayCheck < 3 then
add 1 to delayCheck
else
put 0 into delayCheck
UpdateMusic
end if
--// if script is in a button then
send idlePlay to me in 1 second
--// if script is in a stack then...
--send idlePlay to stack (the mainStack of this stack) in 1 second
end idlePlay

Code: Select all

on UpdateMusic
put the currentTime of player currentPlayer into cTime
if cTime is lastTime then
stop player currentPlayer
set the fileName of player currentPlayer to line 2 of songList --// 2 for example you can use variables
set the currentTime of player currentPlayer to 0
start player currentPlayer
put -1 into last time --// that was we are not starting at the same time as the currentTime of the player
end if
put the currentTime of currentPlayer into lastTime
end UpdateMusic
So essentially every 3 seconds lastTime is updated to the currentTime of player currentPlayer
if currenTime and lastTime are the same for 3 seconds (in this example) , load the next song.
You can shorten that delayCheck to 2 seconds, I think one second would give you the same time everytime.

Might be bugs in this forum script, good luck.


Study this stack, use any code you like.
MusicLoader.zip
Loads and plays music.
(4.92 KiB) Downloaded 9 times
Oh it creates a player object for every file in the folder as soon as you choose a folder. Then it hides them all, and shows/plays the current player. Loading music can stall things like games so I went with this 'load 'em all first' technique.

Samuele
Posts: 21
Joined: Mon Oct 11, 2021 7:05 pm

Re: music playlist

Post by Samuele » Thu Oct 14, 2021 8:49 am

thank you; it's a little complicated, but i will try to understand it :wink:

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”