Playing successive wav sounds in as3 - actionscript-3

I am trying to play through AS3 external wav sounds in a successive way. The obvious way to do it is to use this algorithm:
1. play sound 1
2. when sound 1 is done, play sound 2
etc.
The problem is that I fail the step number 2 (the "when sound 1 is done" part).
Please answer for wav sounds only, NOT mp3.
Here is my code:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.media.Sound;
import org.as3wavsound.WavSound;
import org.as3wavsound.WavSoundChannel;
public final class Main extends Sprite
{
public final function Main():void
{
playB.addEventListener(MouseEvent.MOUSE_UP, loadWav);
}
private final function loadWav(e:MouseEvent):void
{
var urlRequest:URLRequest = new URLRequest('Phone.wav');
var wav:URLLoader = new URLLoader();
wav.dataFormat = 'binary';
wav.load(urlRequest);
wav.addEventListener(Event.COMPLETE, playWav);
}
private final function playWav(e:Event):void
{
var tts:WavSound = new WavSound(e.target.data as ByteArray);
tts.play();
}
}
}
(This is step 1 of course)
Thanx

Try putting your sound into a SoundChannel - which is returned from the play command, then listen for the complete event:
private final function playWav(e:Event):void
{
var tts:WavSound = new WavSound(e.target.data as ByteArray);
var channel:WavSoundChannel = tts.play();
channel.addEventListener(Event.SOUND_COMPLETE, completeHandler)
}
private function completeHandler(e:Event):void {
//play next sound
}

Related

Event listener on button not working

So, my menu for my game is in a separate .fla file and I have used a loader like so to load the menu into my game:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
import caurina.transitions.Tweener;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
function menuLoaded(event:Event):void
{
menuLayer.addChild(myMenu.content);
}
playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}
I set instance names for my buttons but when I try to do something like menuLayer.playBtn.addEventListener(MouseEvent.CLICK, startGame);, I get messages saying Access of undefined property playBtn.
Now, I double checked on my Menu.fla and I definitely gave the button an instance name of playBtn but it's not working. Any help please? Might be something really obvious I've missed but I'm not sure what.
EDIT: Trying it another way (Converting the menu to a movieclip) but not 100% sure how to do it exactly. The code I have is:
public class Main extends MovieClip {
var mainMenu:menuMain = new menuMain;
// Other variables
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
menuLayer.addChild(mainMenu);
mainMenu.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
//var mainMenu:LoaderInfo = event.currentTarget as LoaderInfo;
//var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to execute timers etc.
}
My guess is that you think that when you go menuLayer.addChild(myMenu.content) that menuLayer suddenly becomes an instance of menu.swf That is not the case. It becomes a child of menuLayer.
Try this :
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
trace (menuInstance.playBtn);
This code assumes that you have nothing else added to menuLayer and in that case your menu.swf content would be the only child on the display list of menuLayer.
I am also assuming that menu.swf's contents are a MovieClip.
If my assumptions are wrong, this may not work.
I also noticed that you have your menuLoaded method inside your constructor. Not a good idea. Especially since the next line is expecting playBtn to exist and the menu hasn't even been loaded.
Try something like this :
public function Main()
{
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
var myMenu:Loader = new Loader();
var url:URLRequest = new URLRequest("Menu.swf");
myMenu.load(url);
myMenu.contentLoaderInfo.addEventListener(Event.COMPLETE, menuLoaded);
}
function menuLoaded(event:Event):void
{
var myMenu:LoaderInfo = event.currentTarget as LoaderInfo;
menuLayer.addChild(myMenu.content);
var menuInstance:MovieClip = menuLayer.getChildAt(0) as MovieClip;
// now you can actually add the listener, because the content is actually loaded
menuInstance.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}
public function startGame(e:Event)
{
// Code to remove the menu (menuLayer.removeChild?)
// Code here to start timers etc
}

play flv using netStream appendBytes

