Actionscript - load and play another sound file - actionscript-3

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();
}

Related

Target variable found in .actionscript file from FLA?

I am trying to stop the sound channel below from playing when a movieclip is visible in a flash file, but the variable is located in the .as file which loads in the FLA. How can I target that variable? Right now I am getting "undefiend" for the properties channel and song.
fla:
if(myMC == visible){
channel.stop();
}
else(channel = song.play());
.as file:
public var channel:SoundChannel;
public function Main(param:MovieClip) {
function showXML(e:Event):void {
websiteXML = new XML(e.target.data);
MovieClip(root).gotoAndPlay(3);
//trace(websiteXML);
popupFunction();
var urlStr:String = websiteXML.settings.mp3Url.#srcUrl;
var isStreaming:Boolean = websiteXML.settings.mp3Url.#streaming;
var song:Sound = new Sound();
song.load(new URLRequest(urlStr));
setTimeout(function():void {
channel = song.play(0, 999);
channel.soundTransform = new SoundTransform(1);
},1000);
}
}

actionscript 3.0 function calling using button click

im creating a simple flash playlist using buttons, in my stage i have 4 buttons which is button for song1,song2, stop and play. i have a working code for this one, but i decided to revise it because my previous code is like, per song they have each stop and play button,so i created this one to have a dynamic stop and play, i created a function for each song, the function will change the filename of the song to be loaded,
heres the catch, so i first pick a song, (either song1 or song2) then i click stop, then when i select a new song this error appears
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at playlist_fla::MainTimeline/songSelect1()
i think its not calling the second function because i cant see the trace i put inside it,anyway heres my code, sorry for the long post,
THANKS IN ADVANCE
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songSelect1);
function songSelect1(e:MouseEvent):void{
song = "<filenameofthe1stsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(1);
}
song2.addEventListener(MouseEvent.CLICK,songSelect2);
function songSelect2(e:MouseEvent):void{
song = "<filenameofthe2ndsong>";
mySound.load(new URLRequest(song));
myTransform.volume = 0.5;
myChannel.soundTransform = myTransform;
lastPosition=0;
trace(2);
}
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
function onClickStop(e:MouseEvent):void{
lastPosition = myChannel.position;
myChannel.stop();
}
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function onClickPlay(e:MouseEvent):void{
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}
Correction:
According to Adobe:
Once load() is called on a Sound object, you can't later load a
different sound file into that Sound object. To load a different sound
file, create a new Sound object.
So, creating a new sound object would be the only way to fix it.
- - - Original Post - - -
If you enable debugging in your flash settings, it'd be easier to determine exactly what line is causing issues. That said, your code doesn't look incorrect apart from defining your sound transform twice. Try this:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var myTransform = new SoundTransform();
myChannel.soundTransform = myTransform;
var lastPosition:Number = 0;
var song;
song1.addEventListener(MouseEvent.CLICK,songHandler);
song2.addEventListener(MouseEvent.CLICK,songHandler);
btnStop.addEventListener(MouseEvent.CLICK,onClickStop);
btnPlay.addEventListener(MouseEvent.CLICK,onClickPlay);
function songHandler(e:MouseEvent):void {
switch (e.currentTarget.name) {
case "song1":
songSelect("<filenameofthe1stsong>")
break;
case "song2":
songSelect("<filenameofthe2ndsong>")
break;
}
}
function songSelect(songPath:String):void {
mySound.load(new URLRequest(songPath));
myChannel.soundTransform.volume = 0.5;
lastPosition = 0;
trace("Loading " + songPath);
}
function onClickStop(e:MouseEvent):void {
lastPosition = myChannel.position;
myChannel.stop();
}
function onClickPlay(e:MouseEvent):void {
myChannel = mySound.play(lastPosition);
myChannel.soundTransform = myTransform();
}

Details about as3 Sound.extract() format?

Using the sound object .extract() method, it's possible to retrieve the uncompressed sound data as a ByteArray. What is the format of the extracted sound data? Custom internal format, or established format? Endian type?
I would like to be able to load sounddata directly into a byteArray and feed sound playback from this data (bypassing step 1 below).
Here are my experiments so far:
Step 1. Here I load a sound, extract the data content and save it to disc:
private var sound:Sound;
private var byteArray:ByteArray;
// loading a sound...
private function init():void {
sound = new Sound();
sound.addEventListener(Event.COMPLETE, onComplete);
sound.load(new URLRequest('short.mp3'));
}
// saving it to disc...
private function onComplete(e:Event):void {
byteArray = new ByteArray();
sound.extract(byteArray, 100000000);
byteArray.position = 0;
var file:FileReference = new FileReference();
file.save(byteArray, 'short.data');
}
Step 2. Here I load the data from disc and feed the playback of a new sound object with this data:
private var sound:Sound;
private var byteArray:ByteArray;
// loading a sound...
private function init():void {
file = new FileReference();
file.addEventListener(Event.COMPLETE, onLoaded);
file.addEventListener(Event.SELECT, onSelected);
file.browse();
}
private function onSelected(e:Event):void {
file.load();
}
private function onLoaded(e:Event):void {
trace('loaded');
trace(file.data.length);
byteArray = file.data;
sound2 = new Sound();
sound2.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
sound2.play();
}
private function onSampleData(e:SampleDataEvent):void {
trace(e.position);
for (var i:int = 0; i < 2048; i++) {
var left:Number = byteArray.readFloat();
var right:Number = byteArray.readFloat();
e.data.writeFloat(left * 0.2);
e.data.writeFloat(right * 0.2);
}
}
I would like to be able of accomplishing this second step (loading data from disc and feeding a sound object playback) by using an external converter utility (sox or something).
/ Jonas
It should be standard 32bit floats, one per channel per sample. So I would try -e floating-point for sox, I haven't tried though but I would do it with a certain level of confidence... :) Floats are endian-independent, I believe...

