Skip to main content
Participant
August 25, 2009
Answered

Need audio to not play when moved to new position for audio player

  • August 25, 2009
  • 1 reply
  • 554 views

I have an audio player that has a custom timeline and playhead. I am using a start and stop drag to move in the time line but the only way i can make it work is to have it play when ever I release the play head. I would like it to stay stopped if the player is paused and play if the player was playing.

Thanks in advanced.

var XMLLoaded:XML = new XML();
XMLLoaded.ignoreWhite = true;
var timesLoaded:Number = 0;
this.createEmptyMovieClip("sound_mc", this.getNextHighestDepth);
var sound_mc:Sound = new Sound();
var finished:Boolean = false;
var XMLSnippits:XML = new XML();
var sStart:Array = new Array;
var sText:Array = new Array;
var sS;
var audioFileLoaded:Boolean = false;
var fileToLoadLink = _root.metaUrl;
var callFile:String;
var XMLMetaData:String;
var totalSecs:Number;
var snippitLength:Number;
var XMLTimer;
var soundLength;
var currentTime;
var audioTime;
var timeUpdate;
var amountLoaded;
var time;
var position;
var timerStarted:String;
var leftLimit;
var rightLimit
var secLength:Number;
var min;
var secs;
var playHeadPlaying:Boolean = false;
var pausedInterval:Number;


play_btn._visible = true;
pause_btn._visible = false;
button_background._visible = true;
timeline.loader.audioLoaded._width = 0;
dash._visible = false;
audioTotal._visible = false;
cTime._visible = false;


function startInterval():Void {
         sS = setInterval(soundStatus, 100);
         trace("startInterval is Running");
}
   
function stopInterval():Void {
        clearInterval(sS);
}
   
   
function soundStatus(){
        amountLoaded = sound_mc.getBytesLoaded() / sound_mc.getBytesTotal();
        time = sound_mc.duration;
        position = sound_mc.position;
        timeline.loader.audioLoaded._width = amountLoaded * 378;
        timeline.playHead._x = position / time * 378;
        //trace("Interval is playing");
        }
   

// Load the Audio File

function loadAudioFile():Void{   
   
    trace("loadAudioFile is being run");
   
   
   
    XMLLoaded.onLoad = function(bSuccess:Boolean):Void {
        timerStarted = "no";
        if (bSuccess){
            if(XMLLoaded.firstChild.childNodes[0].childNodes[0].nodeValue == "yes"){
                callFile = XMLLoaded.firstChild.childNodes[1].childNodes[0].nodeValue;
                trace(timerStarted + " timer status");
                clearInterval(XMLTimer);
                audioFileLoaded = true;
                sound_mc.loadSound(callFile, false);
                startInterval();
                //getURL("javascript:alert('"+callFile+"')", '');
               
                sound_mc.onLoad = function (bSucess:Boolean):Void{
                    if (bSucess) {
                        this.start();
                        playHeadPlaying = true;
                        words.htmlText = "Please choose a marker";
                        play_btn._visible = false;
                        pause_btn._visible = true;
                        button_background._visible = true;
                        timeline.playHead.enabled = true;
                        play_btn._alpha = 100;
                        play_btn.enabled = true;
                        button_background._alpha = 100;
                        clearInterval(XMLTimer);
                        if(snippitLength > 0){
                            addMarkers();
                            trace(snippitLength + " add markers");
                        }else{
                            addTimer();
                            trace(snippitLength + " add timer");
                        }
                    }
                }
               
            }else{
                if(timesLoaded == 10){
                    trace("There has been an Error Loading this file");
                    clearInterval(XMLTimer);
                }else{
               
                }
            }
        }
    }

    function loadXML():Void {
        XMLMetaData = XMLSnippits.firstChild.childNodes[2].childNodes[0].nodeValue;
        XMLLoaded.load(XMLMetaData);
        //XMLLoaded.load("call-process-yes.xml");
        timesLoaded += 1
        trace("XML has been loaded " + timesLoaded);
        XMLTimer = setInterval(loadXML, 3000);
    }
   
   
    loadXML();
}


// Loads Meta Data XML
function fileAdded():Void {


    trace(sStart);
    timeline.playHead.enabled = false;
    words.html = true;
   
    XMLSnippits.ignoreWhite = true;

    XMLSnippits.onLoad = function(bSucess:Boolean):Void{
        if (bSucess){
            snippitLength =  XMLSnippits.firstChild.childNodes[3].childNodes.length;
            trace(snippitLength + " this is the length");
            for(var j:Number = 0; j < snippitLength; j++){
                sStart = XMLSnippits.firstChild.childNodes[3].childNodes.childNodes[0].childNodes[0].nodeValue;
                sText = XMLSnippits.firstChild.childNodes[3].childNodes.childNodes[4].childNodes[0].nodeValue;
            }
            //var sStart:Number = 53;
            //var sEnd:Number = 234;
            trace("Sucess");
            //getURL("javascript:alert('"+fileToLoadLink+"')", '');
            trace(sStart);
        }else{
            trace("Failed");
    }
}

    //XMLSnippits.load(fileToLoadLink);
    XMLSnippits.load("call-meta-data.xml");
       
}

