I am trying to use sound in flex. I got the output using urlrequest, as I have less sound files to use, and need not to load everytime i call it. So I tried to put that in css and use it, but I am getting an error : TypeError: Error #1007: Instantiation attempted on a non-constructor.
Below is my code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Style>
draw
{
url:"Assets/Sound/Active.mp3";
}
</mx:Style>
<mx:Script>
<![CDATA[
import flash.media.Sound;
import flash.media.SoundTransform;
internal var sound:Sound;
internal var soundVolume:SoundTransform;
public function playSound():void
{
var SoundClass:Class;
try
{
SoundClass = StyleManager.getStyleDeclaration("draw").getStyle("url") as Class;
soundVolume = new SoundTransform(1, 0);
trace("sound : "+sound);
sound = new SoundClass() as Sound;
sound.play(0, 0, soundVolume);
}
catch(E:Error)
{
trace("E "+E);
}
}
]]>
</mx:Script>
<mx:Button click="playSound()" label="Discard"/>
</mx:Application>
draw {
url:Embed(source="Assets/Sound/Active.mp3");
}
Refer this
Related
I try to dispatch event from actionscript class. But I get the error: "Error #1034: Type Coercion failed: cannot convert flash.events::Event#9f849c1 to mx.events.FlexEvent."
Here is my code:
MXML Code:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import com.testing.package.MyASClass;
import mx.events.FlexEvent;
public var sv:MyASClass;
protected function init(event:FlexEvent):void
{
sv = new MyASClass();
sv.addEventListener("myevent",mainfunc);
sv.myfunc();
}
protected function mainfunc(event:FlexEvent):void
{
trace("receive event");
}
]]>
</fx:Script>
</s:WindowedApplication>
Actionscript file:
package com.testing.package
{
import flash.display.Sprite;
import flash.events.Event;
public class MyASClass extends Sprite
{
public function MyASClass()
{
}
public function myfunc():void
{
dispatch();
}
private function dispatch():void
{
dispatchEvent(new Event("myevent"));
}
}
}
Why the error occur? How can I fix it?
Thank you.
//Solution is need replace FlexEvent into Event
protected function mainfunc(event:Event):void{
trace("receive event");
}
I have an embedded system that serves JSON results via a CGI process. I'm trying to setup a Flex app. to periodically poll the server once per second and update the GUI. I'm trying the approach below using a timer, but I only get the initial result, subsequent updates are not made. What could be going wrong?
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:HTTPService id="getPacketCounts" showBusyCursor="true" resultFormat="text"
url="http://10.1.10.28/cgi-bin/getpacketcounts"
requestTimeout="500"
result="getPacketCounts_resultHandler(event)"
fault="faultHandler(event)">
</mx:HTTPService>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var timer:Timer = new Timer(1000, 0);
private var counter:int;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
timer.addEventListener(TimerEvent.TIMER, timerListener);
timer.start();
}
private function timerListener(evt:Event):void {
label.text = "get http://10.1.10.28/cgi-bin/getpacketcounts";
getPacketCounts.send();
}
protected function getPacketCounts_resultHandler(event:ResultEvent):void
{
label.text = event.result.toString().substr(0, 60);
}
protected function faultHandler(event:FaultEvent):void
{
Alert.show("The server returned error code " + event.statusCode + ".", event.fault.faultString);
}
]]>
</fx:Script>
<s:Label id="label" text="v1.0"/>
</s:Application>
I can't see anything glaringly wrong with your code though I suspect the problem lies with your Timer getting out of sync with your HTTPService. Your Timer fires every second but it might take longer for your HTTPService to return a result and the next tick of your Timer tramples over the previous getPacketCounts.send() request. I'm not 100% sure about this though.
If I were writing this app I'd set it up so the Timer fired once, reset the Timer, sent of the HTTPService request, then waiting for a response before restarting the Timer.
// Make the Timer run once.
private var timer:Timer = new Timer(1000, 1);
// ....
// Add listeners to the Timer and start it
protected function application1_creationCompleteHAndler(event:FlexEvent):void
{
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerListener);
timer.start();
}
// Send the request
private function timerListener(e:TimerEvent):void
{
timer.reset();
// Your other stuff
}
protected function getPacketCounts_resultHandler(event:ResultEvent):void
{
timer.start()
// Your code
}
protected function faultHandler(event:FaultEvent):void
{
timer.start()
// Your code
}
Call to a possibly undefined method test through a reference with static type Class.
here is my class
package com.singleton.sample{
public class SampleSingleton{
public static function test( ):void{
trace('hello world')
}
}
}
and here is my mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
<mx:Script>
<![CDATA[
import com.singleton.sample.SampleSingleton;
public function init():void{
SampleSingleton.test() // error on this line
}
]]>
</mx:Script>
</mx:Application>
Please ignore the singleton references in the naming as I stripped down my class to this and it still doesn't work.
[EDIT]
import com.singleton.sample.SampleSingleton;
public function init():void{
SampleSingleton.test(); // this gives me the error
com.singleton.sample.SampleSingleton.test(); // this works
}
The Application file is named SampleSingleton i bet so you have a name collision. Rename the application.
The requirement is to get the method named getIncidentList() from web service using Soap and wsdl using Flex or Actionscript.
Can anyone help me i have a code which is not working:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:WebService id="DirectoryService"
wsdl="http://cmuicds.rutgers.edu/uicds/core/ws/services/DirectoryService.wsdl"
useProxy="false"
showBusyCursor="true"
result="onResult(event)"
fault="onFault(event)">
</mx:WebService>
<mx:ApplicationControlBar dock="true">
<mx:Button id="button"
label="get incidents from web service"
click="button_click()"/>
<mx:ComboBox id="cmb" dataProvider="{zipfls}" labelField="name" width="241" height="24"/>
</mx:ApplicationControlBar>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
[Bindable] private var zipfls:ArrayCollection;
private var flag:Boolean;
private function button_click():void
{
//Alert.show("Hi");
//DirectoryService.GetIncidentList.send();
DirectoryService.GetIncidentList();
flag = DirectoryService.canLoadWSDL();
//flag = DirectoryService.hasOwnProperty();
//Alert.show("Testing....." + flag);
//Alert.show("Description " +DirectoryService.operations);
}
private function onResult(evt:ResultEvent):void
{
zipfls = new ArrayCollection();
//textArea.text = ObjectUtil.toString(evt.result);
zipfls = evt.result as ArrayCollection;
Alert.show("Is data comming in?" + zipfls.length);
}
private function onFault(evt:FaultEvent):void
{
Alert.show(evt.type);
}
]]>
</mx:Script>
</mx:Application>
I think you are not calling method properly
and event button_click should be
private function button_click():void
{
DirectoryService.GetIncidentList();
}
i.e. send should not be used.
also see Using WebService components
Hopes that helps
Here is the error I get:
1046: Type was not found or was not a compile-time constant: fbAPI.
Here is my MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="startGame();">
<mx:Script>
<![CDATA[
import fbAPI;
public function startGame():void {
var fbAPI:fbAPI = new fbAPI(); // breaks on this line
fbAPI.fbLogin();
}
]]>
</mx:Script>
</mx:Application>
And here is my fbAPI.as stub that doesn't seem to get imported:
package {
public class fbAPI {
import mx.controls.Alert;
public function fbLogin():void {
Alert.show('test');
}
}
}
Try putting your import statements above your class and also just rename the instance name of the fbapi in your mxml real quick.
Edit: nevermind, I forgot in AS3 you don't need a constructor.
Make sure you put the fbAPI.as file in the same location as your mxml file.