6 Replies Latest reply on Aug 16, 2008 9:19 AM by

    Sound Trouble

      newbee question... partial code below

      private var firstSound:Sound; // in class that extends MovieClip

      // set up grid and start game
      public function startMatchThree() {

      // load the sound
      firstSound = new Sound(new URLRequest("FirstSound.aiff"));
      firstSound.play(); // play first sound
      trace('Play Sound Now');

      My trace is working, but the sound is not playing... The sound file is in the same folder as the fla and as files. I've checked spelling and capitalization of file name, but the sound will not play. If I double-click on the sound from explorer it plays in real-player just fine. What am I doing wrong?

        • 1. Re: Sound Trouble
          RossRitchey Level 4
          It looks as though you are asking the sound to play before it is loaded.

          Here is your code edited from the "//load the sound" comment.

          firstSound = new Sound(new URLRequest("FirstSound.aiff"));
          firstSound.addEventListener(Event.COMPLETE,playSound);

          function playSound(evt:Event):void {
          trace("play sound now");
          (evt.target as Sound).play();
          }
          1 person found this helpful
          • 2. Re: Sound Trouble
            I'll try that, but I moved the play sound from where I had it as part of a mouse click event. I moved it just to test, I don't want it to play until the user clicks on something. But I'll try what you have there to see if I can get the sound to play. Then I'll try adding it back to the mouse click event.
            • 3. Re: Sound Trouble
              I tried a variation of what you suggested. But I'm still having trouble playing sound, although it is loaded... See code below

              package {
              import flash.display.*;
              import flash.events.*;
              import flash.text.*;
              import flash.utils.Timer;
              import flash.media.Sound;
              import flash.media.SoundChannel;
              import flash.net.URLRequest;

              public class MatchThree extends MovieClip {
              // constants
              static const numPieces:uint = 7;
              static const spacing:Number = 85;
              static const offsetX:Number = 75;
              static const offsetY:Number = 100;
              static const numRows:uint = 8;
              static const numCols:uint = 8;

              // game grid and mode
              private var grid:Array;
              private var gameSprite:Sprite;
              private var firstPiece:Piece;
              private var isDropping,isSwapping:Boolean;
              private var gameScore:int;
              private var firstSound:Sound;
              private var soundLoaded:Boolean = false;

              // set up grid and start game
              public function startMatchThree() {

              // load the sound
              firstSound = new Sound(new URLRequest("FirstSound.aiff"));
              firstSound.addEventListener(Event.COMPLETE,playSound);

              // other code cut from here

              public function playSound(evt:Event):void {
              soundLoaded = true;
              }

              // other code cut from here

              // player clicks on a piece
              public function clickPiece(event:MouseEvent) {
              var piece:Piece = Piece(event.currentTarget);

              // first one selected
              if (firstPiece == null) {
              piece.select.visible = true;
              firstPiece = piece;
              if (soundLoaded == true) {
              firstSound.play(); // play first sound
              }
              trace(soundLoaded);
              } else...
              // rest of code cut
              _______________________

              the trace shows true on soundLoaded, but the sound does not play
              what next?
              • 4. Re: Sound Trouble
                RossRitchey Level 4
                move the trace to before the if() to be sure that it is set to true before you check it. Otherwise I'm not sure where your problem is coming from. Your code makes sense, and looks correct.
                1 person found this helpful
                • 5. Sound Trouble
                  I moved the trace. It was a dumb place to put it (duh), but it still traced true and still no sound, even though I can play the sound through other sources.

                  I'm still trying to figure this out. The sound is loaded, but it doesn't play.
                  • 6. Sound Trouble
                    I have more infomation that might help troubleshoot this problem. I loaded a different sound MP3 and it worked. Is there some setting that I need to change to make the .aiff sound work?

                    FYI: I have the .aiff in the library of another flash project and thought I'd move it out to disk and load it with AS to keep the project size down and share the file. It works fine as part of the library of my other flash project.

                    I think I found my answer in Flash Help: "Although there are various sound file formats used to encode digital audio, ActionScript 3.0 and Flash Player support sound files that are stored in the mp3 format. They cannot directly load or play sound files in other formats like WAV or AIFF."

                    Although how does it play it fine when it is in the library?