Lua extensions: playlist randomization extension crashes VLC
I have been posting on the VideoLan forums for about a month now working with others on using LUA extensions to accomplish this task and I was referred to submitting a ticket here.
Basically, what is needed is an option for Random (without repeat).
How is this different from the existing Random?
The existing Random button picks files at random in the playlist, but it often plays the same files more than once before playing other files at all.
Thus, I, and others are trying to figure out a way where a simple array can be written that flags each file as it is played in a playlist. For example, playlist has 10 files. The Random button is pressed (perhaps this is an additional option called random-norepeat) and loop-all is pressed so it plays indefinitely.
It needs to randomize the playlist, then play that random order until all 10 files have been played. Upon 10 files being played, it re-randomizes the playlist and plays it again, it does this indefinitely.
Or an array could be made where it randomly picks file in the playlist, when it does so it puts a flag in memory or a file indicating that file has been played. Every time it goes to pick another file from the playlist it checks to see if it has been flagged, if it has it selects another file until it finds one that is either not flagged, or if ALL files in playlist have been flagged it resets the flags and starts over (indicating all files have been played once.) Obviously if it finds the file it selected has not been flagged as played-once it will play it.
Here is the LUA script we've worked with trying to get it to do this:
-- "New Random with Loop all.lua" -- VLC Extension
first_ID = 6
items = 10
random_order={3,5,8,2,9,4,7,1,10,6}
function descriptor()
return {
title = "New Random with Loop all",
capabilities = {"input-listener", "meta-listener"}
}
end
function activate()
create_dialog()
end
function deactivate()
end
function close()
vlc.deactivate()
end
k=0
function input_changed()
state = vlc.var.get(vlc.object.input(), "state")
--vlc.msg.info("["..descriptor().title.."] Input state = "..state)
if state==4 then
k=k+1
if k>#random_order then
k=1
New_Random()
end
vlc.playlist.goto(random_order[k]+first_ID-2)
--vlc.msg.info("["..descriptor().title.."] ".. k ": goto(" .. random_order[k] .. "+"..first_ID.."-2)")
order=""
for ix, vx in ipairs(random_order) do
if ix==k then vx="<b>["..vx.."]</b>" end
order=order..vx..", "
end
w8:set_text(order)
w:update()
end
end
function meta_changed()
end
----- Dialog box: -----
function create_dialog()
w = vlc.dialog(descriptor().title)
w1 = w:add_label("ID of the first item:", 1, 1)
w2 = w:add_text_input(first_ID, 2, 1)
w3 = w:add_button("Check ID:",click_CheckID, 3, 1)
w4 = w:add_label("Number of items:", 1, 2)
w5 = w:add_text_input(items, 2, 2)
w6 = w:add_label("Random order", 1, 3, 2)
w7 = w:add_button("START",click_Start, 3, 3)
w8 = w:add_html("", 1, 4, 3)
end
function click_CheckID()
w3:set_text("Check ID: " .. vlc.playlist.current())
end
function click_Start()
k=0 -- playback reset
New_Random()
order = ""
for _, v in ipairs(random_order) do
order = order..v..", "
end
w8:set_text(order)
end
----- Generator of random order: -----
i=0
function New_Random()
i=i+1
first_ID = w2:get_text()
items = w5:get_text()
random_order = {}
for j=1,items do
random_order[j] = j
end
-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)
random_order = shuffle(random_order)
w6:set_text("Random order #"..i)
end
function shuffle(t)
local n = #t
while n > 2 do
-- n is now the last pertinent index
local m = math.random(n) -- 1 <= m <= n
-- Quick swap
t[n], t[m] = t[m], t[n]
n = n - 1
end
return t
end
This LUA code causes VLC 2.0.7 to crash (stop responding) (windows 7 x64) very quickly.
Any help, or perhaps implementation of this in the source code as an option would be appreciated by myself and many others.
Thank you for your time,