2 zen qiwei zen_qiwei 于 2013.12.05 15:18 提问

js调用flex的开启摄像头的方法失败

我用JavaScript调用Flex方法开启摄像头的方法总是失败。但是Flex直接在CreatComplete方法里面调用开启摄像头的方法是成功的。有人说是安全沙箱的问题,我在我的asp.net根目录下扔了一个crossdomain.xml文件也没用。请大神帮助提供解决的方案。
flex代码:

public function init():void{
            ExternalInterface.addCallback("openCamera", setupCamera);
        }

        //初始化摄像头
        public function setupCamera():void{
            //启动摄像头
            cam = Camera.getCamera(); 
            if(cam != null){
                cam.addEventListener(StatusEvent.STATUS,onStatusHandler);
                cam.addEventListener(ActivityEvent.ACTIVITY,onActiveHandler);
                cam.setMode(320,240,30);
                cam.setQuality(0,100);  //设置清晰度
                vi = new Video();
                vi.width = 320;
                vi.height = 240;
                vi.attachCamera(cam);
                vdisplay.addChild(vi);
            }
        }
               private function onActiveHandler(event:ActivityEvent):void
        {
            if(!cam.muted){    //判断摄像头存不存在
                startRec.enabled = true;
            }else{
                Alert.show("错误2:无法链接到活动摄像头!");
            }
            cam.removeEventListener(StatusEvent.STATUS,onStatusHandler);
        }

        private function onStatusHandler(event:StatusEvent):void{
            if(!cam.muted){    //判断摄像头存不存在
                startRec.enabled = true;
            }else{
                Alert.show("错误1:无法链接到活动摄像头!")
            }
            cam.removeEventListener(StatusEvent.STATUS,onStatusHandler);
        }

JS:

<script type="text/javascript" language=javascript>
    function openCamera() {
        document.getElementById("v").openCamera();
    }

html:














2个回答

u013054049
u013054049   2013.12.05 15:30

1.Flex里面使用ExternalInterface.call(“Js函数名称”,参数)进行调用javascript方法,其返回的值就是Js函数所返回的值。

2.在初始化方法中使用ExternalInterface.addCallback(“注册的方法名”,As中的函数名)进行注册,“注册的方法名”可以再javascript中直接调用

3.在js中,就可以用document.getElementById(“Flash在Html中的ID”).注册的方法名(参数)进行调用,当然,默认”Flash在Html中的ID”就是Flex文件的名称,例如,我们这里就是SampleApp,因为我们的Flex文件就是SampleApp.mxml

//Flex代码
<?xml version="1.0" encoding="utf-8"?>
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="application1_creationCompleteHandler(event)"
minWidth="955" minHeight="600">

<fx:Script>
    <![CDATA[
        import flash.external.*;

        import mx.controls.Alert;
        import mx.events.FlexEvent;
        protected function text_clickHandler(event:MouseEvent):void
        {
            myFunc();
        }

        public function myFunc():void 
        {
            Alert.show("Invoke Ok");                
        }

        public function getValue():Number 
        {
            return 84;        
        }

        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            ExternalInterface.addCallback( "myFlexFunction" ,myFunc);
            ExternalInterface.addCallback( "getValue" ,getValue);
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>

<mx:Button id="Test" label="Hello" click="text_clickHandler(event)">

</mx:Button>

/s:Application

//HTML代码,Flex自动生成的代码中小改动。


<!-- SWFObject's dynamic embed method replaces this alternative HTML content with Flash content when enough
JavaScript and Flash plug-in support is available. The div is initially hidden so that it doesn't show
when JavaScript is disabled.
-->


To view this page ensure that Adobe Flash Player version
11.1.0 or greater is installed.


    <noscript>
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100px" height="100px" id="SampleApp">
            <param name="movie" value="SampleApp.swf" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#ffffff" />
            <param name="allowScriptAccess" value="sameDomain" />
            <param name="allowFullScreen" value="true" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="SampleApp.swf" width="100px" height="100px">
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="sameDomain" />
            <!--<![endif]-->
            <!--[if gte IE 6]>-->
                <p> 
                    Either scripts and active content are not permitted to run or Adobe Flash Player version
                    11.1.0 or greater is not installed.
                </p>
            <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflashplayer">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                </a>
            <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>
    </noscript>     

    <input type="button" value="test" onclick="TestFlex()"/>


<br> function TestFlex()<br> {<br> var obj=document.getElementById(&#39;SampleApp&#39;); <br> //obj.myFlexFunction(); <br> var vals=obj.getValue();<br> alert(vals);<br> }<br>

zen_qiwei
zen_qiwei 不用js调用flex方法的时候 flex自身的方法调用的时候是好的。
大约 4 年之前 回复
zen_qiwei
zen_qiwei 我是问你为啥调用flex开启摄像头的方法的时候 总是:"错误1:无法链接到活动摄像头!"
大约 4 年之前 回复
maxlizmouse
maxlizmouse   2013.12.09 14:21

你没获得那个swf的dom对象!另外ie和非ie获得swf dom的方法也不同! 可参考我的写法
function cpuRateRefresh()
{
getSWF("cpuRateDoughnut").cpuRateRefresh("55");
}

function getSWF(movieName)
{

if (navigator.appName.indexOf("Microsoft") != -1)
{

return window[movieName];

}
else
{

return document[movieName];
}
}

width="100%" height="100%"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
id="cpuRateDoughnut">



http://www.adobe.com/go/getflashplayer">

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!