Decrypt sound before playing a mp3 file in flex - actionscript-3

This is a very broad question , can I de-crpypt a streaming file, (assuming that i encrpyting this file on the server side during upload) , in flex before it being processed for playing.

You can process sound bytes before playing. This is example from Adobe documentation:
var sourceSnd:Sound = new Sound();
var outputSnd:Sound = new Sound();
var urlReq:URLRequest = new URLRequest("test.mp3");
sourceSnd.load(urlReq);
sourceSnd.addEventListener(Event.COMPLETE, loaded);
function loaded(event:Event):void
{
outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
outputSnd.play();
}
function processSound(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
sourceSnd.extract(bytes, 4096);
event.data.writeBytes(upOctave(bytes));
}
function upOctave(bytes:ByteArray):ByteArray
{
var returnBytes:ByteArray = new ByteArray();
bytes.position = 0;
while(bytes.bytesAvailable > 0)
{
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
if (bytes.bytesAvailable > 0)
{
bytes.position += 8;
}
}
return returnBytes;
}
Refer this link.

Related

AS3 event listeners inside loop

I'm having a bit of a hard time figuring this one out.
I'm creating an Air/Flash app that uploads files from a local directory to the server.(without user interaction).
I'm creating an Array of the files found in the folder and can upload them fine without any issues.
However, I can not keep track of the event listeners; meaning, the for loop obviously completes before the event listeners for each upload.
Is there a way to keep track of the event listeners and find out when the last one finished?
Any help is greatly appreciated...
Here's my code:
public var UPLOAD_URL:String = "http://myhost/dev/uptest.php";
upbtn.addEventListener(MouseEvent.CLICK, uploadme);
public function uploadme(event:MouseEvent):void
{
preloader.visible = true;
var desktop:File = File.applicationStorageDirectory.resolvePath("photo");
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
var date:Date = new Date(); // get new date
var mytime:Number = Math.round(date.valueOf()/1000);
var ur:URLRequest = new URLRequest();
var uv:URLVariables = new URLVariables();
uv.filename = files[i].name;
ur.data = uv;
ur.method = URLRequestMethod.POST;
ur.url = UPLOAD_URL;
files[i].addEventListener(ProgressEvent.PROGRESS, updateProgress);
files[i].addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, doneUpload);
files[i].addEventListener(IOErrorEvent.IO_ERROR, fileError);
files[i].upload(ur);
//trace(i)
function updateProgress(event:ProgressEvent):void
{
//wait preloader.visible = true;
//trace(event.currentTarget.name);
}
function doneUpload(event:DataEvent):void
{
//var loader2: URLLoader = URLLoader(event.target);
//trace(i);
trace(event.data);
//preloader.visible = false;
}
function fileError(event:IOErrorEvent):void
{
trace("error");
}
}
}
One approach is to keep track of how many files you're uploading and only act in the doneUpload listener once all files have finished:
private var numFiles:int = 0;
public function uploadme(event:MouseEvent):void
{
...
var files:Array = desktop.getDirectoryListing();
numFiles = files.length;
for (var i:uint = 0; i < files.length; i++)
...
}
function doneUpload(event:DataEvent):void
{
//var loader2: URLLoader = URLLoader(event.target);
//trace(i);
trace(event.data);
numFiles--;
if (numFiles == 0) {
trace("All the files are done uploading.");
}
//preloader.visible = false;
}

Refresh stream content

When pres play it connects to stream and its all ok!
When I click pause btn it stops sound, but the problem
is when you click play btn after them it starts playing stream from the same place where you stop it, doing this all time nothing happens it always start stream from the same place when do it 1st time...
play_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
pause_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound);
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
spiner_mc.visible=true;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
spiner_mc.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}
everything was easy!
var fl_SC:SoundChannel = new SoundChannel;
var fl_ToPlay:Boolean = true;
function fl_ClickToPlayStopSound(evt:MouseEvent):void
{
if(fl_ToPlay)
{
var s:Sound = new Sound();
var req:URLRequest = new URLRequest("http://85.254.49.110:8002/radiov");
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
s.load(req, context);
fl_SC = s.play();
pause_btn.visible=true;
play_btn.visible=false;
}
else
{
fl_SC.stop();
pause_btn.visible=false;
play_btn.visible=true;
}
fl_ToPlay = !fl_ToPlay;
}
The s:Sound, req, context and s.load cant be placed outside and thats all...

AS3 Play & Pause Current (loaded) MP3 (Flash CC)