I know that there are many ways to play an FLV file but considering my project requirements, I need to play the flv using URLStream and NetStream
here's the complete sample code that I'm doing my tests on:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.events.ProgressEvent;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.NetStreamAppendBytesAction;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.net.URLStream;
/**
* ...
* #author Hadi Tavakoli
*/
public class Main extends Sprite
{
private var netConnection:NetConnection;
private var netStream:NetStream;
private var ul:URLStream;
private var video:Video;
private var bytes:ByteArray = new ByteArray();
private var _isSeek:Boolean = false;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
video = new Video();
addChild(video);
netConnection = new NetConnection();
netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionStatusHandler);
netConnection.connect(null);
}
private function netConnectionStatusHandler(ev:NetStatusEvent):void
{
switch(ev.info.code)
{
case 'NetConnection.Connect.Success':
ul = new URLStream();
ul.addEventListener(ProgressEvent.PROGRESS, onProgress);
ul.load(new URLRequest('01.flv'));
break;
}
}
private function onProgress(e:ProgressEvent):void
{
ul.readBytes(bytes, bytes.length);
if (!netStream)
{
netStream = new NetStream(netConnection);
netStream.client = { };
video.attachNetStream(netStream);
netStream.play(null);
trace("BEGIN")
netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
}
else
{
if (!_isSeek)
{
trace("SEEK")
netStream.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
_isSeek = true;
}
}
if (bytes.length == e.bytesTotal)
{
trace("END")
netStream.appendBytesAction(NetStreamAppendBytesAction.END_SEQUENCE);
}
netStream.appendBytes(bytes);
trace("-")
}
}
}
I'm not sure if I am using "appendBytes" method correctly? the video is shown but only a very few first frames will play and then the video stops!
in my eyes it seems all ok! do you have any advice on where my problem is?
I don't think you need the if (!_isSeek) block. It looks like you are pushing the bytes as you receive them in a sequential order and so there's never a seek. It looks like it will push the first set of bytes and then append a seek action and append the rest of the bytes. Try just removing that block and see if it works.
Otherwise I think it's ok.
in "ul.readBytes(bytes, bytes.length);" line, there is a bug i guess. It's never worked for me also. It always return full length (from 0 to the available bytes). So It have a huge memory leak. But if you are using flash player 11.4 or later, you can change it like this.
ul.position = bytes.length;
ul.readBytes(bytes);

Flash workers - sample application not working

I'm trying to get the hang of the AS3 workers, but there must be some elusive bit of understanding that just escapes me.
I've build a fairly simple PoC to see how it should work, but with no luck. When I run the "master" SWF, it seems to load the worker SWF fine and goes through everything without a hitch, except there's no response from the bloody worker.
I'm using Flash Builder 4.6 with FlexSDK 4.9.1, the PoC projects are built as ActionScript projects.
The worker file:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
public class WorkerPOC extends Sprite
{
private var wToM:MessageChannel;
private var mToW:MessageChannel;
public function WorkerPOC()
{
wToM = Worker.current.getSharedProperty("wToM") as MessageChannel;
mToW = Worker.current.getSharedProperty("mToW") as MessageChannel;
trace(mToW.receive());
wToM.send("Ready");
}
}
}
The master file:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
import flash.utils.ByteArray;
public class WorkerMaster extends Sprite
{
private var workerLoader:URLLoader;
private var workerData:ByteArray;
private var worker:Worker;
private var wToM:MessageChannel;
private var mToW:MessageChannel;
public function WorkerMaster()
{
workerLoader = new URLLoader();
workerLoader.dataFormat = URLLoaderDataFormat.BINARY;
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
workerLoader.addEventListener(Event.COMPLETE, onHasWorker);
workerLoader.load(new URLRequest("workers/WorkerPOC.swf"));
}
private function onHasWorker(event:Event):void
{
workerData = workerLoader.data as ByteArray;
workerData.shareable = true;
worker = WorkerDomain.current.createWorker(workerData);
wToM = worker.createMessageChannel(Worker.current);
wToM.addEventListener(Event.CHANNEL_MESSAGE, onMessage);
wToM.addEventListener(Event.CHANNEL_STATE, onState);
mToW = Worker.current.createMessageChannel(worker);
worker.setSharedProperty("wToM",wToM);
worker.setSharedProperty("mToW",mToW);
worker.start();
mToW.send(123);
}
private function onState(event:Event):void
{
trace("Channel state: ", wToM.state);
}
private function onMessage(event:Event):void
{
trace(wToM.receive());
}
}
}
I've been working with workers as well. I had them working for a while then everything kinda just stopped, in the same way yours is not working.
Looks like the messagechannel is not sending the messages properly in debug in 11.7.
Not sure why it is happening, but try running your code without the debugger attached when the worker is created. When I do that it works fine...
Sounds strange, but have you applied
-swf-verion=XXX // XXX must be > 17
as a compiler argument? I am asking this, because I had a very similar problem :)
Workers in Apache Flex 4.7

