Skip to main content
Participant
August 18, 2014
Answered

Trouble with redirect after wav file is saved

  • August 18, 2014
  • 2 replies
  • 457 views

I have a photo/audio booth that we created with flash that take a photo and saves it then it records an audio testimonial and saves it.  My problem is that once the wav file is saved I want to redirect them to a thank you page but can't seem to figure out how to do it.  The following is the code that I am using to handle saving the wav file.

private function recordComplete(e:Event):void

  {

  var request:URLRequest = new URLRequest ( 'save-file.php' );

  var loader: URLLoader = new URLLoader();

  request.contentType = 'application/octet-stream';

  request.method = URLRequestMethod.POST;

  request.data = recorder.output;

  loader.load( request );

  //navigateToURL(new URLRequest(redirectPath), "_self")

  }

I tried to add the navigateToURL at the end and it sends to that page correctly but the wav file isn't saved.  I was wondering if there was a way to determine that the file is saved and then redirect to a thank you page?

thanks in advance!

-justin

This topic has been closed for replies.
Correct answer kglad

Okay I appreciate you hanging in here with me!  So I moved things around like you mentioned and was able to get rid of the error messages.  When I ran it again it creates my wav file but doesn't redirect to the thank you page.  The following is my complete AS File...  maybe I missed something that you suggested...

package

{

  import flash.display.Sprite;

  import flash.media.Microphone;

  import flash.system.Security;

  import org.bytearray.micrecorder.*;

  import org.bytearray.micrecorder.events.RecordingEvent;

  import org.bytearray.micrecorder.encoder.WaveEncoder;

  import flash.events.MouseEvent;

  import flash.events.Event;

  import flash.events.ActivityEvent;

  import fl.transitions.Tween;

  import fl.transitions.easing.Strong;

  import flash.net.FileReference;

  // the following are needed to save the file to the server instead

  import flash.net.URLRequest;

  import flash.net.URLRequestHeader;

  import flash.net.URLRequestMethod;

  import flash.net.navigateToURL;

  import flash.net.URLLoader;

  import flash.events.DataEvent;

  import flash.events.*;

  import flash.net.*;

  public class Main extends Sprite

  {

  private var mic:Microphone;

  private var waveEncoder:WaveEncoder = new WaveEncoder();

  private var recorder:MicRecorder = new MicRecorder(waveEncoder);

  private var recBar:RecBar = new RecBar();

  private var tween:Tween;

  private var fileReference:FileReference = new FileReference();

  private var request:URLRequest = new URLRequest ( 'save-file.php' );

  private var loader: URLLoader = new URLLoader();

  public var redirectPath:String = "http://localhost/thankyou.html";

  public function Main():void

  {

  recButton.stop();

  activity.stop();

  mic = Microphone.getMicrophone();

  mic.setSilenceLevel(0);

  mic.gain = 100;

  mic.setLoopBack(true);

  mic.setUseEchoSuppression(true);

  Security.showSettings("2");

  addListeners();

  }

  private function addListeners():void

  {

  recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);

  recorder.addEventListener(RecordingEvent.RECORDING, recording);

  recorder.addEventListener(Event.COMPLETE, recordComplete);

  activity.addEventListener(Event.ENTER_FRAME, updateMeter);

  loader.addEventListener(Event.COMPLETE,uploadCompleteDataHandler);

  }

  private function startRecording(e:MouseEvent):void

  {

  if (mic != null)

  {

  recorder.record();

  e.target.gotoAndStop(2);

  recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);

  recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);

  addChild(recBar);

  tween = new Tween(recBar,"y",Strong.easeOut, -  recBar.height,0,1,true);

  }

  }

  private function stopRecording(e:MouseEvent):void

  {

  recorder.stop();

  mic.setLoopBack(false);

  e.target.gotoAndStop(1);

  recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);

  recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);

  tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);

  }

  private function updateMeter(e:Event):void

  {

  activity.gotoAndPlay(100 - mic.activityLevel);

  }

  private function recording(e:RecordingEvent):void

  {

  var currentTime:int = Math.floor(e.time / 1000);

  recBar.counter.text = String(currentTime);

  if (String(currentTime).length == 1)

  {

  recBar.counter.text = "00:0" + currentTime;

  }

  else if (String(currentTime).length == 2)

  {

  recBar.counter.text = "00:" + currentTime;

  }

  }

  private function recordComplete(e:Event):void

   {

  request.contentType = 'application/octet-stream';

  request.method = URLRequestMethod.POST;

  request.data = recorder.output;

     loader.load( request );

   }

  private function uploadCompleteDataHandler(event:Event):void {

  navigateToURL(new URLRequest(redirectPath), "_self")

   }

  }

}


replace

  public var redirectPath:String = "http://localhost/thankyou.html";

with

// not sure this should be public but it won't matter for testing.

  public var redirectPath:String = "http://www.adobe.com";

2 replies

kglad
Community Expert
Community Expert
August 18, 2014

use:

  var request:URLRequest = new URLRequest ( 'save-file.php' );

  var loader: URLLoader = new URLLoader();

loader.addEventListener(Event.COMPLETE,uploadCompleteDataHandlerj);

  request.contentType = 'application/octet-stream';

  request.method = URLRequestMethod.POST;

  request.data = recorder.output;

private function recordComplete(e:Event):void

  {

  loader.load( request );

  }

private function uploadCompleteDataHandler(event:Event):void {

            navigateToURL(new URLRequest(redirectPath), "_self")

  }

prod21Author
Participant
August 18, 2014

I greatly appreciate the help...  when I swap the above code with what I previously had I get a bunch of errors:

I see that you moved the recordcomplete down but am I correct in assuming that I would need a private function around the top 6 lines?

kglad
Community Expert
Community Expert
August 18, 2014

no, they should be declared outside your constructor so they are available in your class and NOT local to a function (like they were in your code):

package{

import whatever..

.

.

.

public class whateverclass {

private var:urlloader = ...

etc

public function whateverclass(){

}

.

.

kglad
Community Expert
Community Expert
August 18, 2014

use a urlloader complete listener to determine when saving is complete.  then, in that listener, add your navigateToURL function.

prod21Author
Participant
August 18, 2014

I tried the following but didn't seem to do anything...

private function uploadCompleteDataHandler(event:DataEvent):void {

            navigateToURL(new URLRequest(redirectPath), "_self")

  }

I appreciate the help, and apologize my actionscript isn't very good so mostly have to rely on google and haven't had any luck.

thanks,

-justin