无法检测到麦克风音量。

我按照指南(可以在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行返回“Unexpected symbol near ','”。

我尝试修改代码为:

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 ',')。

我该怎么做来解决这个问题?

点赞
用户2633423
用户2633423

在这一行中:

Runtime:addEventListener( “enterFrame”, soundDetector )

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

Runtime:addEventListener( "enterFrame", soundDetector )

可能是从其他来源(PDF 文档?)中复制并粘贴触发了某个自动替换。出现相同问题似乎是你发布的第一个代码片段的错误原因:

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

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

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

2013-09-13 17:14:44