Streaming sound control in Flex (Newbie) - apache-flex

I'm assuming my lack of knowledge (I just started learning Flex yesterday, hah!) is the reasoning behind my inability to figure out how to make this work correctly - it may even just be a code positioning issue.
I've got several MP3 files I'm trying to stream. Right now I'm just trying to start and stop the main MP3. I've got the MP3 successfully playing, but stopping it is the issue I'm having. Here's my current code:
<mx:Script>
<![CDATA[
import flash.events.Event;
import flash.media.*;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
import mx.controls.Button;
//set current track & load song
var currentTrack:Number = 1;
var song:Sound = new Sound();
var req:URLRequest = new URLRequest("../assets/0"+currentTrack+".mp3");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
//CREATE BUTTONS (being loaded in mx:application on load)
private function createControls():void {
var playButton:Button = new Button();
playButton.label = "PLAY";
playButton.id = "playButton";
playButton.addEventListener(MouseEvent.CLICK, clickPlayHandler);
playerControls.addChild(playButton);
var stopButton:Button = new Button();
stopButton.label = "STOP";
stopButton.id="stopButton";
stopButton.addEventListener(MouseEvent.CLICK, clickStopHandler);
playerControls.addChild(stopButton);
}
//HANDLE CLICKS
private function clickPlayHandler(event:Event):void {
var button:Button = event.currentTarget as Button;
song.load(req, context);
song.play();
}
private function clickStopHandler(event:Event):void {
var button:Button = event.currentTarget as Button;
//This is not working...
song.close();
}
]]>
So I've got the song.play working, but the song.close doesn't stop the stream, it does nothing. Any clue how I could do this correctly/what I'm doing wrong?
Thanks! :)

You need to use the SoundChannel class to stop the sound playing, you assign it this way:
private var channel:SoundChannel = new SoundChannel();
//first assign it to the sound variable
channel = sound.play();
//then you can stop your sound this way
channel.stop();

Related

creating submenu's in flex context menu

Is there any workaround to create submenu in a flex context menu other than stopping right click from javascript.
Regards,
Hi Frank,
Yes, I want to create submenus in a context menu. Can you help me here.
Regards,
Hi Frank,
I need the context menu for the application not for datagrid.
In my initial question the phrase "other than stopping right click from javascript" means
"catch the right click in html, call a javascript function and over js call a as function."
The project that you have specified does the above procedure. I don't want to use this
procedure. Is there any other way for achieving submenus in a flex context menu. Could you
please tell me if so..
Regards,
Arvind
Yes, there is.
I don't know, what you exactly mean with this:
other than stopping right click from
javascript.
But, if you want to create a entry in submenu, do this:
//Instance of my own class
private var myContext:myContextMenu = new myContextMenu();
application.contextMenu = myContext.myContextMenu;
//Here is the Class:
package com.my.components
{
/* ////////////////////////////////////////////
///// My Context MenĂ¼ /////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//to use: //
// private var myContext:MyContextMenu = new MyContextMenu(); //
// init() in creationComplete //
// application.contextMenu = myContext.myContextMenu; //
////////////////////////////////////////////////////////////////////////////// */
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
public class MyContextMenu extends Sprite
{
public var myContextMenu:ContextMenu;
private var menuLabel:String = String.fromCharCode(169)+" My Company GmbH";
public function MyContextMenu()
{
myContextMenu = new ContextMenu;
removeDefaultItems();
addCustomItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
super();
}
private function removeDefaultItems():void
{
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomItems():void
{
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
}
private function menuItemSelectHandler(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest('http://www.my-company.de'));
}
private function createLabel():TextField
{
var txtField:TextField = new TextField();
//txtField.text = textLabel;
txtField.text = "RightClickHere";
return txtField;
}
}
}
Have fun
EDIT:
There is an interesting project here. They catch the right click in html, call a javascript function and over js call a as function.
Unfortunately, the limitation of FP or NativeMenu APi allowed just on level contextmenu. Read here
Frank

Flash UI blocked during flex rpc.soap.Operation::send

