Saturday, December 8, 2012

Silverlight only plays sound when called from Silverlight button (NOT Javascript)

Silverlight only plays sound when called from Silverlight button (NOT Javascript)

I'm running Silverlight 5 and I've got a method that plays a sound. I found a method on codeproject that allows me to call Siilverlight from JS (http://www.codeproject.com/Articles/293515/Calling-Silverlight-Method-from-Javascript-and-Jav)

    [ScriptableMember]     public void PlayDing() {         MediaElement me = new MediaElement();         me.Source = new Uri("/Sounds/ding.wma", UriKind.Relative) ;        LayoutRoot.Children.Add(me);         me.Play();     } 

If I call it from a buttom click in Silverlight, it works. If I call it from Javascript, it doesn't.

I've debugged through it and both calls enter the PlayDing() method, but only the Silverlight button click lets me hear the ding...

Why doesn't the JavaScript call work?

Here is my javascript code:

    <div>     <div style="width: 250px; background: lightblue; font-weight: bold;height:30px">         HTML Part     </div>     <div>         <input type="button" value="Calling Silverlight Method From Javascript"      onclick="CallSilverlight();" /></div> </div> <form id="form1" runat="server" style="height:100%"> <div id="silverlightControlHost">     <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">       <param name="source" value="ClientBin/SoundEngine.xap"/>       <param name="onError" value="onSilverlightError" />       <param name="background" value="white" />       <param name="minRuntimeVersion" value="5.0.61118.0" />       <param name="autoUpgrade" value="true" />            <param name="onLoad" value="pluginLoaded" />       <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">           <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>       </a>     </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div> </form>  <script type="text/javascript">      //calling Silverlight method      var slCtl = null;      function pluginLoaded(sender, args) {          slCtl = sender.getHost();      }      function CallSilverlight() {           slCtl.Content.SL2JS.PlayDing();      } </script>  

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog