Flex:还原Spark VideoDisplay流

时间:2011-12-08 03:43:34

标签: flex live-streaming flex-spark videodisplay

修改 如果有人能够至少告诉我如何在流断开连接时收到一个很棒的事件。

此控件的文档非常糟糕。我有一个应用程序将有一个实时视频流,我正在寻找一种方法,使VideoDisplay控件恢复其连接,以防出现任何这些特定情况:

  1. 应用程序启动,流尚未联机。
  2. 应用程序正在流式传输,用户已从互联网断开连接。
  3. 应用程序正在流媒体,视频服务器崩溃并重新启动。
  4. 我正在使用Wowza Media Server和Wirecast来测试它。 1和3不起作用,我不确定2号的确如此。我通过添加这个非常有问题的代码来完成第一项工作:

        protected function onMediaPlayerStateChange(event:MediaPlayerStateChangeEvent):void
        {
            if (event.state == MediaPlayerState.PLAYBACK_ERROR)
            {
                var videoSource:DynamicStreamingVideoSource = this.videoDisplay.source as DynamicStreamingVideoSource;
    
                try
                {
                    this.videoDisplay.source = null;
                    this.videoDisplay.source = videoSource;
                }
                catch (any:*) {}
            }
        }
    

    正如您所看到的,我需要一个try / catch块,因为对 source 的两次调用都会导致异常,但是在这些异常之前发生的任何事情似乎都会解决问题#1。这不能解决问题#3,因为当您停止视频服务器时,显然不会发生媒体状态更改事件。

    这是我的控制声明:

    <s:VideoDisplay id="videoDisplay" click="onVideoStreamClick(event)" mediaPlayerStateChange="onMediaPlayerStateChange(event)" muted="{this.videoMuted}" top="10" width="280" height="220" autoPlay="true" horizontalCenter="0">
        <s:source>
            <s:DynamicStreamingVideoSource id="videoSource" streamType="live" host="{FlexGlobals.topLevelApplication.parameters.videoStreamURL}">
                <s:DynamicStreamingVideoItem id="videoItemLow" streamName="{FlexGlobals.topLevelApplication.parameters.videoLow}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoLowBitrate}" />
                <s:DynamicStreamingVideoItem id="videoItemMedium" streamName="{FlexGlobals.topLevelApplication.parameters.videoMedium}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoMediumBitrate}" />
                <s:DynamicStreamingVideoItem id="videoItemHigh" streamName="{FlexGlobals.topLevelApplication.parameters.videoHigh}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoHighBitrate}" />
            </s:DynamicStreamingVideoSource>
        </s:source>
    </s:VideoDisplay>
    

    有谁知道如何让VideoDisplay从这些问题中恢复过来?感谢任何帮助。谢谢。

4 个答案:

答案 0 :(得分:1)

如果有人遇到这个问题,我就是这样解决的。您需要将视频源设置为空白图像以停止流式传输,否则永远不会停止。此解决方案适用于上述所有方案:

    private function resetVideo():void
    {           
        //save current source object
        this.videoEventsDisabled = true;
        var videoSource:DynamicStreamingVideoSource = this.videoDisplay.source as DynamicStreamingVideoSource;

        try //switch to blank image, only this will stop the video stream
        {
            this.videoDisplay.source = "assets/images/video_offline.png";
        }
        catch (any:*) {}

        //wait a few seconds and reset video source
        setTimeout(resetVideoSource, 2000, videoSource);
    }

    private function resetVideoSource(videoSource:DynamicStreamingVideoSource):void
    {
        this.videoEventsDisabled = false;
        this.videoDisplay.source = videoSource;
    }

    protected function onMediaPlayerStateChange(event:MediaPlayerStateChangeEvent):void
    {
        if (this.videoEventsDisabled)
        {
            return;
        }

        //something went wrong
        if (event.state == MediaPlayerState.PLAYBACK_ERROR)
        {
            resetVideo();
        }
    }

    protected function onCurrentTimeChange(event:TimeEvent):void
    {
        if (this.videoEventsDisabled)
        {
            return;
        }

        //if there was a number before, and its suddendly NaN, video is offline
        if (isNaN(event.time) && !isNaN(this.previousVideoTime))
        {
            resetVideo();
        }
        else //store event time for future comparisons
        {
            this.previousVideoTime = event.time;
        }
    }

MXML:

<s:VideoDisplay id="videoDisplay" click="onVideoStreamClick(event)" mediaPlayerStateChange="onMediaPlayerStateChange(event)" currentTimeChange="onCurrentTimeChange(event)" muted="{this.videoMuted}" top="10" width="280" height="220" autoPlay="true" horizontalCenter="0">
    <s:source>
        <s:DynamicStreamingVideoSource id="videoSource" streamType="live" host="{FlexGlobals.topLevelApplication.parameters.videoStreamURL}">
            <s:DynamicStreamingVideoItem id="videoItemLow" streamName="{FlexGlobals.topLevelApplication.parameters.videoLow}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoLowBitrate}" />
            <s:DynamicStreamingVideoItem id="videoItemMedium" streamName="{FlexGlobals.topLevelApplication.parameters.videoMedium}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoMediumBitrate}" />
            <s:DynamicStreamingVideoItem id="videoItemHigh" streamName="{FlexGlobals.topLevelApplication.parameters.videoHigh}" bitrate="{FlexGlobals.topLevelApplication.parameters.videoHighBitrate}" />
        </s:DynamicStreamingVideoSource>
    </s:source>
</s:VideoDisplay>

答案 1 :(得分:1)

作为变体,您可以从NetStream对象处理NetStream.Play.PublishNotify。

var _source:DynamicStreamingResource;
_source = new DynamicStreamingResource("rtmp://...", StreamType.LIVE);

var streamItems:Vector.<DynamicStreamingItem> = new Vector.<DynamicStreamingItem>();
streamItems.push(new DynamicStreamingItem(streamName, 0));

_source.streamItems = streamItems;

_rtmpDynamicStreamingNetLoader = new RTMPDynamicStreamingNetLoader();
_rtmpDynamicStreamingNetLoader.addEventListener(LoaderEvent.LOAD_STATE_CHANGE, rtmpDynamicStreamingNetLoaderStateChangeHandler);

var cvp:VideoDisplay = new VideoDisplay();

_source.mediaType = MediaType.VIDEO;

var videoElement:MediaElement = new VideoElement(_source, _rtmpDynamicStreamingNetLoader);

cvp.source = videoElement;

private function rtmpDynamicStreamingNetLoaderStateChangeHandler(event:LoaderEvent):void
{
    var netStream:NetStream = event.loadTrait["netStream"] as NetStream;
    if (netStream != null && !netStream.hasEventListener(NetStatusEvent.NET_STATUS)) {
        netStream.addEventListener(NetStatusEvent.NET_STATUS, netStreamNetStatusHandler, false, 0, true);
    }
}

private function netStreamNetStatusHandler(event:NetStatusEvent):void
{
    if (event.info.code == "NetStream.Play.UnpublishNotify") {

    }
    if (event.info.code == "NetStream.Play.PublishNotify") {

    }
 }

答案 2 :(得分:0)

我使用了Johnatan的代码以及JayPea的想法来解决我的问题!

我使用了Johnatan给出的所有内容,netStreamNetStatusHandler()中的以下小改动:

private function netStreamNetStatusHandler(event:NetStatusEvent):void
{
    trace(event.info.code);

    if (event.info.code == "NetStream.Play.PublishNotify")
    {
        try
        {
            cvp.source = ''; //Doesn't need to be a valid path. Empty string should suffice.
        }
        catch (error:Error)
        {
            trace('Video source error: ' + error.message);
        }

        cvp.source = videoElement;
    }
}

这会在流停止并重新启动时重置源,从而导致视频显示再次播放。

注意:视频显示的autoPlay属性需要设置为“true”。

答案 3 :(得分:0)

我知道这个问题很多年前被问过,但它仍然帮助我解决了我的问题,鼓励我潜入VideoDisplay的来源(因为显然没有“干净”的解决方案; - (

对于那些有同样问题的人,我的“解决方案”也很有帮助:

用户点击中止后我不得不重新启动相同的视频但后来决定再次播放。如果没有“重置”VideoDisplay,则会导致黑屏。

我发现在VideoDisplay中,在commitProperties()函数中调用了内部函数“setupSource()”时,没有定义thumnailSource,这是我解决问题的方法。

由于setupSource()函数无法公开访问,我使用了这个:

        myVideoDisplaySpark.enabled = false; // changes properties
        myVideoDisplaySpark.source = "";    // does not harm ;-)
        myVideoDisplaySpark.mx_internal::thumbnailSource = null; // forces a setupSource in commitProperties
        myVideoDisplaySpark.enabled = true;  // finally results in the call if setupSource

这至少是我所理解的;最后它对我有用; - )