I've got a Flash UI that does a periodic server call to get some updated information. The call uses the flex sdk's rpc.soap.Operation class. It looks something like this:
var wsOperation:Operation = Operation(webService.getOperation(SomeOperation));
wsOperation.addEventListener("fault", wsError);
wsOperation.addEventListener("result", wsResult);
wsOperation.send(...some params);
This call gets some data from a SQL database. I have timed the call from right before the send to the start of the wsResult function at ~4 seconds. During this time, my UI is not updated. It is frozen/unresponsive.
Now, I know Flash is single-threaded/asynchronous, so I'm not sure why this is happening. I see that the send(..) function returns an AsyncToken which I am not using. Could this have something to do with it?
Any other ideas as to why this is happening are appreciated. Thanks.
I still haven't found an acceptable solution to this. It seems ridiculous that I would have to Pseudo thread to get flash to update the UI during a 4 second call. I'm wondering if maybe the parsing of the soap response could be taking up a lot of time. If there is a lot of processing to do, will Flash delay updating the UI indefinitely?
You will get a UI freeze in Flash because it is single threaded. However, you can do pseudo threading with something like this:
package
{
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.utils.getTimer;
import mx.core.UIComponent;
import mx.managers.ISystemManager;
public class PseudoThread extends EventDispatcher
{
public function PseudoThread(sm:ISystemManager, threadFunction:Function, threadObject:Object)
{
fn = threadFunction;
obj = threadObject;
// add high priority listener for ENTER_FRAME
sm.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 100);
sm.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
sm.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
thread = new UIComponent();
sm.addChild(thread);
thread.addEventListener(Event.RENDER, renderHandler);
}
// number of milliseconds we think it takes to render the screen
public var RENDER_DEDUCTION:int = 10;
private var fn:Function;
private var obj:Object;
private var thread:UIComponent;
private var start:Number;
private var due:Number;
private var mouseEvent:Boolean;
private var keyEvent:Boolean;
private function enterFrameHandler(event:Event):void
{
start = getTimer();
var fr:Number = Math.floor(1000 / thread.systemManager.stage.frameRate);
due = start + fr;
thread.systemManager.stage.invalidate();
thread.graphics.clear();
thread.graphics.moveTo(0, 0);
thread.graphics.lineTo(0, 0);
}
private function renderHandler(event:Event):void
{
if (mouseEvent || keyEvent)
due -= RENDER_DEDUCTION;
while (getTimer() < due)
{
if (!fn(obj))
{
if (!thread.parent)
return;
var sm:ISystemManager = thread.systemManager;
sm.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
sm.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
sm.stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
sm.removeChild(thread);
thread.removeEventListener(Event.RENDER, renderHandler);
dispatchEvent(new Event("threadComplete"));
}
}
mouseEvent = false;
keyEvent = false;
}
private function mouseMoveHandler(event:Event):void
{
mouseEvent = true;
}
private function keyDownHandler(event:Event):void
{
keyEvent = true;
}
}
}
This will enable you to do a process without the UI Freeze. It basically uses the stage's RENDER event to defer processing. This code performs as much Actionscript computation as possible limited by the time needed to maintain the frame rate. For more information see: http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html

TextField in AS3 - programming a click listener