I have 8 external mp3s that are loaded and will be played sequencially. I cannot figure how to pause the mp3 currently playing and start playing from the point it was paused. I did have code that would pause it but when clicking the play button, it would play the first mp3 from the beginning.
var s1:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var s2:Sound = new Sound(new URLRequest("Audio Files/2.mp3"));
var s3:Sound = new Sound(new URLRequest("Audio Files/3.mp3"));
var s4:Sound = new Sound(new URLRequest("Audio Files/4.mp3"));
var s5:Sound = new Sound(new URLRequest("Audio Files/5.mp3"));
var s6:Sound = new Sound(new URLRequest("Audio Files/6.mp3"));
var s7:Sound = new Sound(new URLRequest("Audio Files/7.mp3"));
var s8:Sound = new Sound(new URLRequest("Audio Files/8.mp3"));
s1.addEventListener(Event.COMPLETE, doLoadComplete);
s2.addEventListener(Event.COMPLETE, doLoadComplete);
s3.addEventListener(Event.COMPLETE, doLoadComplete);
s4.addEventListener(Event.COMPLETE, doLoadComplete);
s5.addEventListener(Event.COMPLETE, doLoadComplete);
s6.addEventListener(Event.COMPLETE, doLoadComplete);
s7.addEventListener(Event.COMPLETE, doLoadComplete);
s8.addEventListener(Event.COMPLETE, doLoadComplete);
var channel:SoundChannel = new SoundChannel();
channel = s1.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);
function doLoadComplete($evt:Event):void
{
trace("Song loaded.");
}
function doSoundComplete($evt:Event):void
{
trace("1 done.");
channel = s2.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete2)
}
function doSoundComplete2($evt:Event):void
{
trace("2 done.");
channel = s3.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete3);
}`
Here is what I have so far:
This loads the mp3s and plays them. The pause btn works but the play button to resume the audio gives me an error : ReferenceError: Error #1069: Property 0 not found on flash.media.Sound and there is no default value. at mp3sequence_fla::MainTimeline/playSound()
My guess is that the value for the current position or last position is incorrect.
var myArray:Array=[0,1,2,3,4,5,6,7];
var i:uint=1;
var req:URLRequest = new URLRequest("mp3/"+myArray[i]+".mp3");
var VSound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; //last position of the sound
var curSoundIndex:int = 0; //var to store the current sound that is playing
VSound.load(req);
channel = VSound.play();
function playSound(e:Event = null) {
//if no sound channel, load the current sound into it
channel = VSound[curSoundIndex].play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true);
lastPosition = channel.position;
}
function pauseSound(e:Event = null) {
if (channel) {
lastPosition = channel.position;
channel.stop();
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= VSound.length) curSoundIndex = 0;
}
play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
To pause a sound you can store it's position when you pause it, and use that variable to replay from that position.
var s:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var channel:SoundChannel = new SoundChannel();
channel = s.play();
var pausePosition:int;
function pause():void {
soundPosition = channel.position;
channel.stop();
}
function resume():void {
channel = s.play(soundPosition);
}
You can store the position property of the corresponding SoundChannel. I've added some code to make the whole thing less redundant
var curSoundIndex:int = 0; //var to store the current sound that is playing
var lastPosition:Number = 0; //last position of the sound
var soundChannel:SoundChannel;
var sounds:Vector.<Sound> = new Vector.<Sound>(); //array of all sounds
loadNextSound();
function loadNextSound(e:Event = null):void {
//check if all sounds are loaded
if (sounds.length >= 8) {
curSoundIndex = 0; //select first sound
resume(); //start playing
return;
}
//if not, load the next sound and add it to the array/vector
var sound:Sound = new Sound(new URLRequest("Audio Files/" + (sounds.length + 1) + ".mp3"));
sounds.push(sound);
if(sound.bytesLoaded < sound.bytesTotal){ //check if already loaded
sound.addEventListener(Event.COMPLETE, loadNextSound);
}else{
loadNextSound();
}
}
function pause(e:Event = null) {
if (soundChannel) {
lastPosition = soundChannel.position;
soundChannel.stop();
}
}
function resume(e:Event = null) {
//if no sound channel, load the current sound into it
soundChannel = sounds[curSoundIndex].play(lastPosition);
soundChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true); //use weak listener to avoid memory leaks
lastPosition = 0;
}
}
function doSoundComplete($evt:Event):void {
curSoundIndex++;
if (curSoundIndex >= sounds.length) curSoundIndex = 0;
}

How do I know the length of the video (external FLV video)?

How do I know the length of the video (external FLV video)?
I have tried several ways but the result is 0.
videonya.addEventListener(fl.video.VideoEvent.READY, onFlvPlayback_READY);
function onFlvPlayback_READY(event:fl.video.VideoEvent):void
{
var metaDataObj:Object = videonya.metadata as Object;
trace("metaDataObj.duration: "+metaDataObj.duration);
}
The standard way of playing video and accessing metadata is this:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
ns.play("video.flv");
var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);
function onMetaData(infoObject:Object):void
{
var key:String;
for (key in infoObject)
{
trace(key + ": " + infoObject[key]);
}
}
This will trace out all of the metadata codes including duration. If you just want the duration: trace(infoObject.duration); inside the onMetaData(infoObject) function, of course.

AS3 Adobe AIR: Return xml string

I'm working in Flash CS6 with Adobe AIR 3.3 (for iOS) and having trouble returning an XML string to a textField.
It is tracing the proper information, and I've tried a few ways to return the trace but can't seem to quite get it... Here is my most recent try. Any suggestions? Thanks in advance.
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://www.someURL.php"));
//php file that echos xml
myLoader.addEventListener(Event.COMPLETE, init);
var fadedText:TextField;
var touchList:TouchList;
var textOutput:TextField;
var animateLeft:Tween;
var listArray:Array;
var item:TouchListItemRenderer;
var theXML:XML;
var days:Array = new Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
var daySelected;
var businessNameArray:Array = new Array();
var businessLogoArray:Array = new Array();
var businessAddress:Array = new Array();
var distanceArrayDisplay:Array = new Array();
var distanceArrayCount = 0;
function init(e:Event = null):void
{
trace(myLoader.data);
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
myLoader.close();
// add our list and listener
var itemSizeCalculator = stage.stageHeight / 6;
touchList = new TouchList(stage.stageWidth, stage.stageHeight-itemSizeCalculator);
touchList.addEventListener(ListItemEvent.ITEM_SELECTED, handlelistItemSelected);
addChild(touchList);
touchList.x = stage.stageWidth;
// Fill our list with item rendreres that extend ITouchListRenderer.
for(var i:int = 0; i < theXML.food.length(); i++) {
if(theXML.food[i].monday != "un")
{
item = new TouchListItemRenderer();
item.index = i;
item.data = theXML.food[i].business;
item.name = theXML.food[i].business;
item.addEventListener(MouseEvent.CLICK, itemWasClicked);
item.itemHeight = itemSizeCalculator;
businessNameArray[i]= theXML.food[i].business;
businessLogoArray[i]=("http://www.logosURL.com/"+theXML.food[i].logo);
businessAddress[i]= theXML.food[i].address;
var fadedTextFormat:TextFormat = new TextFormat();
fadedTextFormat.bold = true;
fadedTextFormat.color = 0x999999;
fadedTextFormat.size = 14;
fadedTextFormat.font = "Helvetica";
fadedText = new TextField();
fadedText.height = 30;
fadedText.mouseEnabled = false;
fadedText.defaultTextFormat = fadedTextFormat;
item.addChild(fadedText);
fadedText.x = 75;
fadedText.y = 10;
distanceArrayDisplay.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest("http://maps.googleapis.com&origins=someAddress&destinations="+businessAddress[i]+"&mode=walking&language=en-en&sensor=false"));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
var logoLoader:Loader = new Loader();
item.addChild(logoLoader);
var logoURL:URLRequest = new URLRequest("http://www.myLogos.com/"+theXML.food[i].logo);
logoLoader.load(logoURL);
logoLoader.scaleX = .4;
logoLoader.scaleY = .4;
logoLoader.y = logoLoader.y + 5;
logoLoader.mouseEnabled = false;
var arrowGraphic:rightArrow = new rightArrow();
item.addChild(arrowGraphic);
arrowGraphic.x = stage.stageWidth - 5;
arrowGraphic.y = item.height/2;
touchList.addListItem(item);
}
}
}
function distanceCalculated(e:Event)
{
// trace(e.currentTarget.data);
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
var returnVar:String = (distanceXML.row.element.distance.text);
distanceArrayDisplay[distanceArrayCount].text = returnVar;
trace(returnVar);
distanceArrayCount++;
}
I am guessing that you are correctly reading the first XML, and that XML has a list of URLs that you want to load and then display some info from those on TextFields. Without knowing the structure of that XML I can't suggest you any working code, but I can point you on the right direction. For more info on reading/iterating XML on flash as3, please read: http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
//iterator var
var xml_read:uint=0;
//array of textfields for reference
var array_textFields:Array;
//config XML complete
function init(e:Event = null):void
{
array_textFields = new Array();
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
//this depends on the XML structure, please look at the article I linked
//for more info on how to iterate an XML
var i:uint=0;
for(someUrl in theXML..url)
{
var fadedText:TextField = new TextField();
//you should place each Textfield on different coord, otherwise
//they will all stack on top of each other and you will only see one
//for example:
fadedText.y = (fadedText.height+10)*i;
item.addChild(fadedText);
array_textFields.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest(someUrl));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
i++;
}
}
function distanceCalculated(e:Event):void
{
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
//retrieve information
var returnVar:String = (distanceXML.row.element.distance.text);
//set to textfield
TextField(array_textFields[xml_read]) = returnVar;
//increase iterator
xml_read++;
}
Please bear in mind that in ActionScript3, all network I/O is asynchronous. Usually EventListener functions don't return any value because you don't know when the data is ready. What you do is store a reference to where you want the data to go (in your case, a TextField variable) when the EventListener function is called asynchronously.