Netstream音频播放两次

时间:2012-10-05 07:25:59

标签: actionscript-3 flash netstream

我是flash的新手(这是我第一次使用它或动作脚本)而我正在尝试制作视频播放器。视频播放器从嵌入代码中获取参数并从服务器上的文件夹中提取视频。

我有以下代码(我已经删除了我100%确定不会导致我的问题的所有内容):

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;
    }
}

我遇到的问题很小但很令人沮丧(我今天大部分时间都试图通过零进步来解决这个问题)。因此:一切都很完美,除了当视频加载和播放时,声音播放两次(对于启用声音的视频)。我最终试图弄明白这一点,任何帮助都将不胜感激!

谢谢!

编辑:

好的,在进一步的研究中(非常简单地重写每个函数并查看问题是否消失了)我已经确定以下函数是所有邪恶的根源(或者至少是我的问题):< / p>

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);
}

我将坚持我的故障排除(我已经解决了问题,希望下一步是解决方案!

0 个答案:

没有答案
相关问题