views:

1407

answers:

1

I converted a local sound file to a resource, which built this in my XAML:

<UserControl.Resources>
    <my:Uri x:Key="SoundFiles">file:///c:/Audio/HebrewDemo/Shalom.wav</my:Uri>
</UserControl.Resources>

I did this by pasting a local disk mp3 filename into source, then clicked on the "dot" by source and chose "Extract Value to Resource".

When I run, it tells me that "Uri" is not valid, and sure enough, in the Intellisense, I see other elements that start with "uri" but not just URI by itself.

In the real world I want to specify a dynamic mp3 file name. For example, I might have a database of foreign language words used for flashcards, I want to play a sound file on a URL. But I thought I would try to walk before running...

Now I'm trying this:

    mediaElement1.Source = new Uri(
                     "http://HebrewResources.com/SoundFiles/Shalom.mp3", 
                      UriKind.Absolute); 
    mediaElement1.Play(); 

The status bar in the FireFox browser indicates some data being transferred from the website. However, I never hear any sound. Could it just be an encoding issue? If it is not encoded properly, would I get an error?

Also, can I put the Uri statement in the load or make it run in the background, so the user can read the screen, at the same time the sound file is downloading? In other words, when he clicks the button to hear the soundfile, ideally it would already be preloaded for him. In this language-learning app, the user will see a word in a foreign language, and try to pronounce it himself, then he will click the "Play" button to hear the sound to check his results.

+1  A: 

The first code which points to the local file won't work in a normal Silverlight application because of Silverlights "sanboxed" security model. Running a normal Silverlight application in your browser, you can't access local resources like you can if you were running an installed winforms/WPF application. Have a look at this video tutorial http://www.silverlight.net/learn/videos/all/local-file-access/, if you want to learn more about accessing local files using Silverlight.

In regards to your second piece of code, it should work, so quite possibly it's an encoding issue, and no unfortunately you often don't get any errors for things like that.

I created a sample app and pointed it at your mp3 file and it' wouldn't work, however a quick search for sample mp3s, lead me to another freely available (first hit). Using the code/url below, it works ok.

myMediaelement.Source = new Uri("http://www.robtowns.com/music/blind_willie.mp3", UriKind.Absolute);

In regards to the loading, it will start downloading the file when it hits the code that sets the source of the mediaelement. So if you set the source for the media element in your constructor or in your loaded event, it will automatically start downloading in the background. Then you just call myMediaelement.Play(); on the button click event.

If you find using Firefox a bit limited, when it comes to determine what and when something like external files or webservices are being loaded. There's a great free tool called Fiddler (http://www.fiddlertool.com/fiddler/version.asp) which will let you monitor those things very easily.

Good luck :)

Ola Karlsson
Thanks, I ended up starting a thread here and had a lot of discussion. http://forums.silverlight.net/forums/p/174934/397840.aspx. I added the MediaFailed event and used Expression Encoder to encode the file (didn't even know there was an Expression Encoder). Sadly, I dread encoding about 3000 mp3 files.
NealWalters
Good to hear that you got it sorted, in regards to encoding your files, using the encoder SDK and Powershell, you can fairly easily automate that process :) Have a look at this http://www.clarkezone.net/default.aspx?id=adc33486-4a60-4f3f-9ee0-bb8d81c5c6bf and maybe do some searching on "Expression encoder automation" or something like that
Ola Karlsson