2011-04-18 74 views
3

在以下代碼中只有按鈕圖像已嵌入到flex代碼中。但在html對象或embed標籤中,爲什麼必須指定高度和寬度。儘管如此,在一個正常的按鈕,如果我們沒有指定的高度和寬度似乎有一些錯誤html中的SWF文件的高度和寬度

html

<div align="center"> 
    <br /> 
    <div style="display:block;width:100px;height:100px;" id="audio" 
     class="testsound" align="center"/> 
    <p class="clickable"> 
     <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
     width="400" height="100" id="myMovieName"> 
      <param name="movie" value="/media/players/testsound.swf"/> 
      <param name="soundurl" value="/media/players/music.mp3"/> 
      <param name="quality" value="high"/> 
      <param name="bgcolor" value="#FFFFFF"/> 
      <embed width="100" height="100" href="/media/players/testsound.swf" 
       src="/media/players/testsound.swf" 
       flashvars="soundUrl=/media/players/music.mp3" 
       quality="high" bgcolor="#FFFFFF" 
       name="myMovieName" loop="false" align="" 
       type="application/x-shockwave-flash"> 
      </embed> 
     </object> 
    </p> 
</div> 

MXML

<?xml version="1.0"?> 
<!-- embed/EmbedSound.mxml --> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 

    <mx:Script> 
     <![CDATA[ 

      import flash.media.*; 

      public var snd:Sound = new sndCls() as Sound; 
      public var sndChannel:SoundChannel; 

      public function playSound():void { 
       sndChannel=snd.play(); 
      } 

     ]]> 
    </mx:Script> 
    <mx:Image id="loader1" click="playSound();" 
     source="@Embed(source='/opt/cloodon/site/media/img/speaker.gif')" /> 
</mx:Application> 

回答

1

您應該在主要的mxml中指定Application的寬度和高度,即使它包含一個可點擊的圖像!
如果您想您的網頁,我們的width: 400pxheight: 100px柔性圖像(按鈕?),那麼你的Application標籤必須是這樣的:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="100"> 

而儘管如此,在你的HTML你應該有你的兩objectembed標籤具有相同的widthheight,您應該爲您的embed標籤的屬性指定一個值,例如:align="middle"

object標籤應該類似於

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
    width="400" height="100" id="myMovieName" 
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> 
    <param name="movie" value="/media/players/testsound.swf"/> 
    <param name="soundurl" value="/media/players/music.mp3"/> 
    <param name="quality" value="high"/> 
    <param name="bgcolor" value="#FFFFFF"/> 
    <!-- the embed tag does not have href attribute --> 
    <embed width="400" height="100" 
     src="/media/players/testsound.swf" 
     flashvars="soundUrl=/media/players/music.mp3" 
     quality="high" bgcolor="#FFFFFF" 
     name="myMovieName" loop="false" align="middle" 
     type="application/x-shockwave-flash" 
     pluginspage="http://www.adobe.com/go/getflashplayer"> 
    </embed> 
</object> 

其他 哪裏是你的sndClass聲明?如果您有內嵌MP3,你的腳本標籤應包含:

<mx:Script> 
    <![CDATA[ 
     import mx.core.SoundAsset; 

     [Embed('/media/players/music.mp3')] 
     private var _sound:Class; 
     public var snd:SoundAsset = new _sound() as SoundAsset; 

     public var sndChannel:SoundChannel; 

     public function playSound():void { 
      sndChannel=snd.play(); 
     } 

    ]]> 
</mx:Script> 

如果您想指定從HTML你的MP3的路徑,你可能看this post for reference

更新2

