2

I am new to flash (this is the first time I've ever used it or actionscript) and I'm trying to make a video player. The video player gets params from the embed code and pulls the videos from a folder on the server.

I've got the following code (I've removed everything that I'm 100% sure isn't causing my problem):

import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.events.FullScreenEvent;
import flash.events.Event;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextFormat;
import flash.media.SoundTransform;



var nc:NetConnection;
var ns:NetStream;
var ns2:NetStream;
var video:Video;
var video2:Video;
//get filename parameters from embed code
var filename:String = root.loaderInfo.parameters.filename;
var filename2:String = root.loaderInfo.parameters.filename2;
var t:Timer = new Timer(5000);
var duration;
var currentPosition:Number;
var st:Number;
var started:Boolean;

Object(this).mcPlay.buttonMode = true;
Object(this).mcPause.buttonMode = true;
Object(this).ScreenClick.buttonMode = true;
Object(this).mcMax.buttonMode = true;
Object(this).mcSwitcher.buttonMode = true;

Object(this).mcPause.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcPlay.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).ScreenClick.addEventListener(MouseEvent.CLICK,PlayPause);
Object(this).mcMax.addEventListener(MouseEvent.CLICK,Maximize);
Object(this).slideVolume.addEventListener(Event.CHANGE, ChangeVolume);
Object(this).mcSwitcher.addEventListener(MouseEvent.CLICK, ToggleSwitcher);

t.addEventListener(TimerEvent.TIMER, TimerComplete);
stage.addEventListener(MouseEvent.MOUSE_MOVE, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_DOWN, resetTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, resetTimer);
stage.addEventListener(Event.ENTER_FRAME, videoTimer);

if (!nc) start();
var IsPaused:String;

function start():void
{
    Object(this).slideVolume.maximum = 100;
    Object(this).slideVolume.value = 100;

    started = false;

    var tf:TextFormat = new TextFormat();
    tf.color = 0xFFFFFF;
    tf.bold = true;
    this.lblTime.setStyle("textFormat", tf);

    connect();
    t.start();
}

function connect():void
{
    nc = new NetConnection();
    nc.client = this;
    nc.addEventListener(NetStatusEvent.NET_STATUS, OnNetStatus);
    nc.connect(null);
}

function OnNetStatus(e:NetStatusEvent):void
{
    switch(e.info.code)
    {
        case "NetConnection.Connect.Success":
            if (!started)
            {
                started = true;
                stream();
            }
            else
            {
                finish();
            }
        break;
        default:
            finish();
        break;
    }
}

function stream():void
{
    ns = new NetStream(nc);
    ns.client = this;
    ns.bufferTime = 5;  // set the buffer time to 5 seconds

    if ((filename2 != null) && (filename2.length > 0))
    {
        ns2 = new NetStream(nc)
        //ns2.client = this;    //Uncomment to use ns2 vid for duration info
        ns2.bufferTime = 5;  // set the buffer time to 5 seconds
        startVideo(2);
        currentPosition = 1;    //Default

        ns.seek(0);
        ns2.seek(0);
    }
    else
    {
        this.mcSwitcher.visible = false;
        startVideo(1);

        ns.seek(0);
    }
}

function startVideo(num:Number):void
{
    var startVolume:SoundTransform = new SoundTransform();
    startVolume.volume = slideVolume.value / 100;

    if (num == 2)
    {
        video = new Video(320,180);
        video.x = 0;
        video.y = 90;
        addChild(video);
        video.attachNetStream(ns);
        ns.checkPolicyFile = false;
        ns.play(filename);  //path/filename
        setChildIndex(video,1);

        video2 = new Video(320,180);
        video2.x = 320;
        video2.y = 90;
        addChild(video2);
        video2.attachNetStream(ns2);
        ns2.checkPolicyFile = false;
        ns2.play(filename2);    //path/filename
        setChildIndex(video2,1);

        ns.soundTransform = startVolume;
        var videoVolumeTransform2:SoundTransform = new SoundTransform();
        videoVolumeTransform2.volume = 0;
        ns2.soundTransform = videoVolumeTransform2;
        ns2.receiveAudio(false);
    }
    else if (num == 1)
    {
        video = new Video(640,360);
        video.x = 0;
        video.y = 0;
        addChild(video);
        video.attachNetStream(ns);
        ns.checkPolicyFile = false;
        ns.play(filename);  //path/filename
        setChildIndex(video,1);

        ns.soundTransform = startVolume;
    }
    IsPaused = "playing";
    this.removeChild(mcPlay);
    setChildIndex(this.ScreenClick,0);
    setChildIndex(this.mcTitleOverlay,2);
}

function ShowControls ():void
{
    for (var i:int = 0; i < Object(root).numChildren; i++)
    {       
        switch (Object(root).getChildAt(i))
        {
            case mcPause:
                if (IsPaused != "paused")
                    Object(root).getChildAt(i).visible = true;
            break;
            case mcPlay:
                if (IsPaused != "playing")
                    Object(root).getChildAt(i).visible = true;
            break;
            case mcSwitcher:
                if ((filename2 != null) && (filename2.length > 0))
                    Object(root).getChildAt(i).visible = true;
            break;
            default:
                Object(root).getChildAt(i).visible = true;  //Bring back everything else
            break;
        }
        ScreenClick.y = 154;
    }
}

