动作脚本 3:错误 #1009

Actionscript 3: Error #1009

我想测试和编写 ActionScript 3 中是否允许麦克风访问,但是现在,如果没有编译错误,它不会询问我麦克风访问,当我启动 SWF 时什么也没有发生文件。

这是我的代码:

import flash.display.MovieClip;
import flash.events.StatusEvent;
import flash.media.Microphone;


var mic:Microphone = Microphone.getMicrophone();

if(mic){
    mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
}

else{
    trace("No micro");
}

function onMicStatus(event: StatusEvent): void {
    if (event.code == "Microphone.Unmuted") {
        trace("Microphone access was allowed.");

    } else if (event.code == "Microphone.Muted") {
    trace("Microphone access was denied.");
    }
}

你的错误来自这一行:

mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);

因为 Microphone.getMicrophone() 可以 return null :

If Microphone.getMicrophone() returns null, either the microphone is in use by another application, or there are no microphones installed on the system. To determine whether any microphones are installed, use Microphone.names.length (Microphone without "s", there is an error on Adobe's doc).

因此,为了避免该错误,您可以使用一个简单的 if 语句:

if(mic){
    mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
}

在创建 Microphone 对象之前,您还可以使用 Microphone.names.length 验证是否安装了麦克风(至少一个):

if(Microphone.names.length > 0){
    var mic:Microphone = Microphone.getMicrophone();
        mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
}

编辑:

To display the Flash Player Microphone Settings panel, which lets the user choose the microphone to be referenced by Microphone.getMicrophone, use Security.showSettings().

要显示 Flash Player 麦克风设置面板,您可以使用:

Security.showSettings(SecurityPanel.MICROPHONE);

希望能帮到你。