Actionscript 3: Error #1009 - actionscript-3

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

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.

Related

AS3 using Workers

IDE: FLASH CS6 Professional
SDK: AIR 18 for Android
I'm trying to use Workers for my app.
The problem is, it doesn't work that it suppose to be.
public class Main extends Sprite {
private var worker:Worker;
public function Main() {
var ba:ByteArray = this.loaderInfo.bytes;
if(Worker.current.isPrimordial){
worker = WorkerDomain.current.createWorker(ba);
worker.start();
trace("Created Main");
}else{
trace("Created Worker");
}
}
}
It should output
Created Main
Created Worker
but i'm only getting
[SWF] Test.swf - 2831 bytes after decompression
Created Main
[UnloadSWF] Test.swf
Test Movie terminated.
EDIT:
Ok i tried to publish the app and ran it. It works like it should be. But why doesn't it work when i try to ran it with Adobe Debug Launcher ?
This is what it looks like on Android Debug Launcher (ADL)
ADL
This is what it looks like when Published.
Published
EDIT 2:
Tried it with Flash Professional CC 2015. Upgraded to AIR SDK 19.0.0.213.
Still the same result. What a pain Adobe.
It should work, but Flash Pro CS6 doesn't show the worker's trace statements. Check flashlog.txt or send messages from the worker to the main SWF over a MessageChannel.
I made a lib to support use of Workers, if you want use you can check it in GitHub, it is open source project.. hope help ASWorker Link
The implementation is like this
package
{
import com.tavernari.asworker.ASWorker;
import com.tavernari.asworker.notification.NotificationCenter;
import com.tavernari.asworker.notification.NotificationCenterEvent;
import flash.display.Sprite;
public class ASWorkerDemo extends Sprite
{
private var asWorker:ASWorker;
//BOTH AREA
public function ASWorkerDemo()
{
//important know, all class start here will be replicated in all works.
asWorker = new ASWorker(this.stage, uiWorkerStartedHandler, backWorkerStartedHandler);
}
//BOTH AREA END
//UI AREA START
private function uiWorkerStartedHandler():void{
//implement all class or calls for your UI
NotificationCenter.addEventListener("FROM_BACK_EVENT_MESSAGE", onFromBackEventMessageHandler );
}
private function onFromBackEventMessageHandler(e:NotificationCenterEvent):void
{
trace(e.data);
if(e.data == "completed job"){
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("NEXT_MESSAGE") );
}
}
//UI AREA END
//BACK AREA START
private function backWorkerStartedHandler():void{
//implement all class or calls for your BACK operations
NotificationCenter.addEventListener("NEXT_MESSAGE", uiCallForNextMessageHandler );
}
private function uiCallForNextMessageHandler():void
{
for(var i:int = 0; i < 15; ++i){
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, i) );
}
NotificationCenter.dispatchEventBetweenWorkers( new NotificationCenterEvent("FROM_BACK_EVENT_MESSAGE", false, "completed job") );
}
// BACK AREA END
}
}
Good luck with Workers
Air 20.0.0.204 ADL is also not enjoying anything worker related (from my limited experimentation), however the desktop Flash Player seems to work just fine. This makes debugging convoluted and problematic when you are using Air specific libraries in your project and prefer not to wildcard variable types.
At the moment, workers are only supported on desktop platform. This Adobe developer's guide page specifically mentions "for desktop platforms".
Meanwhile, there is an AS3-Worker-Compat library by Jeff Ward that enables you to write code with Workers that will still work correctly in an environment that doesn't support Workers (of course, in this case your code will run in single-threaded mode).
I think many people are waiting for Adobe to implement workers for mobile, hopefully it will come in the future.

AS3: Call function does not work in browser

