Search code examples
actionscript-3flashactionscript

Actionscript 3: Error #1009


I want to test and write if the microphone access was allowed or not in ActionScript 3 but now, ever if there is no compilation error, it doesn't ask me the microphone access, nothings happens when I launch the SWF file.

This is my code :

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.");
    }
}

Solution

  • Your error comes from this line :

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

    because Microphone.getMicrophone() can 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).

    So to avoid that error, you can use a simple if statement :

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

    You can also use Microphone.names.length to verify if you have an Microphone installed (at least one) before creating a Microphone object :

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

    Edit :

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

    To display the Flash Player Microphone Settings panel, you can use :

    Security.showSettings(SecurityPanel.MICROPHONE);
    

    Hope that can help.