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:
private MediaElement mediaElement;
public Page()
{
// Required to initialize variables
InitializeComponent();
mediaElement = new MediaElement();
mediaElement.Width = 400;
mediaElement.Height = 300;
mediaElement.Source = new Uri("test.wmv", UriKind.Relative);
mediaElement.AutoPlay = true;
LayoutRoot.Children.Add(mediaElement);
}
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?
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.
Second, if you choose to use relative path, you have to put the videos under ClientBin folder on your web site.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
Yes, the code should work, but I cannot make it working either.
I've tried this bunch of code :
Canvas
testCan = new
Canvas();
testCan.Width = 400;
testCan.Height = 400;
testCan.Background = new
SolidColorBrush { Color =
Color.FromArgb(255, 255, 255, 0) };
MediaElement test =
new MediaElement();
test.Source = new
Uri("http://localhost:1137/videos/test.wmv",
UriKind.Absolute);
test.Width = 400;
test.Height = 400;
testCan.Children.Add(test);
LayoutRoot.Children.Add(testCan);
And it doesn't work. I've tried a lot of thing, like Relative Uri (w/ and w/o ClientBin) but no luck.
I've tried with "test.SetValue(MediaElement.SourceProperty,..."), no luck either.
The MediaElement CurrentState is stuck on "Closed".
I really don't know what's wrong, because all the code seem right, and in Expression Blind, it does work (and it just takes 1 line...).
I've checked the uri http://localhost:1137/videos/test.wmv directly through the browser, and it works. It opens Windows Media Player and plays the video
file.
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.
Also, when you run your application, what's the text in the browser's address bar? It should begin with
http://localhost... 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.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
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 "breaks" 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.
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.
Yi-Lun Luo> 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.
Okay, this is working now. Don't tell me how, I've really no idea.
Thursday I tried with an absolute URI (http://localhost:1137/videos/test.wmv), and the video was in the WebService directory/videos/test.wmv.
No luck, it didn't work.
Then I tried with relative URI and the video was put in the ClientBin directory.
It didn't work either.
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)
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 ?!
I don't get it, it worked. I tried with a subfolder "videos" inside ClientBin, and it worked too.
So, i tried to go outside of "ClientBin" folder with relative URI (by using ../ ). It didn't work.
I didn't want having to upload video files in ClientBin directory, so I uncommented the line with Absolute URI, and tried again.
And guess what ? It worked !
Seriously I really don't get it, nothing changed.. I tried again, and now it works fine.
RussellMyers
Member
18 Points
10 Posts
Creating a MediaElement in C#
Mar 18, 2008 03:17 PM | LINK
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:
private MediaElement mediaElement;
public Page()
{
// Required to initialize variables
InitializeComponent();
mediaElement = new MediaElement();
mediaElement.Width = 400;
mediaElement.Height = 300;
mediaElement.Source = new Uri("test.wmv", UriKind.Relative);
mediaElement.AutoPlay = true;
LayoutRoot.Children.Add(mediaElement);
}
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?
MediaElement C#
swildermuth
Star
8438 Points
1547 Posts
Re: Creating a MediaElement in C#
Mar 18, 2008 04:31 PM | LINK
This is an issue with the relative paths, not your creation. Try changing the Source to http://yourwebsiteaddress/test.wmv and it should work.
MVP, Speaker and Author
Web Workshop (HTML5/CSS/MVC4)
San Fran, CA - Mar 28-30, 2012
Dallas, TX: Apr 29-May 1, 2012
https://agilitrain.com/Workshop/Info/Web_Workshop
RussellMyers
Member
18 Points
10 Posts
Re: Creating a MediaElement in C#
Mar 18, 2008 06:18 PM | LINK
mediaElement = new MediaElement();
mediaElement.Width = 400;
mediaElement.Height = 300;
mediaElement.Source = new Uri("http://sandbox.russellmyers.com/test.wmv");
mediaElement.AutoPlay = true;
LayoutRoot.Children.Add(mediaElement);
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?
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Creating a MediaElement in C#
Mar 20, 2008 04:31 AM | LINK
Hello, your code should work. Please make sure:
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.
Second, if you choose to use relative path, you have to put the videos under ClientBin folder on your web site.
nucl3ar
Member
22 Points
14 Posts
Re: Creating a MediaElement in C#
Mar 20, 2008 02:55 PM | LINK
Hello,
Yes, the code should work, but I cannot make it working either.
I've tried this bunch of code :
Canvas
testCan = new Canvas();testCan.Width = 400;
testCan.Height = 400;
testCan.Background = new SolidColorBrush { Color = Color.FromArgb(255, 255, 255, 0) };
MediaElement test = new MediaElement();
test.Source = new Uri("http://localhost:1137/videos/test.wmv", UriKind.Absolute);
test.Width = 400;
test.Height = 400;
testCan.Children.Add(test);
LayoutRoot.Children.Add(testCan);
And it doesn't work. I've tried a lot of thing, like Relative Uri (w/ and w/o ClientBin) but no luck.
I've tried with "test.SetValue(MediaElement.SourceProperty,..."), no luck either.
The MediaElement CurrentState is stuck on "Closed".
I really don't know what's wrong, because all the code seem right, and in Expression Blind, it does work (and it just takes 1 line...).
I've checked the uri http://localhost:1137/videos/test.wmv directly through the browser, and it works. It opens Windows Media Player and plays the video file.
So, what's wrong ?? I really don't get it. [:@]
Thanks for your help.
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Creating a MediaElement in C#
Mar 21, 2008 03:27 AM | LINK
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.
<
MediaElement Source="http://localhost:1137/videos/test.wmv"/>Also, when you run your application, what's the text in the browser's address bar? It should begin with http://localhost... 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.
RussellMyers
Member
18 Points
10 Posts
Re: Creating a MediaElement in C#
Mar 24, 2008 04:59 PM | LINK
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 "breaks" 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.
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.
nucl3ar
Member
22 Points
14 Posts
Re: Creating a MediaElement in C#
Mar 25, 2008 07:58 AM | LINK
Yi-Lun Luo> 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.
When I run the application, the text in the browser's address bar is Http://localhost:1137/videos/test.wmv
I've Silverlight 2 and the lastest SDK's and toolkit, like RusselMyers.
So, if you find a tip for that issue, that would be nice :)
nucl3ar
Member
22 Points
14 Posts
Re: Re: Creating a MediaElement in C#
Mar 26, 2008 07:39 AM | LINK
Up [<:o)]
nucl3ar
Member
22 Points
14 Posts
Re: Re: Re: Creating a MediaElement in C#
Mar 26, 2008 08:19 AM | LINK
Okay, this is working now. Don't tell me how, I've really no idea.
Thursday I tried with an absolute URI (http://localhost:1137/videos/test.wmv), and the video was in the WebService directory/videos/test.wmv.
No luck, it didn't work.
Then I tried with relative URI and the video was put in the ClientBin directory.
It didn't work either.
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)
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 ?!
I don't get it, it worked. I tried with a subfolder "videos" inside ClientBin, and it worked too.
So, i tried to go outside of "ClientBin" folder with relative URI (by using ../ ). It didn't work.
I didn't want having to upload video files in ClientBin directory, so I uncommented the line with Absolute URI, and tried again.
And guess what ? It worked !
Seriously I really don't get it, nothing changed.. I tried again, and now it works fine.
[:|]
I have no idea what made it working...