I want to add a simple piece of text to the stage and add a listener to do something when the user clicks it.
Here's my TextLink class:
package some.package
{
import flash.display.Sprite;
import flash.external.ExternalInterface;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class TextLink extends Sprite
{
public var tf:TextField = new TextField();
public var bspr:Sprite = new Sprite();
public function TextLink(tx:int, ty:int, tft:String):void
{
tf.text = tft;
tf.x = tx;
tf.y = ty;
tf.autoSize = TextFieldAutoSize.LEFT;
bspr.addChild(tf);
this.addChild(tf);
}
}
}
And here is the way I am calling it, along with the listener:
public function test_array_of_objects():void
{
var tmp:TextLink = new TextLink(30, 30, "some text");
tmp.addEventListener(MouseEvent.CLICK, roverNotify);
addChild(tmp);
}
protected function roverNotify(e:Event):void
{
ExternalInterface.call("console.log", "got a click");
}
...But I don't get a message for some reason.
I've imported everything successfully. Any thoughts on what else I can try?
Is your TextLink class an event dispatcher? You're trying to add a listener to the TextLink object, but the click listener needs to be attached to the text field that you're using inside TextLink. TextLink needs to be a DisplayObject of some kind to inherit the dispatching capabilities.
Also, constructors should not specify a return type (since they're just returning themselves) -- the :void should not be there where your TextLink constructor is.
Does function TextLink require something like this at the beginning:
var tf:Text = new Text();
Is the problem with clicking the Sprite or getting the event to fire? If it's the former you could try adding the code below.
tmp.mouseChildren = false;
tmp.buttonMode = true;
ExternalInterface.call("console.log", "got a click");
You have a JavaScript function defined like this??:
function console.log(inputString) {
//do something
}
Edit: Nevermind the above, forgot about Firebug.
Also, TextLink doesn't need to be an event dispatcher, though you may want to have TextLink set its mouseChildren property to false (unless you need to be able to select that text), so that you don't inadvertently trigger events on the TextField, and buttonMode to true.
Edit: Also, what's the point of?:
var bspr:Sprite = new Sprite();
bspr.addChild(tf);
Final edit
How about this? http://code.google.com/p/fbug/issues/detail?id=1494
Yes, you are correct, in FF3 the console is injected only when the page has
javascript and uses window.console.
If you put any js that accesses the console before the Flash loads it should work, eg
<script>
var triggerFirebugConsole = window.console;
</script>
Let us know if this works. It's unlikely that we can fix this soon.

calling a function in file1.as from file2.as?

How can I call a function in file1.as from file2.as?
here is the code.
package com.modestmaps
{
import com.modestmaps.overlays.MarkerClip;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.text.TextField;
//import mx.core.Application;
import mx.core.Application;
import flash.events.MouseEvent;
public class InfoBubble extends Sprite
{
private var btn_close:String = "http://(.yada.yada.)/media/close_button.swf";
public var textField:TextField;
public var background:Shape;
public var shadow:Shape;
public var infoClip:MarkerClip;
protected var map:InfoMap;
//var infoClip:MarkerClip;
public var infoBubble:InfoBubble;
public function InfoBubble(urlLink:String)
{
//the name of my markers are set to the links of the swf files in which I want to load into the infobubble
this.name = urlLink;
this.mouseEnabled = false;
this.mouseChildren = true;
this.buttonMode=false;
shadow = new Shape();
shadow.filters = [ new BlurFilter(16, 16) ];
shadow.transform.matrix = new Matrix(1, 0, -0.5, 0.5, 0, 0);
addChild(shadow);
background = new Shape();
addChild(background);
textField = new TextField();
textField.selectable = false;
//the infobubble is still sized according to the textField.width and height
//I don't know how to get the size of the loaded swf
textField.width = textField.textWidth+432+4;
textField.height = textField.textHeight+288+4;
//add main swf
var request:URLRequest = new URLRequest(urlLink);
var loader:Loader = new Loader();
loader.load(request);
addChild(loader);
//position the main swf
//current measurements of swf file w432 h288
loader.y = -288 - 37;
loader.x = mx.core.FlexGlobals.topLevelApplication.LBloaderX;
//add close button
var reqButton:URLRequest = new URLRequest(btn_close);
var loader2:Loader = new Loader();
loader2.load(reqButton);
addChild(loader2);
loader2.addEventListener(MouseEvent.CLICK, closeInfoBubble);
function closeInfoBubble(event:MouseEvent):void
{
infoClip.removeMarkerObject(infoBubble)
infoBubble = null
}
//position the closebutton swf
//current measurements of closebutton swf file w18 h18
loader2.y = -286 - 37;
loader2.x = mx.core.FlexGlobals.topLevelApplication.LBloader2X;
// remember that things in marker clips are positioned with (0,0) at the given location
textField.y = -textField.height - 35;
textField.x = -10;
//I need to find out how to detect the width and height of the swf file loaded into loader2
//instead of the size of the textField
var rect:Rectangle = textField.getRect(this);
// get your graph paper ready, here's a "speech bubble"
background.graphics.beginFill(0x12345);
shadow.graphics.beginFill(0x000000);
for each (var g:Graphics in [ background.graphics, shadow.graphics ] ) {
g.moveTo(rect.left, rect.top);
g.lineTo(rect.right, rect.top);
g.lineTo(rect.right, rect.bottom);
g.lineTo(rect.left+15, rect.bottom);
g.lineTo(rect.left+10, rect.bottom+15);
g.lineTo(rect.left+5, rect.bottom);
g.lineTo(rect.left, rect.bottom);
g.lineTo(rect.left, rect.top);
g.endFill();
}
}
}
}
in this package i am attempting to add the shadow, which works, and then add the infobubble, which works, and then add a main swf which works, and then add a close_button.swf which it does load the swf; however, when I try to add the listener, I am unable to make the infobubble close back up.
Convention typically allows for one .as file per class, so you would need to have a reference to an instantiated object of that class inside the second one.
That said, we need -way- more information to provide any kind of useful answer.
To add to Tegeril's answer: Aside from accessing the functions inside of an instantiated class, you could also (as is the case in a utility class), make the functions static. In this case you could call the method like so: ClassName.methodName(), without the need to instantiate.... my random 2p.

Best way to get the color where a mouse was clicked in AS3

I have an image (mx) and i want to get the uint of the pixel that was clicked.
Any ideas?
A few minutes on the BitmapData LiveDoc Page will take you where you need to go. Once you have your image loaded into a Bitmap variable, you can access its BitmapData property. Add a Mouse Click Event Listener to the image and then use BitmapData::getPixel. The example for getPixel shows how to convert the uint response to an rgb hex code.
Here's a modification of the Example given on the BitmapData page that worked for me (using mxmlc - YMMV):
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
public class BitmapDataExample extends Sprite {
private var url:String = "santa-drunk1.jpg";
private var size:uint = 200;
private var image:Bitmap;
public function BitmapDataExample() {
configureAssets();
}
private function configureAssets():void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var request:URLRequest = new URLRequest(url);
loader.load(request);
addChild(loader);
}
private function completeHandler(event:Event):void {
var loader:Loader = Loader(event.target.loader);
this.image = Bitmap(loader.content);
this.addEventListener(MouseEvent.CLICK, this.clickListener);
}
private function clickListener(event:MouseEvent):void {
var pixelValue:uint = this.image.bitmapData.getPixel(event.localX, event.localY)
trace(pixelValue.toString(16));
}
}
}
Here's an even simpler implementation. All you do is take a snapshot of the stage using the draw() method of bitmapData, then use getPixel() on the pixel under the mouse. The advantage of this is that you can sample anything that's been drawn to the stage, not just a given bitmap.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.*;
stage.addEventListener(MouseEvent.CLICK, getColorSample);
function getColorSample(e:MouseEvent):void {
var bd:BitmapData = new BitmapData(stage.width, stage.height);
bd.draw(stage);
var b:Bitmap = new Bitmap(bd);
trace(b.bitmapData.getPixel(stage.mouseX,stage.mouseX));
}
Hope this is helpful!
Edit:
This edited version uses a single BitmapData, and removes the unnecessary step of creating a Bitmap. If you're sampling the color on MOUSE_MOVE then this is essential to avoid memory issues.
Note: if you're using a custom cursor sprite you'll have to use an object other than 'state' or else you'll be sampling the color of the custom sprite instead of what's under it.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.*;
private var _stageBitmap:BitmapData;
stage.addEventListener(MouseEvent.CLICK, getColorSample);
function getColorSample(e:MouseEvent):void
{
if (_stageBitmap == null) {
_stageBitmap = new BitmapData(stage.width, stage.height);
}
_stageBitmap.draw(stage);
var rgb:uint = _stageBitmap.getPixel(stage.mouseX,stage.mouseY);
var red:int = (rgb >> 16 & 0xff);
var green:int = (rgb >> 8 & 0xff);
var blue:int = (rgb & 0xff);
trace(red + "," + green + "," + blue);
}
This is not specific to Flex or mx:Image, and allows you to grab a pixel color value from any bitmap drawable object (provided you have permission):
private const bitmapData:BitmapData = new BitmapData(1, 1);
private const matrix:Matrix = new Matrix();
private const clipRect:Rectangle = new Rectangle(0, 0, 1, 1);
public function getColor(drawable:IBitmapDrawable, x:Number, y:Number):uint
{
matrix.setTo(1, 0, 0, 1, -x, -y)
bitmapData.draw(drawable, matrix, null, null, clipRect);
return bitmapData.getPixel(0, 0);
}
You could easily grab a pixel from the stage or your mx:Image instance. It's a lot more efficient than drawing the entire stage (or drawable object), and should be fast enough to hook up to MouseEvent.MOUSE_MOVE for instant visual feedback.

Resources