4 Replies Latest reply on Jun 12, 2006 6:08 PM by kglad

    Streaming MP3 only plays after page refresh

    strife.dk
      I have this problem which is driving me mad... I'm have a player on a site, but the music won't start untill I refresh the page. It seems like the player have to be cached before it will play the music. And it is the same problem in IE and FF...

      Frame 1: Loading the XML
      // Create container for XML
      var xmlMusic:XML = new XML();
      // Ignore whitespace in XML file
      xmlMusic.ignoreWhite = true;
      // Load XML file into flash
      xmlMusic.load("path to xml (Censored to protect customer name)");

      Frame 2: Making sure XML is loaded before moving on
      stop();
      // If XML not loaded go to frame 2 to make sure XML is loaded before it is used
      var xmlloadedBytes:Number = xmlMusic.getBytesLoaded();
      var xmltotalBytes:Number = xmlMusic.getBytesTotal();

      xmlMusic.onLoad = function(bSuccess:Boolean):Void {
      if (bSuccess) {
      if (! xmlloadedBytes == xmltotalBytes) {
      _root.gotoAndPlay(2)
      } else {
      _root.gotoAndStop(4)
      }

      } else {
      _root.gotoAndPlay(2)
      }
      }

      Frame4: The player
      // Get outer node
      var xmlFiles:XMLNode = xmlMusic.firstChild;
      // How many files is there
      var lengthMusic:Number = xmlFiles.childNodes.length;
      // Create array to hold files
      var musicList:Array = new Array();
      // Initiate status text
      var musicStatus:String = "";

      // Put file info into array
      for (var i:Number = 0; i<lengthMusic; i++) {

      // Path to file ( http://www.domain.com/folder/)
      var xmlFilePath:String = xmlFiles.childNodes .attributes.filePath;
      // filename (file.mp3)
      var xmlFileName:String = xmlFiles.childNodes
      .attributes.fileName;
      // To show name artist and trackname
      var xmlFileShowName:String = xmlFiles.childNodes .attributes.fileShowName;
      // Insert into array
      musicList.push({filePath:xmlFilePath, fileName:xmlFileName, fileShowName:xmlFileShowName});

      }
      // Set i to 0 to load the first song in the array
      i = 0;
      // Load the music into flash
      function loadMusic():Void {
      // Create container for mp3 file, if container exists, delete it and create new
      if (myMP3){
      myMP3.stop();
      delete myMP3;
      myMP3 = new Sound(this);
      } else {
      myMP3 = new Sound(this);
      }
      // Load streaming into object
      myMP3.loadSound(musicList
      ["filePath"] + musicList .fileName, true);
      myMP3.start();
      myMP3.setVolume(15);
      musicStatus = "playing"
      }

      // Start playing song immediately
      loadMusic();

      // Pause Button
      _root.musicPause.onRelease = function():Void {
      var musicStatusSwitch:String = musicStatus;
      switch (musicStatusSwitch) {
      case "paused" :
      myMP3.start(pauseTime/1000);
      musicStatus = "playing";
      break;
      case "playing" :
      myMP3.stop();
      pauseTime = myMP3.position;
      musicStatus = "paused";
      break;
      }
      }
      // Play Button
      _root.musicPlay.onRelease = function():Void {
      var musicStatusSwitch:String = musicStatus;
      switch (musicStatusSwitch) {
      case "playing" :
      break;
      case "paused" :
      myMP3.start(pauseTime/1000);
      musicStatus = "playing";
      break;
      case "stopped" :
      loadMusic();
      break;
      }
      }
      // Stop Button
      _root.musicStop.onRelease = function():Void {
      myMP3.stop();
      musicStatus = "stopped";
      delete myMP3;
      }

      onEnterFrame = function ():Void {
      // After track ends, continue with the next
      myMP3.onSoundComplete = function():Void {
      if (i>=0 && i<lengthMusic-1) {
      i += 1;
      } else {
      i = 0;
      }
      loadMusic();
      }
      };
      // Stop playhead so main movie doesn't loop
      stop();