Dave_in_Raleigh
Dave_in_Raleigh

Reputation: 53

Play the silverlight player from outside the silverlight object

I have a simple Silverlight player that uses the MediaElement. For reasons out of my control, they want to be able to have all the play/pause/stop, volume controls etc. based in the ASP.NET code and not be built in Silverlight. I embed the Silverlight in my aspx as follows:

<object id="SilverlightPlayer" data="data:application/x-silverlight," 
                                    type="application/x-silverlight-2" width="750" height="460" >
<param name="source" value="ClientBin/VideoPlayer.xap"/>  
<param name="EnableGPUAcceleration" value="true" />
<param name="OnResize" value="HandleResize" />
<param name="autoUpgrade" value="true" />
<param name="initParams" id="SLInitParameters" value='video=MyVideo.wmv' />                    
</object>

I want to have the user click a "Play" button in the ASPX and it will tell the Silverlight player to play the video. (Same kind of thing for all the other buttons) I have been unable to find out how to do this anywhere since everyone it seems builds these controls into their Silverlight.

Any help is greatly appreciated.

UPDATE:
I am using the following to recreate the silverlight:

function CreateSilverlight(hostElement, source, initParams) {
        var pluginId = hostElement.id + "PluginId";

        hostElement.innerHTML = Silverlight.createObject(source, null, pluginId,
            {
                width: '750',
                height: '460',
                background: 'black',
                isWindowless: true,
                alt: '<!--Silverlight not installed-->',
                data: 'data:application/x-silverlight,',
                type: 'application/x-silverlight-2',
                EnableGPUAcceleration: true,
                version: '4.0',
                autoUpgrade: true
            },
            { onError: null, onLoad: null, OnResize: HandleResize },
            initParams, hostElement.id);
}

So I don't have the reference to the object.

Upvotes: 1

Views: 786

Answers (3)

Dave_in_Raleigh
Dave_in_Raleigh

Reputation: 53

I didn't need to reload the silverlight control to get it to load a different video. I just needed to pass a new media path to the silverlight mediaElement.Source via the JS to silverlight bridge. I also solved my issue of loading a video passed to the page by adding to invoke a js function once the SL was loaded on the screen.

Upvotes: 0

Matt Bridges
Matt Bridges

Reputation: 49395

In the constructor of your silverlight page on which the media element appears, call:

HtmlPage.RegisterScriptableObject("player", this)

Then, you can add methods to your page like this:

[ScriptableMember]
public void Play()
{
    this.MediaElement.Play();
}

[ScriptableMember]
public void Pause()
{
    this.MediaElement.Pause();
}

[ScriptableMember]
public void Stop()
{
    this.MediaElement.Stop();
}

Those [ScriptableMember] attributes are important. Then, from javascript, you can do:

var slApp = document.getElementById("SilverlightPlayer");
slApp.player.Play();

That would call the exposed "Play" method, which in turn tells the MediaElement to Play().

Upvotes: 1

AlignedDev
AlignedDev

Reputation: 8232

You can communicate from HTML to Silverlight via Javascript through the HtmlPage.Window.Invoke() method. Check out this link.

Upvotes: 1

Related Questions