1 Reply Latest reply on Oct 15, 2008 9:04 AM by Newsgroup_User

    Play-once-only via cookie (oops, wrong forum!)

    Bananzaman Level 1
      oops, sorry.
      deleted this message - 'twas a WMV file on the page that I wanted to prevent re-playing, so not appropriate for this forum. Can't seem to delete message, so I erased original.

        • 1. Re: Play-once-only via cookie (or "shared object")...?
          Level 7

          Between the choices you mentioned, the shared object approach is
          definitely the easier route. Nothing wrong with using JavaScript, of
          course, but it only makes the endeavor more complicated. By using the
          SharedObject class in ActionScript, you can manage your shared object (aka
          "Flash cookie") without having to mess with HTML or JavaScript at all.

          > What is confusing to me is how to alter my code to integrate one of
          > these solutions. [...] the "shared object" script; I'm not sure where I
          > plug in the pathway or how to link it to my own code.

          > // create a Shared Object
          > var my_so:SharedObject = SharedObject.getLocal("animationPlayed");

          So far, so good. If this shared object (animationPlayed) exists, its
          data is now stored in the my_so variable. If not, Flash creates that shared
          object from scratch, which means its still stored in the my_so variable, but
          doesn't contain data.

          > // if it's the first time here, i.e, <em>played</em> hasn't been set
          > already, see the animation
          > if (my_so.data.played == undefined)
          > {
          > // set played to true
          > my_so.data.played = true;
          > my_so.flush(); // write the data
          > }

          All instances of the SharedObject class have a data property, and that's
          where your custom properties are stored. In this case, you've got a custom
          property named played, which is accessed via the data property of your
          SharedObject instance (that is, via the variable) -- my_so.data.played. If
          that variable is undefined, it means the user is visiting for the first
          time, and the if() statement sets that property to true for the next visit.
          Strictly speaking, the flush() method isn't needed here, but it's fine to
          use it. (The SharedObject.flush() method saves the shared object's data
          immediately. If you don't use flush(), the data is stored when the SWF is
          closed, such as when someone exits the page.)

          > else
          > {
          > // you've already watched the animation before
          > // go to the last frame
          > gotoAndStop(_totalframes);
          > }

          And here's the opposite case, in which the user has already visited this
          SWF. This gotoAndStop() function sends the playhead to the last frame in
          the timeline.

          In your case, you want this code to be used for playing (or not playing)
          a video. You haven't mentioned what approach you're using for the video, so
          I'll assume you're using the FLVPlayback component. Even if you're not
          using that, the next few lines should illustrate how easy it is to
          re-purpose the SharedObject code for your own needs. Assuming your
          FLVPlayback's instance name is videoPlayer ...

          var my_so:SharedObject = SharedObject.getLocal("animationPlayed");
          if (my_so.data.played == undefined) {
          my_so.data.played = true;

          You don't really need the "else" clause at this point, because you're
          only interested in playing the video during the user's first visit. If that
          played property is undefined, then call the FLVPlayback component by its
          instance name and invoke the FLVPlayback.play() method on that instance.
          Then set played to true and save your data.

          By happenstance -- and this is what made your subject line catch my
          eye -- I just wrote a tutorial for Community MX on the topic of shared

          http://www.communitymx.com/abstract.cfm?cid=124BC (free content)

          It doesn't illustrate directly how to do anything but send the playhead
          to another location (just like your own sample code), but it may give you a
          bit more insight into how to use the SharedObject class. Let me know if you
          continue to have questions.

          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          "Luck is the residue of good design."