Decode flv in bytes in AS3

I am streaming flv file trough vlc media player running as http streaming server. So I am able to get the bytes but how to decode them?
Shoud I take float from the URLStream with readFloat() or plain bytes with readBytes()?
So long as only flv's are streaming, you will want to use the NetStream.appendBytesAction and NetStream.appendBytes for http streaming flv playback. Checkout the following blog post at ByteArray.org and also the quick example below:
AppendBytes
Playback Initialization:
var video:Video = new Video(width, height);
var video_nc:NetConnection = new NetConnection();
var video_ns:NetStream = new NetStream();
video_nc.connect(null);
video_ns.play(null);
video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
video.attachNetStream(video_ns);
ProgressEvent.PROGRESS Handler:
video_ns.appendBytes(bytesAvailable);
This is essentially the jist of it, bytesAvailable will represent the read bytes from the event data buffer. A full example is listed below:
package
{
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.ProgressEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
[SWF(width="1280", height="720")]
public class NetStreamAppendBytes extends Sprite
{
var video:Video;
var video_nc:NetConnection;
var video_ns:NetStream;
var video_stream:URLStream;
public function NetStreamAppendBytes()
{
super();
video_nc = new NetConnection();
video_nc.connect(null);
video_ns = new NetStream(video_nc);
video_ns.client = this;
video_ns.addEventListener(NetStatusEvent.NET_STATUS, ns_statusHandler);
video = new Video(1280, 720);
video.attachNetStream(video_ns);
video.smoothing = true;
video_ns.play(null);
video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
video_stream = new URLStream();
video_stream.addEventListener(ProgressEvent.PROGRESS, videoStream_progressHandler);
video_stream.load(new URLRequest("path_to_flv"));
addChild(video);
}
private function ns_statusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
}
private function videoStream_progressHandler(event:ProgressEvent):void
{
var bytes:ByteArray = new ByteArray();
video_stream.readBytes(bytes);
video_ns.appendBytes(bytes);
}
}
}
Best of luck!

ActionScript play audio

I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials so I took a stab that you can extend Sound the same way as you would extend Sprite. The final version is going to be headless and called my Java Script to play audio on Events.
package {
import flash.media.Sound;
import flash.net.URLRequest;
public class typeRight extends Sound {
public function HelloWorld( ) {
load(new URLRequest('./sound.mp3'));
play();
}
}
}
I am NOT working in Flash so please no GUI advice ; )
Rather than subclassing the Sound class, create a document class like this that contains a Sound class in it:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundPlayer extends Sprite
{
protected var _sound : Sound;
protected var _channel : SoundChannel;
public function SoundPlayer()
{
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
_sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
_sound.load(new URLRequest("./sound.mp3"));
}
protected function soundLoadCompleteHandler(evt : Event) : void
{
// Use the _channel object to control sound properties such as pan and volume.
_channel = _sound.play();
}
protected function loadError(evt : IOErrorEvent) : void
{
trace ("ERROR :: " + evt);
// You could try recovering from the error here.
}
}
}