在 Android 平板电脑上使用 StageVideo 闪烁?

Posted

技术标签:

【中文标题】在 Android 平板电脑上使用 StageVideo 闪烁?【英文标题】:Flicker with StageVideo on Android tablet? 【发布时间】:2012-05-20 08:34:34 【问题描述】:

我有一个 stageVideo 类,我用它在平板电脑上播放视频,但每次播放视频时,平板电脑屏幕都会闪烁几次(变黑然后又出现大约四次左右)我想知道是什么可能导致这种情况。当我切换到播放视频的视图时,它会执行此操作。视频的 url 被传递给视频 mxml 视图。我正在使用 flex 4.6 和 android 平板电脑(EEE 变压器素数)。

package ios 
     
import flash.display.Sprite; 
import flash.display.StageAlign; 
import flash.display.StageQuality; 
import flash.display.StageScaleMode; 
import flash.events.Event; 
import flash.events.NetStatusEvent; 
import flash.events.StageVideoAvailabilityEvent; 
import flash.events.StageVideoEvent; 
import flash.geom.Rectangle; 
import flash.media.StageVideo; 
import flash.media.StageVideoAvailability; 
import flash.media.Video; 
import flash.net.NetConnection; 
import flash.net.NetStream; 


[Bindable] 
public class iOSStageVideo extends Sprite 
 
    private var videoPath:String; 
    private var videoWidth:Number; 
    private var videoHeight:Number; 
    private var _sv:StageVideo; 
    private var _vd:Video; 
    private var _obj:Object; 
    private var _ns:NetStream; 

    public function iOSStageVideo( path:String , w:Number , h:Number ):void
     
        videoPath = path; 
        videoWidth = w; 
        videoHeight = h; 
        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
     

    //stage is ready 
    private function onAddedToStage(e:Event):void
     
        stage.scaleMode = StageScaleMode.NO_SCALE; 
        stage.align = StageAlign.TOP_LEFT; 

        var nc:NetConnection = new NetConnection(); 
        nc.connect(null); 

        _ns =  new NetStream(nc); 
        _obj = new Object(); 

        _ns.client = _obj; _ns.bufferTime = 2; 
        _ns.client = _obj; 

        _obj.onMetaData = MetaData; 

        _sv = stage.stageVideos[0]; 
        _sv.viewPort = new Rectangle(0, 42, videoWidth , videoHeight ); 
        _sv.attachNetStream(_ns); 

        playVideo(); 
     


    //video is ready, play it 
    //public, can be called externally 
    public function playVideo():void 
        _ns.play( videoPath ); 
        _ns.addEventListener(NetStatusEvent.NET_STATUS, videoStatus); 
     



    //required metadata for stagevideo, even if not used 
    private function MetaData(info:Object):void  

    //get video status 
    private function videoStatus(e:NetStatusEvent):void 

        switch(e.info.code) 
            case "NetStream.Play.StreamNotFound": 
                //do something 
                break; 
            case "NetStream.Play.Start": 
                //do something 
                break 
            case "NetStream.Play.Stop": 
                //do something
                trace('the video has ended');
                stopVideo();
                break; 
            case "NetStream.Buffer.Empty": 
                //do something 
                break; 
            case "NetStream.Buffer.Full": 
                //do something 

                break; 
            case "NetStream.Buffer.Flush": 
                //do something 
                break; 
            case "NetStream.Play.Complete":
                //do something 
                break;
         
     

    //stop and clear the video 
    //public, can be called externally 
    public function stopVideo():void  
        trace("StopVideo is ran.");
        _ns.close();
        _ns.dispose(); 
        dispatchEvent( new Event('videoDone', true ) ); 

     

    public function stopVideoBack():void 
        _ns.close();
        _ns.dispose(); 
    
 

这是我在视频播放完毕时播放视频的视图代码

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="" backgroundAlpha="0" creationComplete="init(event)" addedToStage="onAddedToStage(event)"  >

<fx:Script>
    <![CDATA[

        import ios.iOSStageVideo;
        import mx.core.UIComponent;
        import mx.events.FlexEvent;



        protected var path:String = new String(""); 
        protected var backPage:String = new String("");
        protected var vid:iOSStageVideo = new iOSStageVideo( path , 1280 , 720 );
        private var arr:Array;
        //protected var vid:iOSStageVideo; 
        protected var container:UIComponent = new UIComponent(); 

        protected function init(event:FlexEvent):void
        
            // Sets up the back button to back to the right slide
            arr =  data as Array;
            trace(data);
            path = String(arr[0]);
            backPage = String(arr[1]) 

            //creates the video
            vid = new iOSStageVideo( path , 1280 , 720 );
            loadVideo();

            //takes out the trash when the back button is hit so the stage video is ready when the new view is loaded
            stage.addEventListener("keyDown", handleButtons, false,1);
            stage.addEventListener("keyUp", handleButtons, false, 1);
        

        override public function createReturnObject():Object 
            var returnedObject:Object = new Object();
            returnedObject.myValue = arr[2];
            trace("arr[2] ->" + arr[2])
            return returnedObject;
        


        protected function loadVideo():void
        
            //loades the video
            vid.addEventListener('videoDone' , videoStop); 
            container.width = stage.stageWidth; 
            container.height = stage.stageHeight; 

            addElement( container ); 
            container.addChild( vid ); 

        

        protected function playVideo(event:MouseEvent):void
        
            vid.playVideo();
        

        private function videoStop(e:Event):void 
                //removes container
                container.removeChild( vid ); 
                removeElement( container ); 
                navigator.popView();
        

        private function removeEverything():void 
            vid.stopVideoBack();
            try
            
                container.removeChild( vid ); 
                removeElement( container );
             
            catch(error:Error) 
            
                trace("error with container");
            
        

        protected function onAddedToStage(event:Event):void
                       
            if (stage.autoOrients) 
                stage.removeEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging);
                stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChanging, false, 100, true);
            
        

        private function orientationChanging(event:StageOrientationEvent):void 
            event.stopImmediatePropagation();
            if (event.afterOrientation == StageOrientation.ROTATED_LEFT  || event.afterOrientation == StageOrientation.ROTATED_RIGHT) 
                event.preventDefault(); 
               
        

        protected function handleButtons(event:KeyboardEvent):void
        
            if (event.keyCode == Keyboard.HOME) 
                // Handle Home button.
            
            else if (event.keyCode == Keyboard.BACK) 
                // Hanlde back button.
                removeEverything();
            
        

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:View>