sound_mc.onSoundComplete = function():Void {
        finished = true;
        play_btn._visible = true;
        pause_btn._visible = false;
        words.htmlText = "Please choose a marker";
       
    }
   
    play_btn.onRelease = function():Void{
        if(audioFileLoaded){
            if(finished){
                sound_mc.start();
                finished = false;
            }else{
                sound_mc.stop();
                sound_mc.start(sound_mc.position/1000);
            }
            pause_btn._visible = true;
            play_btn._visible = false;
            playHeadPlaying = true;
            clearInterval(pausedInterval);
        }else{
            loadAudioFile();
            words.htmlText = "Loading your call";
            play_btn._alpha = 45;
            play_btn.enabled = false;
            button_background._alpha = 45;
        }
        trace(sStart + " is the sStart array");
    }   
   

    pause_btn.onRelease = function():Void{
   
        sound_mc.stop();
        pause_btn._visible = false;
        play_btn._visible = true;
        playHeadPlaying = false;
        pausedInterval = setInterval(updatePlayHead, 100);
    }
   

    timeline.playHead.onPress = function ():Void{
        startDragger();
       
    }
   
    timeline.playHead.onRelease = function ():Void{
        stopDragger();
        if(playHeadPlaying){
           
        }else{
            sound_mc.stop
        }
       
    }
   
    timeline.playHead.onReleaseOutside = function ():Void {
        stopDragger();
    }
   
    function startDragger():Void {
        stopInterval();
        leftLimit = timeline.loader._x;
        rightLimit = timeline.loader._width + timeline.loader._x;
        sound_mc.stop();
        play_btn._visible = false;
        pause_btn._visible = true;
        startDrag(timeline.playHead, false, leftLimit, timeline.loader._y, rightLimit, timeline.loader._y);
       
    }
   
    function stopDragger():Void {
        soundLength = sound_mc.duration / 1000;
        stopDrag();
        sound_mc.start(timeline.playHead._x / timeline._width * soundLength);
        trace("seconds equal:" + timeline.playHead._x / timeline._width * soundLength);
        startInterval();
    }
   
    function addMarkers():Void {
        totalSecs = XMLSnippits.firstChild.childNodes[1].childNodes[0].nodeValue;
        secLength = timeline.loader._width / totalSecs;
        for(var i:Number = 0; i < sStart.length; i++){
            timeline.loader.attachMovie("marker", "marker" + i, i);
            timeline.loader["marker" + i]._x = sStart * secLength;
            timeline.loader["marker" + i]._y = - 3;
            // trace(i);
            //trace("marker" + i + "is being made");
            trace(timeline.loader["marker" + i]._name);
        }
    }
function addTimer():Void {
    timeUpdate = setInterval(checkTime, 500);
    audioTime = convertSecs(sound_mc.duration/1000);
    audioTotal.text = audioTime;
    trace(currentTime);
    words._visible = false;
    dash._visible = true;
    cTime._visible = true;
    audioTotal._visible = true;
}

function convertSecs(sec:Number):String{
    sec = Math.round(sec);
    min = Math.floor(sec/60);
    secs = sec - (min*60);
    secs = String(Math.round(secs*100)/100);
    if(Number(secs) < 10){
        secs = "0"+secs;
    }
    return min+":"+secs;
}

function checkTime():Void {
    currentTime = convertSecs(sound_mc.position/1000);
    cTime.text = currentTime;
}

function updatePlayHead():Void{
   
}
fileAdded();

This topic has been closed for replies.
Correct answer kglad

if the player is paused and then i drag the playhead and push play it starts playing from where i tried to drag the playhead to.


clear your pausedInterval.

1 reply

kglad
Community Expert
Community Expert
August 25, 2009

use the play_btn._visible property to determine whether to start your sound.  ie, if true, don't start it.  if false, start the sound.

brostdAuthor
Participant
August 25, 2009

So here is what I did. I did an if statement on the stopDragger function to see if the play button is visible. but when it is false it jumps back to where I start dragging it from. Any ideas?

This is what I changed from above:

function stopDragger():Void {
        soundLength = sound_mc.duration / 1000;
        stopDrag();
        if(play_btn._visible == true){
            //sound_mc.start(timeline.playHead._x / timeline._width * soundLength);
            trace("the play button is visible: " + play_btn._visible);
            sound_mc.stop();
            trace(play_btn._visible);
        } else{
            trace("the play button is visible: " + play_btn._visible);
            sound_mc.start(timeline.playHead._x / timeline._width * soundLength);
        }
        trace("seconds equal:" + timeline.playHead._x / timeline._width * soundLength);
        startInterval();
    }

kglad
Community Expert
Community Expert
August 25, 2009

does the sound continue playing from the correct position after you stop dragging?