Silverlight Controls and Silverlight Toolkithttp://forums.silverlight.net//35.aspx/1?Silverlight+Controls+and+Silverlight+ToolkitDiscussions around using and developing Silverlight controls and the Silverlight ToolkitMon, 01 Jan 0001 00:00:00 -05003538482http://forums.silverlight.net//p/11987/38482.aspx/1?Creating+a+MediaElement+in+C+Creating a MediaElement in C# <p>I'm trying to create a MediaElement completely programmatically (e.g. no Xaml). I can get things like buttons to appear, but not the MediaElement. I figure its something trivial that I'm doing wrong, but here's essentially what I'm doing:</p> <p>private MediaElement mediaElement;<br> public Page() </p> <p>{<br> &nbsp;&nbsp;&nbsp; // Required to initialize variables<br> &nbsp;&nbsp;&nbsp; InitializeComponent();<br> <br> &nbsp;&nbsp;&nbsp; mediaElement = new MediaElement();<br> &nbsp;&nbsp;&nbsp; mediaElement.Width = 400;<br> &nbsp;&nbsp;&nbsp; mediaElement.Height = 300;<br> &nbsp;&nbsp;&nbsp; mediaElement.Source = new Uri(&quot;test.wmv&quot;, UriKind.Relative);<br> &nbsp;&nbsp;&nbsp; mediaElement.AutoPlay = true;<br> <br> &nbsp;&nbsp;&nbsp; LayoutRoot.Children.Add(mediaElement);</p> <p>}<br> </p> <p>Of course in another method I've tried a .Play() command and it still doesn't work. If I were to replace the media element initialization with that of a button and then add the button, it works as expected. Seems like it should be incredibly straight forward, but all the documents I've discovered only talk about doing this via XAML. Any ideas?<br> </p> 2008-03-18T15:17:02-04:0038531http://forums.silverlight.net//p/11987/38531.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>This is an issue with the relative paths, not your creation.&nbsp; Try changing the Source to <a href="http://yourwebsiteaddress/test.wmv">http://yourwebsiteaddress/test.wmv</a> and it should work.</p> 2008-03-18T16:31:23-04:0038582http://forums.silverlight.net//p/11987/38582.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# InitializeComponent();<br> <br> mediaElement = new MediaElement();<br> mediaElement.Width = 400;<br> mediaElement.Height = 300;<br> mediaElement.Source = new Uri(&quot;http://sandbox.russellmyers.com/test.wmv&quot;);<br> mediaElement.AutoPlay = true;<br> <br> LayoutRoot.Children.Add(mediaElement); <p>Did that and no dice. Tried a kind of absolute as well. Is this something I have to upload to test or should it work locally?<br> &nbsp;</p> 2008-03-18T18:18:25-04:0039113http://forums.silverlight.net//p/11987/39113.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>Hello, your code should work. Please make sure:</p> <p>First, you must run your application from http protocol. Create a new web site, and add a Silverlight Link. Run the web site, and you should be OK.</p> <p>Second, if you choose to use relative path, you have to put the videos under ClientBin folder on your web site.</p> 2008-03-20T04:31:16-04:0039275http://forums.silverlight.net//p/11987/39275.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>Hello,</p> <p>Yes, the code should work, but I cannot make it working either.<br> I've tried this bunch of code :</p> <font color="#2b91af" size="2"> <p>Canvas</font><font size="2"> testCan = </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">Canvas</font><font size="2">();<br> testCan.Width = 400;<br> testCan.Height = 400;<br> testCan.Background = </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">SolidColorBrush</font><font size="2"> { Color = </font><font color="#2b91af" size="2">Color</font><font size="2">.FromArgb(255, 255, 255, 0) };<br> </font><font color="#2b91af" size="2">MediaElement</font><font size="2"> test = </font> <font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">MediaElement</font><font size="2">();<br> test.Source = </font><font color="#0000ff" size="2">new</font><font size="2"> </font> <font color="#2b91af" size="2">Uri</font><font size="2">(</font><font color="#a31515" size="2">&quot;http://localhost:1137/videos/test.wmv&quot;</font><font size="2">, </font><font color="#2b91af" size="2">UriKind</font><font size="2">.Absolute);<br> test.Width = 400;<br> test.Height = 400;<br> testCan.Children.Add(test);<br> LayoutRoot.Children.Add(testCan);</font></p> <p><font size="2">And it doesn't work. I've tried a lot of thing, like Relative Uri (w/ and&nbsp;w/o&nbsp;ClientBin) but no luck.<br> I've tried with &quot;test.SetValue(MediaElement.SourceProperty,...&quot;), no luck either.<br> The MediaElement CurrentState is stuck on &quot;Closed&quot;.</font></p> <p>I really don't know what's wrong, because all the code seem right, and&nbsp;in Expression Blind, it does work (and it just takes 1 line...).<br> I've checked the uri <font color="#a31515"><a href="http://localhost:1137/videos/test.wmv">http://localhost:1137/videos/test.wmv</a>&nbsp;</font><font color="#000000"> directly through the browser, and it works. It opens Windows Media Player and plays the video file.</font></p> <p><font color="#000000">So, what's wrong ?? I really don't get it. [:@]</font></p> <p><font color="#000000">Thanks for your help.<font size="2"></p> </font></font> 2008-03-20T14:55:28-04:0039473http://forums.silverlight.net//p/11987/39473.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>Well, are you sure your video is Silverlight compatible? What if you simply write this in XAML? If it doesn't work, consider encoding the video with Expression Encoder.</p> <font color="#0000ff" size="2"> <p>&lt;</font><font color="#a31515" size="2">MediaElement</font><font color="#ff0000" size="2"> Source</font><font color="#0000ff" size="2">=&quot;http://localhost:1137/videos/test.wmv&quot;/&gt;</p> </font> <p>Also, when you run your application, what's the text in the browser's address bar? It should begin with <a href="http://localhost/">http://localhost</a>... This means it's running from the http protocol. If it begins with something like C:\, that's wrong. In this case, you must create a web site, add a Silverlight Link, and run the web site.</p> 2008-03-21T03:27:56-04:0040525http://forums.silverlight.net//p/11987/40525.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>As you guys have all said, there wasn't a problem with the code. The issue was actually with Visual Studio. Apparently I can compile in Visual Studio and it &quot;breaks&quot; aka it doesn't show the media element but it does show the canvas and buttons. However, when I compile using Blend 2.5 it works as you would expect.</p> <p>&nbsp;In short, I have to compile using Blend for some reason. I DO have the latest SDK's and tool kits for developing in Visual Studio in case you're wondering.<br> &nbsp;</p> 2008-03-24T16:59:20-04:0040746http://forums.silverlight.net//p/11987/40746.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>Yi-Lun Luo&gt; The video is Silverlight compatible. I've tried when Expression Blend, and it does work. However, I can't simply write it in XAML, because the video URI is dynamically loaded from a database.</p> <p>When&nbsp;I run the application, the text in the browser's address bar is <a href="http://localhost:1137/videos/test.wmv"> Http://localhost:1137/videos/test.wmv</a></p> <p>I've Silverlight 2 and the lastest SDK's and toolkit, like RusselMyers.</p> <p>So, if you find a tip for that issue, that would be nice :)</p> 2008-03-25T07:58:38-04:0041118http://forums.silverlight.net//p/11987/41118.aspx/1?Re+Re+Creating+a+MediaElement+in+C+Re: Re: Creating a MediaElement in C# <p>Up [&lt;:o)]</p> 2008-03-26T07:39:38-04:0041127http://forums.silverlight.net//p/11987/41127.aspx/1?Re+Re+Re+Creating+a+MediaElement+in+C+Re: Re: Re: Creating a MediaElement in C# <p>Okay, this is working now. Don't tell me how, I've really no idea.</p> <p>Thursday I tried with an absolute URI (<a href="http://localhost:1137/videos/test.wmv">http://localhost:1137/videos/test.wmv</a>), and the video was in the WebService directory/videos/test.wmv.<br> No luck, it didn't work.<br> Then I tried with relative URI and the video was put in the ClientBin directory.<br> It didn't work either.</p> <p>Today, I wanted to find a workaround for this issue. So, I tried again the absolute URI, but still no luck. I commented the line (same line as Thursday, nothing changed)<br> I tried the relative URI (uncommented the same line as Thursday either, nothing changed), copied the video file in the ClientBin directory, and then it worked ?!</p> <p>I don't get it, it worked. I tried with a subfolder &quot;videos&quot; inside ClientBin, and it worked too.<br> So, i tried to go outside of &quot;ClientBin&quot; folder with relative URI (by using ../ ). It didn't work.<br> I didn't want having to upload video files in ClientBin directory, so I uncommented the line with Absolute URI, and tried again.<br> And guess what ? It worked !</p> <p>Seriously I really don't get it, nothing changed.. I tried again, and now it works fine.</p> <p>[:|]</p> <p>I have no idea what made it working...</p> 2008-03-26T08:19:43-04:00207081http://forums.silverlight.net//p/11987/207081.aspx/1?Re+Creating+a+MediaElement+in+C+Re: Creating a MediaElement in C# <p>It took me a half day to figure this out.&nbsp; It falls under that category of seeing what I wanted rather than seeing what was actually there.</p> <p>My eyes refused to &quot;see&quot; the following statement: &nbsp;</p> <blockquote><span class="icon-blockquote"></span> <h4>Yi-Lun Luo - MSFT</h4> In this case, you must create a web site</blockquote> <p></p> <p>The solution to my problem is this:&nbsp; When you create a Silverlight application you are presented with two choices, 1)&nbsp;Create a web site to run it in or 2) Create an html test page to run it in.</p> <p>I was running in an html test page and so it wouldn't work.&nbsp; When I finally threw that code away and made a web site to test my code in then it all started working (using either <a href="http://localhost:nnnn/whatever/test.wmv">http://localhost:nnnn/whatever/test.wmv</a> or <a href="http://mywebsite.com/whatever/test.wmv">http://mywebsite.com/whatever/test.wmv</a>)</p> 2009-04-15T21:32:17-04:00