编译器参数

-locale en_US
-swf-version=13 
-target-player=11.0
-define CONFIG::LOGGING false 
-define CONFIG::FLASH_10_1 true

已添加已更新代码以具有 StageVideoEvent 侦听器并简化为一个视图(还知道 renderMode 设置为 direct)

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="video" backgroundAlpha="0" creationComplete="init(event)" >
<fx:Script>
    <![CDATA[
        import mx.core.UIComponent;

        protected var videoPath:String = new String("video.mp4"); 
        private var videoWidth:Number = 1280;
        private var videoHeight:Number = 680; 

        private var stageVideoAvail:Boolean;
        private var sv:StageVideo;

        private function init(e:Event):void 
            trace("ran one");
            onAddedToStage(); 
        

        private function onAddedToStage():void 
            trace('test');
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT; 


               stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailability);

        

        private function onStageVideoAvailability( e : StageVideoAvailabilityEvent ) : void
        
            if (e.availability == StageVideoAvailability.AVAILABLE)
                stageVideoAvail = true;
                initVideo();
             else 
                stageVideoAvail = false;
            
        

        private function initVideo():void
        
            var obj:Object = new Object();
            var nc:NetConnection = new NetConnection();
            nc.connect(null);
            var ns:NetStream = new NetStream(nc);
            ns.client = obj;

            if(stageVideoAvail)
            
                sv = stage.stageVideos[0];
                sv.addEventListener(StageVideoEvent.RENDER_STATE, onRender);
                sv.attachNetStream(ns);
                trace('available');
            
            else
            
                var vid:Video = new Video(videoWidth, 768);
                addChild(vid);
                vid.attachNetStream(ns);
                trace('not');
            

            ns.play( videoPath );
        

        private function onRender(e:StageVideoEvent):void
        
            sv.viewPort = new Rectangle(0, 0, videoWidth, 768);
        

        public function onMetaData(e:Object):void
        

        

        public function onXMPData(e:Object):void
        

        

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:View>

此应用程序中唯一的其他视图只有一个带有 navigator.pushView(Copyofvideo,null,null); 的按钮当点击按钮时,屏幕会闪烁,然后播放视频。

【问题讨论】:

你能解决闪烁和黑屏的问题吗? 【参考方案1】:

闪烁的一个可能原因是您在致电_ns.play() 之前没有收听StageVideo 的可用性。通过收听StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY,您可以管理获得和失去StageVideo

代替

private function onAddedToStage(e:Event):void
 
    ...
    _sv = stage.stageVideos[0]; 
    _sv.viewPort = new Rectangle(0, 42, videoWidth , videoHeight ); 
    _sv.attachNetStream(_ns); 

    playVideo(); 
 

这样做

    private function onAddedToStage(e:Event):void
    
        stage.addEventListener( StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailability );
    

    private function onStageVideoAvailability( event : StageVideoAvailabilityEvent ) : void
    
        var available : Boolean = (event.availability == StageVideoAvailability.AVAILABLE);
        ...

        if ( available )
        
            addStageVideo();
        
        else
        
            //implement an alternative e.g. addRegularVideo();
        

        playVideo();
    

Adobe Developer Connection 在本教程中讨论了如何监听 StageVideo 的可用性:

Getting started with stage video

本教程提供了更多代码示例:

Creating a StageVideo Test in FDT with Flex 4.5 and FlashPlayer 10.2

【讨论】:

它似乎还在闪烁。我为 stageVideoAvailability 添加了一个事件侦听器,并尝试将其简化为一个视图。新代码位于添加的帖子底部。应该能够复制视图和编译器参数并将 renderMode 设置为直接添加视频并查看它。顺便谢谢你的帮助!【参考方案2】:

您是否尝试更改渲染模式?当舞台视频处于直接模式时,我遇到了问题。将其更改为 CPU 似乎可以解决我遇到的 StageVideo 问题。

【讨论】:

以上是关于在 Android 平板电脑上使用 StageVideo 闪烁?的主要内容,如果未能解决你的问题,请参考以下文章

在适用于 Android 的移动设备/平板电脑上使用 Leanback UI

Android Compose - 平板电脑上的应用程序在使用 LazyColumn 时崩溃

手机或平板电脑上的 android.jar 平台类/dex 文件在哪里?

YouTube IFrame API 播放方法在某些 Android 平板电脑上触摸之前不起作用

背景尺寸:封面在 Android 平板电脑上无法纵向工作

如何在电脑上安装一个Android虚拟机