The simulator silently crashes when reusing the same samplePlayer and playing different range.
Resetting the sampleplayer in between calls of SetRange fixes the issues (
uncomment self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample
in the code below)
To briefly explain the call below:
I am loading a sample and want to play the first section, then the second, etc. until I reached the last section. Not resetting/reloading the sample player, it freezes after sample 2.
-- main.lua
local arrTimeRange = {1, 20552, 38934, 60933, 82268}
local songPlayer = SongPlayer("sound/french", arrTimeRange)
-- SongPlayer.lua
local pd <const> = playdate
class("SongPlayer").extends()
function SongPlayer:init(pathSample, arrTimeRange)
SongPlayer.super.init(self)
print("Start init")
self.arrTimeRange = arrTimeRange
self.idxSound = 1
self.pathSample = pathSample
self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample)
self:playSound()
end
function SongPlayer:playSound()
print("Play sample ", self.idxSound)
-- self.sampleplayer = pd.sound.sampleplayer.new(self.pathSample)
if self.idxSound == #self.arrTimeRange then print("END") return end -- Nothing to play anymore
self.sampleplayer:setPlayRange(self.arrTimeRange[self.idxSound], self.arrTimeRange[self.idxSound+1])
self.sampleplayer:setFinishCallback(function ()
self.idxSound += 1
self:playSound()
end)
-- self.sampleplayer:play(1, math.random(3, 17) / 10)
self.sampleplayer:play(1)
end