Sound Function Doesn't work on TouchEvent - actionscript-3

When I'm trying to add a sound to TouchEvent Function the sound doesn't work on the Android emulator. But it works fine if I replace the TouchEvent with MouseEvent and test it on Adobe Animate. What am I doing wrong?
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
var mysound: ClickTap = new ClickTap();
var mySprite: Sprite = new Sprite();
var myTextField: TextField = new TextField();
mySprite.graphics.beginFill(0x336699);
mySprite.graphics.drawRect(0, 0, 40, 40);
addChild(mySprite);
mySprite.addEventListener(TouchEvent.TOUCH_TAP, taphandler);
function taphandler(evt: TouchEvent): void {
mysound.play();
myTextField.text = "I've been tapped";
myTextField.y = 50;
addChild(myTextField);

Related

Video play error in Actionscript3

i am working on an actionscript3 project. when i am loading the video file in a class without the e:Event parameter;
private function setupVideo_2():void
{
vid = new Video(640,480);
addChild(vid);
nc= new NetConnection();
nc.connect(null);
ns= new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData= function (evt:Object):void{};
ns.client = listener;
ns.play("introduction to 3G and 4G.flv");
}
it plays fine.
The moment i put it in the condition stating play only when the marker pattern is sited; it loadz but there is a visible lag followed by stopping of the video and repeating of the first line around 3 times and then the air application stops working and eventually crashes.
here is the code for it.
private function loop (e:Event):void
{
bmd.draw(vid);
try
{
if (detector.detectMarkerLite(raster, 80)&& detector.getConfidence() > 0.5)
{
vid = new Video(640,480);
addChild(vid);
nc= new NetConnection();
nc.connect(null);
ns= new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData= function (evt:Object):void{};
ns.client = listener;
ns.play("4 Clever Ethernet Cable Hacks.flv");
}
}
catch(e:Error){}
}
Please tell me why is the video lagging when played inside the if condition.

How to create a sprite from an image loaded in Loader()?

I'm loading a background image via the Loader() class and wanted to know if there's a way to create a sprite from that loaded image?
I'm wanting to put a function in an external class file to put the image in the loader and then call the class to create a sprite from the loaded image. I'm not even sure this is possible.
Note: I'm using flashdevelop and no timeline.
You can just use the loader object as a display object or you can access the Bitmap object in the loader and add that to a sprite.
var loader:Loader = new Loader();
loader.load(new URLRequest(filename));
addChild(loader);
loader.x = 100;
loader.y = 200;
//so on
To get access to the bitmap and bitmapdata loaded just add an event listener and access them.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(filename));
private function onLoadComplete(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var loadedBitmap:Bitmap = loaderInfo.content as Bitmap;
var sprite:Sprite = new Sprite();
sprite.addChild(loadedBitmap);
addChild(sprite);
sprite.x = 100;
sprite.y = 200;
//so on
}

Actionscript - load and play another sound file

i am playing a sound file and i want onclick to start playing another file.
You can check the funcyion PlayAnother() in the following example:
private var TheSound:Sound = new Sound();
private var mySoundChannel:SoundChannel = new SoundChannel();
private function PlaySound(e:MouseEvent):void
{
TheSound.load(new URLRequest("../lib/File1.MP3"));
mySoundChannel = TheSound.play();
}
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound.load(new URLRequest("../lib/File2.MP3"));
}
public function Test1():void
{
var Viewer:Shape = new Shape();
Viewer.graphics.lineStyle(0, 0x000000);
Viewer.graphics.beginFill(0x000000);
Viewer.graphics.drawRect(0, 0, 1, 10);
Viewer.graphics.endFill();
Viewer.width = 30;
Viewer.x = 10;
var Viewer1:Shape = new Shape();
Viewer1.graphics.lineStyle(0, 0x000000);
Viewer1.graphics.beginFill(0x000000);
Viewer1.graphics.drawRect(0, 0, 1, 10);
Viewer1.graphics.endFill();
Viewer1.width = 30;
Viewer1.x = 50;
var tileSpot:Sprite = new Sprite();
var tileSpot1:Sprite = new Sprite();
tileSpot.addChild(Viewer)
tileSpot1.addChild(Viewer1)
addChild(tileSpot);
addChild(tileSpot1);
tileSpot.addEventListener(MouseEvent.CLICK, PlaySound);
tileSpot1.addEventListener(MouseEvent.CLICK, PlayAnother);
}
but i'm getting the error (Functions called in incorrect sequence, or earlier call was unsuccessful).
can any one help please.
Flash is complaining because you're loading a new file into a Sound object which already has data. (If you look at the docs for Sound.load() here, it says "Once load() is called on a Sound object, you can't later load a different sound file into that Sound object").
You just need to instantiate a new Sound before loading File2 and run play() again:
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound = new Sound();
TheSound.load(new URLRequest("../lib/File2.MP3"));
mySoundChannel = TheSound.play();
}

