WPF MediaElement with source URL never HasTimeSpan? - wpf

I try to play a video from a URL source. When MediaOpened is fired, the test in
private void OnMediaOpened(object sender, EventArgs e)
{
if (mediaView.NaturalDuration.HasTimeSpan)
{
// ...
}
// ...
}
always returns false and positioning within the media timespan is not possible.
Remark: If I use a file as source then HasTimeSpan is true and MediaElement.NaturalDuration.TimeSpan can be retrieved.
Is this always the case for URL based sources ?
Is there another way to get the duration of a media for URL based sources ?
I'm using .net 4.6.1.

The problem seems to be in the video format you use. I tested this code and it works fine.
XAML:
<MediaElement
x:Name="mediaView"
MediaOpened="OnMediaOpened"
Source="http://mirrors.standaloneinstaller.com/video-sample/Panasonic_HDC_TM_700_P_50i.mp4" />
Code:
private void OnMediaOpened(object sender, RoutedEventArgs e)
{
if (mediaView.NaturalDuration.HasTimeSpan)
{
MessageBox.Show("It has TimeSpan and its duration is : " + Convert.ToString(mediaView.NaturalDuration) );
}
}

Meanwhile I can not reproduce the issue described in my question. The video streamed from my Tomcat via a servlet (see my comment to HYA's answer) now has a time span. I wonder if this is due to a "minor" update in the .Net framework.

Related

silverlight exit call a Web service

I want to send data to the server when you close the application
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Exit(object sender, EventArgs e)
{
ClientReverse.UserExitGameAsync((Guid)Login);
}
Server:
public void UserExitGame(Guid UserGuid)
{
Games.Games.ExitUserGames(UserGuid);
}
but the server side is not satisfied.
It is already too late when you reach the ApplicationExit event.
I have seen Javascript that keeps on a webpage until confirmed (Stack Overflow does it a lot). You might want to modify a version of that Javascript that sends a message back to the Silverlight app before it allows page closing.
Calling Silverlight methods from JS is easy (you can simply expose SL methods to JS with the [Scriptable] attribute).

Silverlight Web Service

Below is the code i used to consume web servcie in SilverLight.
private void button1_Click(object sender, RoutedEventArgs e)
{
BasicHttpBinding bind = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress("http://loalhost/Service.asmx");
ServiceSoapClient client = new ServiceSoapClient(bind, endpoint);
client.RunHelloCompleted += new EventHandler<RunHelloCompletedEventArgs>(client_RunQwinCompleted);
client.RunHelloAsync(command);
}
void client_RunHelloCompleted(object sender, RunHelloCompletedEventArgs e)
{
txtProcess.Text=Process(e.Result);
}
I want to know a way that after i run RunHelloAsync(Command), I want to get the returned result without going to Completed event. Please advise me. thank you.
Simple answer : You can't. Everything in Silverlight is Asynchronous so there is no way to block after the client.RunHelloAsync(command) call and wait for the result.
Long answer : There are ways to simulate working with calls in a synchronous fashion, but the calls still being made asynchronously. Take a look at this thread for a few more answers.

Passing Parameters to Silverlight

Any ideas on how to pass parameters to Silverlight on startup from URL QueryString?
Thank You
One approach you can take is to expose a method that can be accessed from JavaScript. So in your xaml.cs file you need to add the following to your constructor:
this.Loaded += new RoutedEventHandler(Page_Loaded);
Then add the following event handler:
void Page_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.RegisterScriptableObject("YourControlName", this);
}
and:
[ScriptableMember]
public void YourMethod(string yourData)
{
// Do your stuff here
}
Then in the ascx or aspx page where your Silverlight control is instantiated add the following JavaScript:
var silverlightControl;
function onSilverlightLoad(sender, args) {
silverlightControl = sender.getHost();
var yourData = "some data";
silverlightControl.Content.YourControlName.YourMethod(yourData);
}
It does also mean that your Silverlight control has to be instantiated via the <object... tag rather than via <asp:Silverlight...
Although Chris's method will work, it's easier to pass startup information through Silverlight's initialization parameters feature.
If all you need to do is get at key-value pairs of the query string, there is a much simpler way using the HtmlPage class:
HtmlPage.Document.QueryString["your_key"];

Silverlight 3 Out of the Browser Updates

I have a few users that are using a silverlight app that aren't recieving updates when a new release is published. Isn't this suppose to be automatic or perhaps I'm missing an option somewhere? I was also starting to think that maybe the XAP file is cached and I some how need to prevent that.
Any thoughts out there?
You need to write a few lines of code.
If you're familiar with 'one click' deployment then some of the options you're used to don't exist in Silverlight. You need to write the code yourself.
http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
if (Application.Current.IsRunningOutOfBrowser)
{
Application.Current.CheckAndDownloadUpdateAsync();
}
and then in your App() constructor :
Application.Current.CheckAndDownloadUpdateCompleted +=
new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
and an event handler :
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
// http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
if (e.UpdateAvailable)
{
MessageBox.Show("The application has been updated! Please close and reopen it to load the new version.");
}
else if (e.Error != null && e.Error is PlatformNotSupportedException)
{
MessageBox.Show("An application update is available, " +
"but it requires a new version of Silverlight. " +
"Please contact tech support for further instructions.");
}
}
It only auto updates if the developer performs the CheckAndDownloadUpdateAsync() call. See updates: http://timheuer.com/blog/archive/2009/07/10/silverlight-3-released-what-is-new-and-changed.aspx#oob

Download a html file in Silverlight

I'm trying to use the WebClient class to download a html file from another website and present it as a text stream, But I'm getting a security error, what am I doing wrong, or is this another one of Silverlights security "Features"
[code]
namespace ImageScrape
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
WebClient cl = new WebClient();
cl.OpenReadCompleted += new OpenReadCompletedEventHandler(cl_OpenReadCompleted);
cl.OpenReadAsync(new Uri(#"http://www.google.co.uk/",UriKind.Absolute));
}
void cl_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
testTextBlock.Text = e.Result.ToString();
}
}
}
[/code]
EDIT
Thanks guys, I was really hoping I wouldn't have to create this as a WCF service as 1) I only know the basics and 2) The idea is that you can use this .xap without having to connect to a central server, mainly because for this I don't have a server that I could host a WCF service on.
Does anyone know a way to get around this, or anywhere that would host a WCF service for free?
I think there are security issues with going directly to another site from the silverlight client.
The best work around for this would be to move this code into a web service and then serve the content you require to client from there.

Resources