Here I have AS3 to for uploading recorded sound file to server. When I test it in Flash it works properly (record sound and upload it and goes to next frame) , but in browser it doesn't work. It seems can't call myUpload but I don't why? Is it should be mouse event? Thanks.
function VOCWordToYourMp3()
{
setTimeout(startRecording,3000);
recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
recorder.addEventListener(Event.COMPLETE, onRecordComplete);
}
function startRecording()
{
if (! recording)
{
recorder.record();
}
else if (recording)
{
recorder.stop();
recording = false;
}
}
function onRecording(e:RecordingEvent)
{
//
}
function onRecordComplete(e:Event):void
{
//
}
function renderWav(src, convertToMp3 = false)
{
//
function handleRenderTimer(e:TimerEvent)
{
//
}
function finishRender()
{
//
}
}
function makeIntoMp3(wav)
{
wav.position = 0;
mp3Encoder = new ShineMP3Encoder(wav);
mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
mp3Encoder.addEventListener(ProgressEvent.PROGRESS, mp3EncodeProgress);
mp3Encoder.start();
function mp3EncodeProgress(e:ProgressEvent):void
{
//
}
function mp3EncodeComplete(e: Event):void
{
myUpload('sound1',mp3Encoder.mp3Data);
}
}
function myUpload(namefile:String,sba: ByteArray):void
{
//upload code
}
Update:
In Flash Player 10 and Actionscript 3.0, all the calls to URLLoader must be in the same callstack.
http://helpx.adobe.com/flash-player/kb/user-interaction-required-upload-download.html
What is same callstack mean?
I recommend using something like Vizzy: https://code.google.com/p/flash-tracer/ to debug your swf in the browser. You can put trace statements into the onRecordComplete() and myUpload() functions, etc, to see how far the code is getting, and to see if you are getting any new errors. You may have some kind of security sandbox error because you are running in the browser. Being able to see what that error is will help you figure out what to do next.
To use Vizzy, you need to have the debug player running in your browser, and to configure the right path to your log file. This is sometimes a little bit tricky, so I'll give you some tips:
Add a file to your home directory called mm.cfg, and populate it with these settings:
ErrorReportingEnable=1
AS3Verbose=0
TraceOutputFileEnable=1
AS3Trace=0
TraceOutputBuffered=0
AS3StaticProfile=0
Then in Vizzy you have to set up the path to the log. On my windows 7 machine it is here:
C:\Users\MyUserName\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt
It may be different depending on your operating system.
Good luck, and report back if you Vizzy gives you any new information.

Actionscript 3.0: Addition of Document Class causes errors

I'm trying to learn Actionscript 3.0 with a game I'm making in Flash CS6, and I'm having some issues with the Document Class. Initially, I had a working menu with some scripting for keyboard events and sound. I realized that I needed to store some variables in a way that I could access them from any frame, so I created a Document Class with an empty class and set my game to reference it, and now my menu scripting is generating a compiler error. The error I'm getting is "1046: Type was not found or was not a compile-time constant: KeyboardEvent," which doesn't make any sense to me since it worked just fine beforehand. Anybody have any idea what the problem might be? Thanks!
Document Class:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
}
}
Menu Script:
import flash.utils.getDefinitionByName;
import flash.ui.Keyboard;
stop();//Used to stay on the current frame
var selection:int = 0;//Will be used to determine which button has its "On" animation activated
var canMove:Boolean = true;
var menuSong:Sound = new MenuSong();
menuSong.play (0 , 9999);//Plays and loops(9999 times) menu theme
var menuMove:Sound = new MenuMove();
var menuSelect:Sound = new MenuSelect();
stage.addEventListener(KeyboardEvent.KEY_DOWN, move);//Calls move function when a key is pressed
function move(event:KeyboardEvent):void{//The line causing the error
if(canMove){
if(event.keyCode == 40){
selection = (selection + 1)%3;//Occurs when down key is pressed
menuMove.play();
}
else if(event.keyCode == 38){
selection = (selection + 2)%3;//Occurs when up key is pressed
menuMove.play();
}
else if(event.keyCode == 32){
canMove = false;
SoundMixer.stopAll();
menuSelect.play();
fadeOut.gotoAndPlay(1);
}
switch(selection){
case 0:
this.singlePlayer.gotoAndPlay("On");
this.multiplayer.gotoAndStop("Off");
this.credits.gotoAndStop("Off");
break;
case 1:
this.singlePlayer.gotoAndStop("Off");
this.multiplayer.gotoAndPlay("On");
this.credits.gotoAndStop("Off");
break;
case 2:
this.singlePlayer.gotoAndStop("Off");
this.multiplayer.gotoAndStop("Off");
this.credits.gotoAndPlay("On");
break;
}//All this just tells the selected button (Based on the selection variable)
//to play its "On" animation, and the other buttons to play their "Off" animation.
}
}
You need to import flash.events.KeyboardEvent as you use it in your code (Menu script).
Why dont you use the script you called "Menu Script" as Document Class ? If the goal of your SWF is what is designed in the Menu Script code, it should be the Document Class.
In a way or another, if you use stage.addEventListener(KeyboardEvent.KEY_DOWN, move); in your code, you must import flash.utils.KeyboardEvent. Same thing for Sound ( import flash.media.Sound) & SoundMixer (import flash.media.SoundMixer).

record video using flash media server 4.5

