music playlist
Posted: 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! 

Questions and answers about the LiveCode platform.
https://www.forums.livecode.com/
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: 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