Is it possible to add a movieclip over a bitmap of StageWebView

I want to add a simple spinnerloader(movieclip) over a StageWebView html page for that i converted StageWebView page to bitmap but that didnt work please help me to solve this problem
var webView:StageWebView = new StageWebView();
var textGoogle:TextField=new TextField();
var textFacebook:TextField=new TextField()
textGoogle.htmlText="<b>Google</b>";
textGoogle.x=300;
textGoogle.y=-80;
addChild(textGoogle);
textFacebook.htmlText="<b>Facebook</b>";
textFacebook.x=0;
textFacebook.y=-80;
addChild(textFacebook);
textGoogles.addEventListener(MouseEvent.CLICK,goGoogle);
textFacebooks.addEventListener(MouseEvent.CLICK,goFaceBook);
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 100, stage.stageWidth, stage.stageHeight);
function goGoogle(e:Event):void
{
webView.loadURL("http://www.google.com");
webView.stage = null;
webView.addEventListener(Event.COMPLETE,handleLoad);
}
function goFaceBook(e:Event):void
{
webView.loadURL("http://www.facebook.com");
webView.stage = null;
webView.addEventListener(Event.COMPLETE,handleLoad);
}
function handleLoad(e:Event):void
{
var bitmapData:BitmapData = new BitmapData(webView.viewPort.width, webView.viewPort.height,true,0xffffff);
webView.drawViewPortToBitmapData(bitmapData);
var webViewBitmap:Bitmap=new Bitmap(bitmapData);
webViewBitmap.y = 100;
addChild(webViewBitmap);
}
This should be possible. Once you have created your bitmap:
1. remove the StageWebView from the stage
2. add your bitmap to the stage.
3. add your movie clip to the stage.

Controlling as2 swf playback in as3

I am embedding a swf built in flash 8 into an as3 project. When I call stop() or gotoAndStop(0); on the MovieClip that represents an instance of the embedded swf it stops for a sec and then continues. Trying to call removeChild on the mc removes it from the display but the audio in the swf keeps playing. The swf, in this case must be embedded, I cannot use loader. Any ideas
The code:
[Embed (source = "t1.swf")]
private var t1:Class;
private var mc:MovieClip;
public function iphoneTest()
{
var tf:TextField = new TextField();
tf.x = 10;
tf.y = 100;
tf.width = 100;
tf.height = 50;
tf.text = "Hello worl";
mc = new t1();
var button:CustomSimpleButton = new CustomSimpleButton();
button.width = 50;
button.height = 50;
button.x = 10;
button.y = 150;
button.addEventListener(MouseEvent.CLICK, onClick);
this.addChild(mc);
this.addChild(tf);
this.addChild(button);
}
private function onClick(e:MouseEvent):void {
mc.stop();
this.removeChild(mc);
}
did you try mc = null;?
also since you know it's an as2 swf, should probably use avm1movie instead of MovieClip
At very worst you can just kill all sounds in the SWF...
Make sure you import the sound mixer class then kill the sound..
import flash.media.SoundMixer;
SoundMixer.stopAll();
If your SWF has any hierarchy, you'll need to recurse through it to stop all movie clips.
private function stopAll(do:DisplayObject):void
{
var clip:MovieClip = do as MovieClip;
if (clip != null)
clip.stop();
var container:DisplayObjectContainer = do as DisplayObjectContainer;
if (container != null)
{
for (var i:int = 0; i < container.numChildren; ++i)
{
stopAll(container.getChildAt(i));
}
}
}