适用于iOS的AS3 AIR - StageVideo无法正常工作

时间:2012-03-14 09:18:26

标签: ios actionscript-3 air

我正在尝试让StageVideo在我的应用中运行,但我遇到了问题。我需要一个接一个地播放一系列视频,第一个视频总是播放正常,但其他视频只播放音频,StageVideo将显示第一个视频的最后一帧。

就好像StageVideo已经冻结并正在播放视频,但我看不到它(只听到它)。我在这里发布了完整的代码:

我正在使用Adobe Air 3.2 beta在iPad2上进行测试,但也测试了3.1和相同的结果。

这是我的视频课程:

package  {

    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.events.Event;
    import flash.media.StageVideo;
    import flash.events.StageVideoEvent;
    import flash.events.StageVideoAvailabilityEvent;
    import flash.media.StageVideoAvailability;
    import flash.net.NetStream;
    import flash.net.NetConnection;
    import flash.geom.Rectangle;
    import flash.events.NetStatusEvent;

    public class SVideo2 extends MovieClip {

        public static const VIDEO_FINISHED:String = 'videoFinished';

        private var debugPanel:TextField;
        private var addedToStage:Boolean = false;

        private var videoFile:String;

        private var hwaEnabled:Boolean = false;

        private var video:StageVideo;
        private var ns:NetStream;
        private var nc:NetConnection;

        public function SVideo2() {
            addDebugPanel();

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(e:Event) :void{
            output('ADDED TO STAGE');
            addedToStage = true;
            stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onAvail);
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        public function playVideo(videoFile:String) :void{
            output('playVideo: '+videoFile);
            if(addedToStage){
                this.videoFile = videoFile;
                if(hwaEnabled){
                    startPlaying();
                }
                else{
                    output('HWA NOT AVAILABLE');
                }
            }
            else{
                output('NOT ON STAGE');
            }
        }

        private function onAvail(e:StageVideoAvailabilityEvent) :void{
            output(e.availability);
            if(e.availability == StageVideoAvailability.AVAILABLE){
                output('VIDEO AVAILABLE');
                hwaEnabled = true;
            }
        }

        private function startPlaying() :void{
            output('STARTING TO PLAY');
            video = stage.stageVideos[0];
            video.addEventListener(StageVideoEvent.RENDER_STATE, onRender);

            nc = new NetConnection();
            nc.connect(null);
            ns = new NetStream(nc);

            ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

            ns.client = this;

            video.attachNetStream(ns);
            ns.play(videoFile);
        }

        private function onRender(e:StageVideoEvent) :void{
            output('onRender');
            video.viewPort = new Rectangle(192, 50, 640, 480);
        }

        private function onNetStatus(e:NetStatusEvent) :void{
            output(e.info.code);
            if(e.info.code == 'Netstream.Play.Stop'){
                output('VIDEO STOPPED');
                dispatchEvent(new Event(VIDEO_FINISHED));
            }
        }

        private function addDebugPanel() :void{
            var tFormat:TextFormat = new TextFormat('Arial', 14, 0x000000, true);
            var tField:TextField = new TextField();

            tField.setTextFormat(tFormat);
            tField.multiline = true;
            tField.border = true;
            tField.borderColor = 0x000000;
            tField.background = true;
            tField.backgroundColor = 0xFFFFFF;

            tField.x = 0;
            tField.y = 568;
            tField.width = 1024;
            tField.height = 200;

            this.debugPanel = tField;
            addChild(debugPanel);
        }

        private function output(what:String) :void{
            debugPanel.appendText("\n"+what);
        }

        public function onXMPData(info:Object) :void{}
        public function onMetaData(info:Object) :void{}
        public function onCuePoint(info:Object) :void{}
        public function onPlayStatus(info:Object) :void{}

    }

}

这是我在框架中使用的代码:

import flash.display.Sprite;
import flash.events.MouseEvent;

var vid:SVideo2 = new SVideo2();
addChild(vid);

var btn:Sprite = new Sprite();
btn.graphics.beginFill(0x333333);
btn.graphics.drawRect(0, 0, 100, 40);
btn.x = 462;
btn.y = 518;
btn.width = 100;
btn.height = 40;

addChild(btn);

btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent) :void{
    var rand:String = String(Math.floor(Math.random() * 37));
    vid.playVideo('mp4/result_'+rand+'.mp4');
}

2 个答案:

答案 0 :(得分:4)

在与这个bug斗争并完全重写代码之后,我发现原因是因为我没有清除附加到StageVideo的netStream引用,即; StageVideo.attachNetStream(null);

这需要在播放每个新视频之前/之后清除。

  

在第二次调用attachNetStream()之前,请调用当前的   附加NetStream对象的close()方法。调用close()版本   所有资源,包括硬件解码器,参与播放   该视频。然后你可以用另一个调用attachNetStream()   NetStream对象或null。

答案 1 :(得分:0)

你设置backgroundAlpha = 0吗? - > s:应用backgroundAlpha = 0