【问题标题】:Unable to detect microphone volume无法检测麦克风音量
【发布时间】:2013-09-18 09:47:36
【问题描述】:

我按照指南(可在http://sree.cc/corona-sdk/detect-microphone-volume-blowing-into-microphone 找到)尝试检测麦克风音量。

我使用的代码是:

local _w = display.contentWidth
local _h = display.contentHeight


local background_ = display.newRect(0,0,_w,_h)
background_:setFillColor(255)
local text_ = display.newText(“Initial…”,200,10,nil,30)
text_:setTextColor(0)

local r = media.newRecording()
r:startRecording()
r:startTuner()

function soundDetector( event )
local v = r:getTunerVolume()
if v == 0 then
return
end

v = 20 * 0.301 * math.log(v)
m = v*10
if(m>= -50)then
text_.text = “High…”
background_:setFillColor(255,0,0)
elseif(m< -50 and m>-100)then
text_.text = “Medium…”
background_:setFillColor(0,0,255)
else
text_.text = “Low…”
background_:setFillColor(0,255,0)
end
end


Runtime:addEventListener( “enterFrame”, soundDetector )

问题是控制台在第 7 行返回“',' 附近的意外符号”。

我尝试修改以下代码:

local _w = display.contentWidth
local _h = display.contentHeight

local background_ = display.newRect(0,0,_w,_h)
background_:setFillColor(255)




local r = media.newRecording()
r:startRecording()
r:startTuner()


function soundDetector( event )
local v = r:getTunerVolume()
if v == 0 then
return
end


v = 20 * 0.301 * math.log(v)
m = v*10

if(m>= -50)then

background_:setFillColor(255,0,0)
elseif(m< -50 and m>-100)then

background_:setFillColor(0,0,255)
else

background_:setFillColor(0,255,0)
end

end


Runtime:addEventListener( “enterFrame”, soundDetector )

但随后控制台在最后一行 ("Runtime:addEventListener("enterFrame", soundDetector)") 处返回相同的错误 (Unexpected symbol near ',')

我可以做些什么来解决这个问题?

【问题讨论】:

    标签: iphone ios lua coronasdk


    【解决方案1】:

    行内:

    Runtime:addEventListener( “enterFrame”, soundDetector )
    

    enterFrame 周围的引号不是 ASCII 双引号。正确的行应该是:

    Runtime:addEventListener( "enterFrame", soundDetector )
    

    可能从另一个来源(PDF 文档?)剪切和粘贴在某处触发了自动替换。同样的问题似乎是您发布的第一个 sn-p 中的错误原因:

    local text_ = display.newText(“Initial…”,200,10,nil,30)
    

    对比:

    local text_ = display.newText("Initial…",200,10,nil,30)
    

    P.S.:你的代码格式很糟糕。

    【讨论】:

    • 非常感谢!它终于可以工作了,对于可怕的代码格式感到抱歉
    • @luaLover 如果对你有帮助,请考虑接受我的回答。
    猜你喜欢
    • 2011-09-05
    • 2015-11-26
    • 2023-04-02
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多