execute remote SWF in AIR Security Sandbox

I try to download an external SWF and run it within the AIR security sandbox.
this is the code of the AIR application:
public class Downloader extends Sprite
{
private static const REMOTE_FILE:URLRequest = new URLRequest("http://myserver.com/downloadable.swf");
private var _main:NativeWindow;
public function Downloader()
{
var loader:URLLoader = new URLLoader(REMOTE_FILE);
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, downloadComplete);
}
private function downloadComplete(e:Event):void{
var ba:ByteArray = e.target.data;
var stream:FileStream = new FileStream();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
stream.open(file, FileMode.WRITE);
stream.writeBytes(ba);
stream.close();
loadAndRunSwf();
}
private function loadAndRunSwf(){
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
////obsolete?
//var context:LoaderContext = new LoaderContext();
//context.allowLoadBytesCodeExecution = true;
//context.applicationDomain = ApplicationDomain.currentDomain;
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context*/);
this._main.stage.addChild(loader);
this._main.activate();
}
}
Code of the downloadable.swf:
public class Downloadable extends Sprite
{
private var _btn:Button = new Button();
private var _baseFolder:File = new File("app-storage:/");
public function downloadable_test()
{
this.addChild(_btn);
_btn.label = "access Harddisk";
...
}
}
so now, If I run Downloader, it will download the swf and try to run it, but i'll get an exception in Downloadable on the line
private var _baseFolder:File = new File("app-storage:/");
the error:
SecurityError: file
at runtime::SecurityManager$/checkPrivilegeForCaller()
So - what Do I need to do to prevent such security errors? I want my remote SWF to be treated as native code running in the same security sandbox as the AIR code.
I'm not sure about Android, but for the regular web player you would need to specify SecurityDomain.currentDomain for the securityDomain of the Loader's context for the loaded code to be considered equal to the loader in terms of permissions. Also note that for reasons impossible to explain, if you use SecurityDomain when loading from the file system on PC Flash Player will complain.
However complicated, the Flash Player security is often a security by obscurity... So, if it doesn't work the way you coded it, try Loader.loadBytes() "workaround".
function loadAndRunSwf()
{
var context:LoaderContext=new LoaderContext(false);
context.allowCodeImport=true;
var ba:ByteArray=new ByteArray();
var file:File = File.applicationStorageDirectory.resolvePath("downloadable.swf");
var fileStream:FileStream=new FileStream();
var loader:Loader = new Loader();
fileStream.open(file,"read");
fileStream.readBytes(ba);
ba.position=0;
loader.loadBytes(ba,context);
this._main = new NativeWindow();
this._main.width = 1024;
this._main.height = 768;
this._main.stage.addChild(loader);
this._main.activate();
}

Object Data Type in AS3 and Flash Builder 4.5.1

I am trying to save a Sprite object as a file on the device I'm working on and it seems to work. the problem I'm having is reading the saved file back and placing it back on stage as a sprite. Below is the code I have so far, could someone tell me what it is I'm doing wrong? I have a suspicion that the saved isn't what I expect it to be since the file sizes have been under a kilobyte.
public function save_album(e:Event):void
{
var outFile:File = File.documentsDirectory; // dest folder is desktop
outFile = outFile.resolvePath("canvas3.bin");
var fs:FileStream = new FileStream();
var bytes:ByteArray = new ByteArray();
//trace (File.documentsDirectory.url + "/canvas2.bin");
fs.open(outFile, FileMode.WRITE);
bytes.writeObject(graffitiContainer) //graffitiContainer is a Sprite
bytes.position = 0;
fs.writeBytes(bytes, 0, bytes.length);
fs.close();
}
public function open_album(e:Event):void
{
var inBytes:ByteArray = new ByteArray();
var inFile:File = File.documentsDirectory;
inFile = inFile.resolvePath("canvas3.bin"); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(inBytes, 0, inBytes.length);
inStream.close();
inBytes.position = 0;
ui.removeChild(graffitiContainer);
var obj:Sprite = inBytes.readObject() as Sprite; //returns a null
graffitiContainer = obj;
ui = new UIComponent();
graffitiContainer.x = 0;
graffitiContainer.y = 100;
ui.addChild(graffitiContainer);
}
Not fully sure I understand what you're trying to accomplish; however, this implementation doesn't do what you're thinking - writeObject could only serialize general public properties, and not the graphics member.
You could render it to a Bitmap.
Saw a blog post about this:
http://jacwright.com/201/serializing-display-objects/