Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.

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:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="{init()}">
  3.     <mx:Script>
  4.     <![CDATA[
  5.     import mx.controls.Alert;
  6.    
  7.     private var currentFile:File; // The currentFile opened (and saved) by the application
  8.     private var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile
  9.     private var defaultDirectory:File = File.documentsDirectory; // The default directory
  10.     public var reusableSoundChannel:SoundChannel = new SoundChannel();
  11.    
  12.     public function init():void {
  13.         loadButton.addEventListener(MouseEvent.CLICK, openFile, false, 0, true);
  14.         playButton.addEventListener(MouseEvent.CLICK, playSound, false, 0, true);
  15.         stopButton.addEventListener(MouseEvent.CLICK, stopSound, false, 0, true);
  16.     }
  17.    
  18.     /**
  19.     * Called when the user clicks the Open button. Opens a file chooser dialog box, in which the
  20.     * user selects a currentFile.
  21.     */
  22.     private function openFile(e:Event):void {
  23.         var fileChooser:File;
  24.         if (currentFile) {
  25.             fileChooser = currentFile;
  26.         }
  27.         else {
  28.             fileChooser = defaultDirectory;
  29.         }
  30.        
  31.         var filter1:FileFilter = new FileFilter("Sound or music or something", "*.mp3;*.wav;*.html;*.txt;*.xml");
  32.         fileChooser.browseForOpen("Open", [filter1]);
  33.         fileChooser.addEventListener(Event.SELECT, fileOpenSelected);
  34.     }
  35.    
  36.     /**
  37.     * Called when the user selects the currentFile in the FileOpenPanel control. The method passes
  38.     * File object pointing to the selected currentFile, and opens a FileStream object in read mode (with a FileMode
  39.     * setting of READ), and modify's the title of the application window based on the filename.
  40.     */
  41.     private function fileOpenSelected(event:Event):void {
  42.         currentFile = event.target as File;
  43.         alarmSound.text = currentFile.url;
  44.         currentFile.removeEventListener(Event.SELECT, fileOpenSelected);
  45.     }
  46.    
  47.     public function playSound(e:MouseEvent):void {
  48.         if (currentFile== null || !currentFile.exists) { Alert.show("Pick a file dude!"); return}
  49.         trace("playing sound " + currentFile.name)
  50.         //fileName = fileName.replace(/file/, "app");
  51.        
  52.         var location:URLRequest = new URLRequest(currentFile.url);
  53.         //var soundURL:String = "app:/" + fileName;
  54.         var soundURL:String = currentFile.name; 
  55.         var sound:Sound = new Sound();
  56.  
  57.         sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
  58.         sound.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError);
  59.        
  60.        sound.load(location);
  61.  
  62.     }
  63.    
  64.     public function stopSound(event:MouseEvent):void {
  65.         reusableSoundChannel.stop();
  66.     }
  67.     
  68.     private function onSoundLoadComplete(event:Event):void {
  69.         var localSound:Sound = event.target as Sound;
  70.         reusableSoundChannel = localSound.play();
  71.     }
  72.    
  73.     private function onSoundIOError(event:IOErrorEvent):void {
  74.         trace("The sound could not be loaded: " + event.text);
  75.     }
  76.  
  77.     ]]>
  78.     </mx:Script>
  79.    
  80.     <mx:Button id="loadButton" x="10" y="129" label="load sound"/>
  81.     <mx:TextInput y="99" id="alarmSound" left="10" right="10"/>
  82.     <mx:Button x="105" y="129" label="play" id="playButton"/>
  83.     <mx:Button x="163" y="129" label="stop" id="stopButton"/>
  84.     <mx:Label x="10" y="73" text="Choose a file to play"/>
  85.    
  86. </mx:WindowedApplication>

Playing sound locally with AIR

Please post in the comments your use case to help the community. :)

Leave a Reply