可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm having a few problems detecting whether a microphone is detected or not. I'm running the function Microphone.getMicrophone()
and that should return null if there is no microphone attached, or if the user has clicked Deny on the security panel, right?
The problem I'm facing is, on some computers where there is no microphone installed, Microphone.getMicrophone()
still traces out as [object Microphone]
.
So say for example the user doesn't have a microphone, and clicks allow in the security panel, I can't validate whether to switch to different controls.
If anybody can shed some light on how to detect if there is no microphone connected, then I'm all ears.
Many thanks in advance, Will
回答1:
var mic:Microphone = Microphone.getMicrophone(); try { micName = mic.name trace("mic.name "+mic.name) } catch (e:Error) { trace("no mic detected") }
回答2:
Well, i would recommend to make mic test upon connection, record 1-2 seconds from user and send it back to server for evaluation, send it back to user and make some button for user to approve if he heard the sound. This is what Skype do, you can add some fancy controllers to have proper noise threshold level and in/out volume and such on the same test screen.
I'm not sure but even if getMicrophone() returns false/non existent device it cannot record it properly anyway.
回答3:
you can check the mic.activityLevel property to check if there is any mic level
also, if the computer has multiple mics, you can iterate amic in flash.media.Microphone.names to check each level.
回答4:
I would advise to check flash.media.Microphone.names
and see if it is empty.
回答5:
sample code to test a microphone
var micIndex:String = null;//whatever mic you want to target 0,1,2 var _activityLevels:Array=[]; var _mic:Microphone = MicrophoneProvider.getMicrophone(micIndex); var _testPassed:Boolean=false; _mic.setLoopBack(true); setTimeout(_timedOut, TIMEOUT_MS);//to not run forever _checkActivity(); function _checkActivity():void{ if (_mic) { var level:Number = _mic.activityLevel; trace("MIC _checkActivity", level, _activityLevels); if (level>0 && level != _activityLevels[_activityLevels.length - 1]) _activityLevels.push(level); if (_activityLevels.length < 3 && !_testTimedOut) setTimeout(_checkActivity, 100); else{ _destroy(); _testPassed=true; //your mic is detected and working } } } function _timedOut():void{ _testTimedOut = true; _destroy(); } function _destroy():void{ if (_mic) _mic.setLoopBack(false); _mic=null; }