我试图在一个函数中填充一个表,然后使用它在以后的调用中收到的值,在使用后删除每个值。由于某种原因,这些值不会在函数调用之间保持不变。保存句柄的表应在subscribeToService()
调用时填充,然后在unsubscribeToService()
.
local subscriptionSignals_AudioMixerManager = {"volumeLevel", "muteStatus", "fadeLevel", "balanceLevel", "bassLevel", "trebleLevel", "midLevel", "AVCLevel", "activeAudioSrc", "interruptSrc"}
local signalHandlers_AudioMixerManager = {["volumeLevel"] = OnSignal, ["muteStatus"] = OnSignal, ["fadeLevel"] = onSignal, ["balanceLevel"] = OnSignal, ["bassLevel"] = OnSignal, ["trebleLevel"] = OnSignal, ["midLevel"] = OnSignal, ["AVCLevel"] = OnSignal, ["activeAudioSrc"] = OnSignal, ["interruptSrc"] = OnSignal}
local subscriptionHandles_AudioMixerManager = {}
local subscriptionSignals_AudioManager = {"targetSource"}
local signalHandlers_AudioManager = {["targetSource"] = onAudioSourceChange}
local subscriptionHandles_AudioManager = {}
local function subscribeToService(objectPath, signalNames, signalHandlers, subscriptionHandles)
print("Subscribing...")
for i,v in ipairs(signalNames) do
subscriptionHandles[v] = service.subscribe(objectPath, v, signalHandlers[v]);
print(v .. " handle: ")
print(subscriptionHandles[v])
end
print("Done subscribing")
end
local function unsubscribeFromService(subscriptionHandles)
print("Unsubscribing...")
for i,v in ipairs(subscriptionHandles) do
print("Entered for loop")
service.unsubscribe(v)
print(v)
subscriptionHandles[i] = nil
end
print("Done unsubscribing")
end
local function subscribe()
subscribeToService(AudioMixerManager, subscriptionSignals_AudioMixerManager, signalHandlers_AudioMixerManager, subscriptionHandles_AudioMixerManager)
subscribeToService(AudioManager, subscriptionSignals_AudioManager, signalHandlers_AudioManager, subscriptionHandles_AudioManager)
local result, error = service.invoke(AudioMixerManager, "registerMuteClient", {serviceName="audioSettings-interrupt"})
muteClientID = result.id
result, error = service.invoke(AudioMixerManager, "registerMuteClient", {serviceName="audioSettings-modechange"})
muteClientID2 = result.id
end
local function unsubscribe()
unsubscribeFromService(subscriptionHandles_AudioMixerManager);
unsubscribeFromService(subscriptionHandles_AudioManager);
end
我在被调用时得到以下输出subscribe()
,然后是unsubscribe()
Subscribing...
volumeLevel handle:
userdata: 18f418
muteStatus handle:
userdata: 18f490
fadeLevel handle:
userdata: 18f508
balanceLevel handle:
userdata: 18f580
bassLevel handle:
userdata: 18f5f8
trebleLevel handle:
userdata: 18f670
midLevel handle:
userdata: 18f6e8
AVCLevel handle:
userdata: 18f760
activeAudioSrc handle:
userdata: 18f7d8
interruptSrc handle:
userdata: 18f850
Done subscribing
Subscribing...
targetSource handle:
userdata: 18f8c8
Done subscribing
Unsubscribing...
Done unsubscribing
Unsubscribing...
Done unsubscribing
看起来它甚至没有进入 for 循环unsubscribeFromService()
,我认为这意味着它正在传递的表由于某种原因是空的。是否有过在 Lua 中不会通过引用传递表的情况?它可以解释这种行为,因为我已经分解了较小的组件并单独测试它们并且它们似乎正在工作。