設置HTML object的寬度和高度根據你的Flex按鈕的大小,你應該作如下更新您的應用程序MXML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    width="1" height="1" backgroundColor="#bee3f6" 
    horizontalScrollPolicy="off" verticalScrollPolicy="off" 
    creationComplete="onCreationComplete()" viewSourceURL="srcview/index.html"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 

      //This method will change the button's label ==> width. 
      public function sayWhat():void { 
       var _date:Date = new Date(); 
       extButton.label = "It's " + (_date.getHours() + 1) + ":" + _date.getMinutes() + 
        ((_date.getMilliseconds() % 2) == 0 ? ". nice." : ". very tired."); 
      } 

      public function onCreationComplete():void { 
       // binds this.sayWhat to the external interface, so when from 
       // javascript is called the compiled swf object's sayWhat function, 
       // it will be transferred to this.sayWhat. 
       ExternalInterface.addCallback("sayWhat", sayWhat); 

       // set the flash application's size to be exact the button's size. 
       this.width = extButton.width; 
       this.height = extButton.height; 

       try 
       { 
        // The js function: onFlashAppInited(width, height); 
        // The next line tells the external interface (the parent application: 
        // browser window), that this application has finished loading, its 
        // ready to be used. 
        // We're passing the button's width and height as parameters 
        // In js there has to be a global method with this name. 
        ExternalInterface.call("onFlashAppInited", extButton.width, extButton.height); 
       } 
       catch (e:Error) 
       { 
        trace(e.message + "\n" + e.getStackTrace(), e.name); 
       } 
      }   

      protected function onButtonUpdateComplete(event:FlexEvent):void 
      { 
       // if the button's size has changed, we notify the javascript to set the 
       // correct size to the object tag too. 
       if ((this.width != extButton.width) || (this.height != extButton.height)) 
       { 
        // set the application size 
        this.width = extButton.width; 
        this.height = extButton.height; 
        // notify the js about the change 
        ExternalInterface.call("onFlashAppSizeChanged", this.width, this.height); 
       } 
      } 

     ]]>    
    </mx:Script> 
    <mx:Button id="extButton" label="Do something!" updateComplete="onButtonUpdateComplete(event)" /> 
</mx:Application> 

注意,最初這個應用程序只有1x1像素(如果更少,它不會初始化!)。
onCreationComplete處理程序中,其大小更改爲匹配按鈕的大小。

這些值(寬度和高度)傳遞給javascript函數onFlashAppInited,您也應該更新:

function onFlashAppInited(width, height) { 
    alert("Flash app inited!"); 
    appInited = true; 

    onFlashAppSizeChanged(width,height); 

    // remove the temporary swf container: tmp 
    document.body.removeChild(document.getElementById("tmp")); 
} 

function onFlashAppSizeChanged(width, height) { 
    flashApp.width = width + "px"; 
    flashApp.height = height + "px"; 
} 
從您的文章
+0

我同意你有關在mxml和html中設置高度和寬度。但默認情況下,我們不知道Flex中按鈕的大小,所以如果指定了默認按鈕,如何知道它的高度和寬度以及如何在html中設置..? – Naveen 2011-04-19 17:04:04

+0

: - ?你可以嘗試從flex的onCreationComplete方法中檢索按鈕的大小(寬度/高度),並將其傳遞給JavaScript(沿着flashAppInited方法作爲參數)。通過這種方式,JavaScript端會收到關於按鈕寬度的通知,並且可以調整swf對象的大小以獲得完全相同的大小。 – rekaszeru 2011-04-19 17:06:46

+0

K..Can你讓我知道如何檢索它的任何鏈接..順便說一下,我看到你的網站。雖然它的功能它已經很酷.. – Naveen 2011-04-19 17:21:29

7

高度和嵌入標籤中的寬度是必需的屬性。在標籤定義上有swfobjectAdobe以及required and optional attributes

你問的是爲什麼他們是必需的,或者爲什麼你的頁面會因爲沒有他們而中斷(提示,第一個答案是第二個問題)?

編輯:

您應該修改嵌入和OBJECT標籤的寬度和高度屬性正好到SWF的大小。

如果將scaleMode設置爲「noScale」的情況,你不會看到在SWF任何縮放您可以設置應用程序的寬度和高度在的.mxml

<mx:Application width="300" height="200"> 

stage.scaleMode = StageScaleMode.NO_SCALE; 
stage.align = StageAlign.TOP_LEFT; 

如果你在使用Actionscript 3(你應該是),你可以在你的主類中設置元數據。

[SWF(width="640", height="480")] 
public class MyClass extends Sprite 
{ 
    public function MyClass() 
    { 
     stage.scaleMode = StageScaleMode.NO_SCALE; 
     stage.align = StageAlign.TOP_LEFT; 
    } 
} 
+0

但現在它並不確切地表明應該做什麼將SWF適合其確切大小的HTML文件 – Naveen 2011-04-19 05:33:25

+0

好吧,在我上面的編輯多一點的建議。希望這可以幫助。 – 2011-04-19 16:53:35

+0

+1爲解釋..謝謝 – Naveen 2011-04-23 12:14:00

1

加成@Dominic的Tancredi的評論: 兩個OBJECT和EMBED標籤需要WIDTH和HEIGHT屬性。