13 Replies Latest reply on May 30, 2007 6:15 AM by ncgames

    loading dynamic php url in flex

    ncgames
      When I comment out the two lines:
      import SoundPlayer;
      public var soundPlayer:SoundPlayer = new SoundPlayer (String(textService.lastResult));
      then it seem that the text from cookie.php is loaded correctly. but if i keep those two lines, then I have a blank flash application. if I remove the httpservice, and manually type the url new SoundPlayer ("song.mp3"); then the player works ok. what is wrong with the code i have?

      here is the code I have so far:

      <?xml version="1.0" encoding="utf-8" ?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="initializeHandler(event)">
      <mx:Script>
      <![CDATA[
      private function initializeHandler(event:Event):void {
      textService.send();
      }
      private function resultHandler(event:Event):void {
      textArea.text = String (textService.lastResult);
      }
      import SoundPlayer;
      public var soundPlayer:SoundPlayer = new SoundPlayer (String(textService.lastResult));
      ]]>
      </mx:Script>
      <mx:HTTPService id="textService" url="cookie.php" result="resultHandler(event)" />
      <mx:Text text="my player" id="playername" width="100%" />
      <mx:Button label="Play" click="soundPlayer.play()" />
      <mx:Button label="Stop" click="soundPlayer.stop()" />
      <mx:Button label="Pause" click="soundPlayer.pause()" />
      <mx:Label id="cookie" text="this is the cookie text" />
      <mx:TextArea id="textArea" />
      </mx:Application>

        • 1. Re: loading dynamic php url in flex
          chris.huston.t10 Level 3
          Try putting your soundPlayer assignment in your resultHandler.

          private function resultHandler(event:ResultEvent):void {
          textArea.text = String (event.result);
          soundPlayer:SoundPlayer = new SoundPlayer (String(event.result));
          }

          You need to do wait for the return from your HTTPService call before assigning the value to your soundPlayer variable.

          Vygo
          • 2. Re: loading dynamic php url in flex
            ncgames Level 1
            when putting it there I get these errors:

            Error: Implicit coercion of a value of type SoundPlayer to an unrelated type Class.
            Error: Illegal assignment to class SoundPlayer.
            soundPlayer:SoundPlayer = new SoundPlayer (String(textService.lastResult));

            here is the code that results in this error:
            <?xml version="1.0" encoding="utf-8" ?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="initializeHandler(event)" backgroundColor="white">
            <mx:Script>
            <![CDATA[
            import SoundPlayer;
            private function initializeHandler(event:Event):void {
            textService.send();
            }
            private function resultHandler(event:ResultEvent):void {
            textArea.text = String (textService.lastResult);
            soundPlayer:SoundPlayer = new SoundPlayer (String(textService.lastResult));
            }
            ]]>
            </mx:Script>
            <mx:HTTPService id="textService" url="cookie.php" result="resultHandler(event)" />
            <mx:Text text="my player" id="playername" width="100%" />
            <mx:Button label="Play" click="soundPlayer.play()" />
            <mx:Button label="Stop" click="soundPlayer.stop()" />
            <mx:Button label="Pause" click="soundPlayer.pause()" />
            <mx:Label id="cookie" text="this is the cookie text" />
            <mx:TextArea id="textArea" />
            </mx:Application>
            • 3. Re: loading dynamic php url in flex
              chris.huston.t10 Level 3
              Try this:

              <?xml version="1.0" encoding="utf-8" ?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="initializeHandler(event)" backgroundColor="white">
              <mx:Script>
              <![CDATA[
              import SoundPlayer;
              public var soundPlayer:SoundPlayer;
              private function initializeHandler(event:Event):void {
              textService.send();
              }
              private function resultHandler(event:ResultEvent):void {
              textArea.text = String (event.result);
              soundPlayer = new SoundPlayer (String(event.result));
              }
              ]]>
              </mx:Script>
              <mx:HTTPService id="textService" url="cookie.php" result="resultHandler(event)" />
              <mx:Text text="my player" id="playername" width="100%" />
              <mx:Button label="Play" click="soundPlayer.play()" />
              <mx:Button label="Stop" click="soundPlayer.stop()" />
              <mx:Button label="Pause" click="soundPlayer.pause()" />
              <mx:Label id="cookie" text="this is the cookie text" />
              <mx:TextArea id="textArea" />
              </mx:Application>

              Vygo
              • 4. Re: loading dynamic php url in flex
                ncgames Level 1
                I tried that, but I get this:

                Error: Type was not found or was not a compile-time constant: ResultEvent.
                private function resultHandler(event:ResultEvent):void {

                how can I include ResultEvent as a compile time constant?
                • 5. Re: loading dynamic php url in flex
                  ncgames Level 1
                  ok I got this to work by adding:
                  import mx.rpc.events.ResultEvent;

                  everything seems to work, but the sound doesn't stream very well... it streams on IE6, but it crashes firefox. using the flash9 debugger in IE, no messages came up. any ideas on why this would cause firefox to crash?
                  • 6. Re: loading dynamic php url in flex
                    chris.huston.t10 Level 3
                    It is hard to say why Firefox is crashing without having a look at your SoundPlayer class code. I have a couple of aps that stream sound, both mp3 files and via FMS, in both IE and Firefox without any problem. Can you post a link to your ap and I can test it in Firefox to see if it crashes?

                    Vygo
                    • 7. loading dynamic php url in flex
                      ncgames Level 1
                      ok, I just checked. it works in firefox and IE in windows, but it seems the flash player I have installed in linux is the wrong version. which version of flash should be able to play flex generated swf files in linux? (firefox in linux crashes when displaying this flash application...) (i have flash 9.0.31.0 installed in linux)

                      here is the link to test out the player:
                      http://nc-games.com/cookie/test.php
                      • 8. Re: loading dynamic php url in flex
                        chris.huston.t10 Level 3
                        Your ap works on Windows IE 7 and Windows Firefox but freezes my Linux (Cent OS 4, flash 9.0.31.0) Firefox. I am able to play mp3 files in my Linux Firefox with other Flex created swf files, so the problem looks like it is with your SoundPlayer code. Can you post that code so we can have a look at what might be causing the problem?

                        Vygo
                        • 9. Re: loading dynamic php url in flex
                          ncgames Level 1

                          here is the SoundPlayer, hope someone can help me spot what's wrong with the code:

                          package
                          {
                          import flash.media.SoundChannel;
                          import flash.media.Sound;
                          import flash.net.URLStream;
                          import flash.events.Event;
                          import flash.net.URLRequest;
                          import flash.events.EventDispatcher;

                          public class SoundPlayer extends EventDispatcher
                          {
                          [Bindable]
                          private var _sound:Sound;
                          private var _soundChannel:SoundChannel;
                          private var _currentPosition:Number;

                          public function SoundPlayer(url:String)
                          {
                          _sound = new Sound(new URLRequest(url));
                          play();
                          }

                          public function stop():void
                          {
                          _soundChannel.stop();
                          _currentPosition = 0;
                          }

                          public function pause():void
                          {
                          _currentPosition = _soundChannel.position;
                          _soundChannel.stop();
                          }

                          public function play():void
                          {
                          if(_soundChannel != null)
                          {
                          _soundChannel.stop();
                          }
                          _soundChannel = _sound.play(_currentPosition);
                          }
                          }
                          }
                          • 10. Re: loading dynamic php url in flex
                            chris.huston.t10 Level 3
                            Your _currentPosition variable was not initialized before calling the play function which was causing problems with the linux player. The code below now works for me on linux.

                            public function SoundPlayer(url:String)
                            {
                            _sound = new Sound(new URLRequest(url));
                            _currentPosition = 0;
                            play();
                            }

                            Vygo
                            • 11. loading dynamic php url in flex
                              ncgames Level 1
                              I tried adding _currentPosition = 0;
                              so the play code now looks like this:

                              public function SoundPlayer(url:String)
                              {
                              _sound = new Sound(new URLRequest(url));
                              _currentPosition = 0;
                              play();
                              }


                              but it still hangs on my linux box...

                              the newly compiled swf is uploaded and you can go to the same link to try it again... perhaps I've done something wrong at compile time?
                              this is what i did:
                              /opt/flex_sdk/bin/compc --source-path ./ --include-classes SoundPlayer -output output.swc
                              unzip output.swc and extract library.swf
                              rename library.swf to SoundPlayer.swf
                              put player2.swf and SoundPlayer.swf and test mp3 in same folder.
                              player works in winxp, but not in linux
                              • 12. Re: loading dynamic php url in flex
                                chris.huston.t10 Level 3
                                Here is a link to my ap which is working in both windows and linux. You can right click and choose View Source to see the complete code which hopefully will be of use in finding why your code is still hanging with linux. I tried your link but it still doesn't work for me in linux either.

                                http://www.mgu.ac.jp/eibunka/tester/tester.html

                                I added an event listener to SoundPlayer to check if the sound had finished playing so that the _currentPosition would reset to 0. Otherwise, the play function would only begin playing from the previously paused position rather than the beginning.

                                Vygo
                                • 13. Re: loading dynamic php url in flex
                                  ncgames Level 1
                                  thanks. after seeing your code, i found the problem was in the order of operations. importing the results needed to be before importing the sound player.

                                  in the flash player, this caused the error:
                                  <![CDATA[
                                  import SoundPlayer;
                                  import mx.rpc.events.ResultEvent;


                                  it should be like this:
                                  <![CDATA[
                                  import mx.rpc.events.ResultEvent;
                                  import SoundPlayer;