Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
Reason:
Trying to load a sound file in AIR and the path was incorrect or I was reusing a sound instance. I don't really know on this one but I think I was reusing a sound when I should have been reusing a soundchannel. I rewrote the code and it works now. Included below and attached as a download. This example is for Adobe AIR: Loading and playing sounds locally in an Adobe AIR application
XML:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="{init()}">
-
<mx:Script>
-
<![CDATA[
-
import mx.controls.Alert;
-
-
private var currentFile:File; // The currentFile opened (and saved) by the application
-
private var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile
-
private var defaultDirectory:File = File.documentsDirectory; // The default directory
-
public var reusableSoundChannel:SoundChannel = new SoundChannel();
-
-
public function init():void {
-
loadButton.addEventListener(MouseEvent.CLICK, openFile, false, 0, true);
-
playButton.addEventListener(MouseEvent.CLICK, playSound, false, 0, true);
-
stopButton.addEventListener(MouseEvent.CLICK, stopSound, false, 0, true);
-
}
-
-
/**
-
* Called when the user clicks the Open button. Opens a file chooser dialog box, in which the
-
* user selects a currentFile.
-
*/
-
private function openFile(e:Event):void {
-
var fileChooser:File;
-
if (currentFile) {
-
fileChooser = currentFile;
-
}
-
else {
-
fileChooser = defaultDirectory;
-
}
-
-
var filter1:FileFilter = new FileFilter("Sound or music or something", "*.mp3;*.wav;*.html;*.txt;*.xml");
-
fileChooser.browseForOpen("Open", [filter1]);
-
fileChooser.addEventListener(Event.SELECT, fileOpenSelected);
-
}
-
-
/**
-
* Called when the user selects the currentFile in the FileOpenPanel control. The method passes
-
* File object pointing to the selected currentFile, and opens a FileStream object in read mode (with a FileMode
-
* setting of READ), and modify's the title of the application window based on the filename.
-
*/
-
private function fileOpenSelected(event:Event):void {
-
currentFile = event.target as File;
-
alarmSound.text = currentFile.url;
-
currentFile.removeEventListener(Event.SELECT, fileOpenSelected);
-
}
-
-
public function playSound(e:MouseEvent):void {
-
if (currentFile== null || !currentFile.exists) { Alert.show("Pick a file dude!"); return}
-
trace("playing sound " + currentFile.name)
-
//fileName = fileName.replace(/file/, "app");
-
-
var location:URLRequest = new URLRequest(currentFile.url);
-
//var soundURL:String = "app:/" + fileName;
-
var soundURL:String = currentFile.name;
-
var sound:Sound = new Sound();
-
-
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
-
sound.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError);
-
-
sound.load(location);
-
-
}
-
-
public function stopSound(event:MouseEvent):void {
-
reusableSoundChannel.stop();
-
}
-
-
private function onSoundLoadComplete(event:Event):void {
-
var localSound:Sound = event.target as Sound;
-
reusableSoundChannel = localSound.play();
-
}
-
-
private function onSoundIOError(event:IOErrorEvent):void {
-
trace("The sound could not be loaded: " + event.text);
-
}
-
-
]]>
-
</mx:Script>
-
-
<mx:Button id="loadButton" x="10" y="129" label="load sound"/>
-
<mx:TextInput y="99" id="alarmSound" left="10" right="10"/>
-
<mx:Button x="105" y="129" label="play" id="playButton"/>
-
<mx:Button x="163" y="129" label="stop" id="stopButton"/>
-
<mx:Label x="10" y="73" text="Choose a file to play"/>
-
-
</mx:WindowedApplication>
Playing sound locally with AIR
Please post in the comments your use case to help the community. :)