Hello i am trying to capture my camera as an flv file with fms 4.5 i am doing the following:
protected function rec_clickHandler(event:MouseEvent):void
{
nc = new NetConnection();
nc.client = { onBWDone: function():void{ trace("onBWDone") } };
nc.connect("rtmp://localhost/vod");
nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
}
private function netStatusHandler(e:NetStatusEvent):void {
var code:String = e.info.code;
if(code == "NetConnection.Connect.Success"){ //in case of recording...
ns = new NetStream(nc);
ns.attachCamera(cam);
ns.attachAudio(mic);
ns.publish("filename","record");
}
else{
trace(code);
}
}
but i get the following error:
Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Record.NoAccess
Can anyone help? what am i doing wrong?
This status message, NetStream.Record.NoAccess, generally indicates that you don't have write permissions to the stream. Check the permissions of your streams dir to see if it is read only.
If that is not the issue, check which application are you trying to publish to, does not SSAS that has code to deny write access to stream
Make sure the previously recorded video is not being opened in any video player. If it is being accessed by some other program, it will not allow you to record or rewrite it.

AAC/MP4 not working in ActionScript 3's NetStream

I'm trying to play a remote AAC file in ActionScript 3 in Flash CS3 and am currently using this code:
var url:String = "http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.play(url);
(This is based on: http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html)
No errors are thrown, but no sound is played. I get the same behavior with a local AAC file and with a local MP4 video.
If I use a URL or file path that isn't a streamable file, I get a NetStream.Play.StreamNotFound error, which I'm guessing means that the stream is found in the case of a valid URL. If I use a local FLV, its audio is played just fine.
If I add the following listener and trace(evt.info.code) in netStatusHandler, I only see any codes (e.g. NetStream.Play.Start) traced with the FLV. No codes are traced with the AAC or MP4.
stream_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
The same is true of adding this listener (i.e. the onMetaData argument is only traced with the FLV, not with the other file types), with metaDataListener defined as an object with an onMetaData method that traces its argument.
stream_ns.client = metaDataListener;
Any ideas of what might be going wrong here, or how to diagnose it?
Thanks!
As stated here http://www.adobe.com/devnet/flashplayer/articles/hd_video_flash_player_03.html what you are doing is correct.
var connect_nc:NetConnection = new NetConnection();
connect_nc.connect(null);
var stream_ns:NetStream = new NetStream(connect_nc);
stream_ns.play("RE-Sample.m4a");
However, the Actionscript Language reference about nestream found here:
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/NetStream.html#play%28%29
states that:
play () method
...
...
When you use this method without Flash Media Server, there are security considerations. A file in the local-trusted or local-with-networking sandbox can load and play a video file from the remote sandbox, but cannot access the remote file's data without explicit permission in the form of a cross-domain policy file. Also, you can prevent a SWF file running in Flash Player from using this method by setting the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content.
...
...
Parameters
... arguments — The location of the video file to play, as a URLRequest object or a string. In Flash Player and in AIR content outside of the application security sandbox, you can play local video files that are stored in the same directory as the SWF file or in a subdirectory; however, you can't navigate to a higher-level directory.
So it's probably a security sandbox issue.
Everything in ActionScript 3.0 is event based (with few random exceptions where callbacks are used).
You need to listen for the NetStatusEvent with info.code "NetConnection.Connect.Success" in order to be allowed to call the NetStream.play() function.
Here's something that works (I just wrote it now, and tested it for you):
package
{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
public class MainDocument extends Sprite
{
private var _connection:NetConnection=new NetConnection();
private var _netStream:NetStream=null;
private var _strM4AURL:String="http://a1.phobos.apple.com/us/r1000/020/Music/d4/50/94/mzm.kjjofihr.aac.p.m4a";
//constructor
public function MainDocument():void
{
this._connect();
}
private function _connect():void
{
this._connection.close();
this._connection=new NetConnection();
this._connection.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
this._connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);
this._connection.connect(null);
}
private function _netStatusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
this._requestAudio();
break;
}
}
private function _requestAudio():void
{
if(this._netStream!==null)
this._netStream.close();
this._netStream=new NetStream(this._connection);
this._netStream.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
this._netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this._asyncErrorHandler);
this._netStream.checkPolicyFile=false;
this._netStream.play(this._strM4AURL);
}
private function _asyncErrorHandler(event:AsyncErrorEvent):void
{
trace(event);
}
}
}
Consult the ActionScript 3.0 Language Reference for more information.
There's a good chance what Oliver is saying is true as your not getting any feedback from any event listeners associated with the NetStream, and you are getting a StreamNotFound response.
StreamNotFound when not connecting to a FMS means that you either have the path wrong or its not seeing it, due to a security issue.