function videoTimer(e:Event):void
{
    var curTime = ns.time;  //Current time in seconds
    var curMinutes = Math.floor(curTime / 60);  //Get the minutes
    var curSeconds = Math.floor(curTime % 60);  //Get the leftover seconds

    var durMinutes = Math.floor(duration / 60);
    var durSeconds = Math.floor(duration % 60);

    //Add the zeroes to the begining of the seconds if it is needed.
    if (curSeconds < 10)
        curSeconds = "0" + curSeconds;

    if (durSeconds < 10)
        durSeconds = "0" + durSeconds;

    Object(this).lblTime.text = curMinutes + ":" + curSeconds + " / " + durMinutes + ":" + durSeconds;
}


function PlayPause (e:MouseEvent):void
{
    switch (IsPaused)
    {
        case "playing":
            IsPaused = "paused";
            this.mcPlay.visible = true;
            this.mcPause.visible = false;
            ns.togglePause();
            ns2.togglePause();
        break;
        case "paused":
            IsPaused = "playing";
            this.mcPause.visible = true;
            this.mcPlay.visible = false;
            ns.togglePause();
            ns2.togglePause();
        break;
        default:
            //
        break;
    }
}

The problem I have is small but frustrating (I've spend most of today trying to figure it out with zero progress made). It is thus: Everything works perfectly, except that when the videos load up and play, sound plays twice (for the video that has sound enabled). I am at my wits end trying to figure this out, any help would be greatly appreciated!

Thanks!

EDIT:

Ok, on further research (re-writing every function very simply and seeing if the problem goes away with the features) I've determined that the following function is the root of all evil (or at least my problems):

function startVideo(num:Number):void
{
    var startVolume:SoundTransform = new SoundTransform();
    startVolume.volume = Object(this).slideVolume.sldVol.value / 100;

    if (num == 2)
    {
        video = new Video(320,180);
        video.x = 0;
        video.y = 90;
        addChild(video);
        video.attachNetStream(ns);
        ns.checkPolicyFile = false;
        ns.play(filename);  //path/filename
        this.removeChild(btnPlay);
        setChildIndex(video,1);

        video2 = new Video(320,180);
        video2.x = 320;
        video2.y = 90;
        addChild(video2);
        video2.attachNetStream(ns2);
        ns2.checkPolicyFile = false;
        ns2.play("test.mp4");   //path/filename
        setChildIndex(video2,1);

        ns.soundTransform = startVolume;
        var videoVolumeTransform2:SoundTransform = new SoundTransform();
        videoVolumeTransform2.volume = 0;
        ns2.soundTransform = videoVolumeTransform2;
        ns2.receiveAudio(false);
    }
    else if (num == 1)
    {
        video = new Video(640,360);
        video.x = 0;
        video.y = 0;
        addChild(video);
        video.attachNetStream(ns);
        ns.checkPolicyFile = false;
        ns.play("test.flv");    //path/filename
        setChildIndex(video,1);

        ns.soundTransform = startVolume;
    }
    IsPaused = "playing";
    this.removeChild(btnPlay);
    setChildIndex(this.ScreenClick,0);
    //setChildIndex(this.mcTitleOverlay,2);
}

I shall persevere with my troubleshooting (I've isolated the problem, hopefully the next step is a solution!

Maverick
  • 4,449
  • 4
  • 36
  • 46
  • 2
    Now that's a big bunch of somehow confusing code there :) I've trimmed it down so that i could test it (FlashDevelop, FlashPlayer 11.4.402.278), and there's no audio doubling for me. I think it would be a good idea to isolate the problem, and supply working source files (working in terms of that they compile and run, and trigger the problem) including test videos. – ndm Oct 05 '12 at 16:33
  • Well, all i can say is that it works for me. I'm afraid that without being able to reproduce the problem i won't be able to help you. – ndm Oct 06 '12 at 10:52
  • @ndm The .fla has barely anything extra - I have currently commented everything except the above out of my file. Can I upload it here some anyway? Object wise I've got an overlay that shows the title of the video, images that I've set as MovieClips via F8 (and instanced with the scope-to tool). The only other thing I have is a black square that is click-able behind the video (and also extends to be a background when I hide the controls). I have tried multiple source files (.mp4 and .flv both), btw. Also, if I comment out the commands to play the video(s), I still get sound (but no video). – Maverick Oct 06 '12 at 10:53
  • Appreciate the help anyway. If you aren't getting the problem I must have done something weird outside the code, I haven't made any final graphics yet (placeholders from youtube screenies ftw), so maybe I'll just start over again... – Maverick Oct 06 '12 at 10:56
  • Confusing is the use of "playing twice" - do you mean litteraly that after video loads you can hear 2 same sound tracks beign played? I would suggest to use some already built video player (e.g. ca.turbulent.media.Pyro) which will encapsulate all the work needed for playing video (with or without audio). And all you have to do is the "skin". Also worth noting - when you try to play remote FLV file - load and stop - you can hear some audio played before the viedo will be stopped - I'm setting volume to 0 and then set it back when the FLV needs to be played. – Lukasz 'Severiaan' Grela Oct 07 '12 at 20:35
  • Yes, the audio is playing twice simultaneously. I'd also rather make my own player, rather than use one that exists already. – Maverick Oct 07 '12 at 22:11
  • It's your choice, however why reinvent the wheel? You will only worry about the look and feel and not how it works - and library like Pyro or others are already tested so there is less chances that it will fail like your implementation. – Lukasz 'Severiaan' Grela Oct 08 '12 at 07:41
  • Fixed! I re-wrote it from scratch (I know about 20x as much about flash as I did last week, so it only took a couple of hours) :). If anybody has any desire to see my code, I can post it here. Just let me know :). – Maverick Oct 09 '12 at 02:32

0 Answers0