I'm trying to make a simple extension that works in the following manner:
1. User presses button and playlist is shuffled and begins playing
2. At the end of the last playlist item, the playlist is shuffled again
3. Playing loops to the start without stopping.
I'd like to loop instead of calling playlist.stop() and playlist.start() because stop brings you back to the playlist UI.
My code is here:
Code: Select all
function descriptor()
return {
title = "Shuffle Playlist",
version = "1.1.1",
shortdesc = "Shuffle Playlist",
description = "Shuffles playlists once they are finished",
author = "Jonathan Mackenzie",
capabilities = {"playing-listener", "meta-listener", "input-listener"}
}
end
function activate()
vlc.playlist.loop('on')
resetOn = nil
shuf()
vlc.playlist.play()
return true
end
function deactivate()
vlc.playlist.loop('off')
vlc.playlist.stop()
vlc.deactivate()
return true
end
function close()
vlc.deactivate()
end
function input_changed()
-- related to capabilities={"input-listener"} in descriptor()
-- triggered by Start/Stop media input event
vlc.msg.dbg("[Shuffle] input changed currentId: " ..vlc.playlist.current())
end
function meta_changed()
-- related to capabilities={"meta-listener"} in descriptor()
-- triggered by available media input meta data?
vlc.msg.dbg("[Shuffle] meta changed currentId : " .. vlc.playlist.current())
if(vlc.playlist.current()==resetOn) then
shuf()
end
end
function playing_changed()
vlc.msg.dbg("[Shuffle] playing changed currentId: "..vlc.playlist.current())
end
function shuf()
math.randomseed(os.time())
local pl = vlc.playlist.get("normal", false)
local randomList = {}
for i, v in ipairs(pl.children) do
if v.duration == -1 then v.duration = 0 end
randomList[#randomList + 1] = v
end
local swap
for i = 1, #randomList do
swap = math.random(#randomList)
randomList[i], randomList[swap] = randomList[swap], randomList[i]
end
vlc.playlist.clear()
vlc.playlist.enqueue(randomList)
vlc.playlist.goto(randomList[1].id)
resetOn = randomList[#randomList].id
vlc.msg.dbg("[Shuffle] Playlist shuffled! Reset on id: ".. resetOn)
return true
end