Help with pause/unpause sound problem

Hello again,
I’ve been struggling with the AS3 sound issues for some days now.

What I’m trying to do is for the player to be able to pause an animation with sound with the spacebar,and than return to the exact same time when pressing the spacebar again.
For that I’ve made a pause variable which stores the SoundChannel.position property and than using this variable each time I use the SoundChannel=Sound.play command.

The issue is that whenever I play the sound in a loop after I paused it (aka SoundChannel=Sound.play(pauseVariable,50) ) , the sound doesn’t reset itself when it arrives to the end and just keeps getting stuck in the last two seconds in the loop.

I used the SOUND_COMPLETE event which doesn’t fire up no matter where I’ve used it.

I followed an advice that the SOUND_COMPLETE event should be put directly after the player plays the sound again because the SoundChannel creates a new sound instance and overrides itself each time you play the sound.
It doesn’t work either.

I’ve tried another method of working without loops and doing them manually by comparing the soundChannel.position to the Sound.length property and when the sound ends to go back to the start,well it works alright but after 3 times the Sound or SoundChannel becomes a null and it crashes.

Help well be very much appreciated

Heres’ the code-
1st Method with loops and Event.SOUND_COMPLETE


package 
{

    
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.events.Event; 
    import flash.media.Sound;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.media.SoundChannel;

    public class MediumLevel extends MovieClip
    {
        //sound variables
        public var sound5:MediumLevelSound5 = new MediumLevelSound5();
        private var pauseStatus:Boolean;
        private var playHeadPosition:Number;
        public var soundIsComplete:Boolean;
        public var isPressed:Boolean;
        
       //sound channles
           public var sound1Channel:SoundChannel = new SoundChannel();
        


        public function MediumLevel() {
            addEventListener(Event.ADDED_TO_STAGE, init);
            
        }
        
        public function init(event:Event):void {
            gotoAndStop("activityStart");
            pauseStatus = true;
            playHeadPosition = 0;
            soundIsComplete = false;
            isPressed = false;
            
            stage.focus = this
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
            
               sound1Channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteFunction);
            addEventListener(Event.ENTER_FRAME, mediumLevelHandler);
        }
        
        
        public function mediumLevelHandler(event:Event):void {
            if (currentLabel == "activityEnd") {
                gotoAndStop("activityStart");
            }
            
            trace(sound1Channel.position, sound5.length);
            
        }
        
        public function keyDownFunction(event:KeyboardEvent):void {
            switch(event.keyCode) {
            case Keyboard.SPACE:
             play();
            
            startSound();    
            isPressed = true;
            break;    
          }
        }
        
        public function keyUpFunction (event:KeyboardEvent):void {
            switch(event.keyCode) {
            case Keyboard.SPACE:
            stopSound();
            stop();
            isPressed = false;
            break;
            }
        }
        
        
        
    [COLOR=Red]    public function soundCompleteFunction(e:Event):void {
            trace("complete");
        }[/COLOR]
        
        public function stopSound():void {
            if (!pauseStatus) {
             if (sound1Channel != null) { 
              sound1Channel.stop();
              playHeadPosition = sound1Channel.position;
              sound1Channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteFunction);
              pauseStatus = true;

             }
            }
        }
        
        public function startSound():void {
          if (pauseStatus==true) {
            if (sound1Channel != null) {          
             [COLOR=Red] sound1Channel = sound5.play(playHeadPosition, 20);//Play offset , number of loops
              sound1Channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteFunction);[/COLOR]

              pauseStatus = false;
            }
          }
          
        }
        

        
        
         
    }
}

The Second method with Manual loops


package 
{

    
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import flash.events.Event; 
    import flash.media.Sound;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.media.SoundChannel;

    public class MediumLevel extends MovieClip
    {
        //sound variables
        public var sound5:MediumLevelSound5 = new MediumLevelSound5();
        private var pauseStatus:Boolean;
        private var playHeadPosition:Number;
        public var soundIsComplete:Boolean;
        public var isPressed:Boolean;
        
       //sound channles
           public var sound1Channel:SoundChannel = new SoundChannel();
        


        public function MediumLevel() {
            addEventListener(Event.ADDED_TO_STAGE, init);
            
        }
        
        public function init(event:Event):void {
            gotoAndStop("activityStart");
            pauseStatus = true;
            playHeadPosition = 0;
            soundIsComplete = false;
            isPressed = false;
            
            stage.focus = this
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);

            addEventListener(Event.ENTER_FRAME, mediumLevelHandler);
        }
        
        
        public function mediumLevelHandler(event:Event):void {
            if (currentLabel == "activityEnd") {
                gotoAndStop("activityStart");
            }
            
            trace(sound1Channel.position, sound5.length);
            
        }
        
        public function keyDownFunction(event:KeyboardEvent):void {
            switch(event.keyCode) {
            case Keyboard.SPACE:
             play();
            
            startSound();    
            isPressed = true;
            break;    
          }
        }
        
        public function keyUpFunction (event:KeyboardEvent):void {
            switch(event.keyCode) {
            case Keyboard.SPACE:
            stopSound();
            stop();
            isPressed = false;
            break;
            }
        }
        
        
        
        public function stopSound():void {
            if (!pauseStatus) {
             if (sound1Channel != null) { 
              sound1Channel.stop();
              playHeadPosition = sound1Channel.position;
              pauseStatus = true;
            [COLOR=Red]  resetSound();[/COLOR]
             }
            }
        }
        
        public function startSound():void {
          if (pauseStatus==true) {
            if (sound1Channel != null) {          
              sound1Channel = sound5.play(playHeadPosition, 0);//Play offset , number of loops
             
              [COLOR=Red]resetSound();[/COLOR]
              pauseStatus = false;
            }
          }
          
        }
        

        
        public function resetSound():void {
        
           [COLOR=Red] if (sound1Channel.position == sound5.length) {
                trace("end");
                playHeadPosition = 0;
              }[/COLOR]
          
        }
         
    }
}