Silverlight mediaelement drops part of a sound file - silverlight

I am using Silverlight mediaelement to play narrative sound files. Sometimes it drops part of the sound
like:
"Hallo this is something....." would sound like "allo this is something....." sometimes it happens at the begining, the middle or the end of the file !!!
Dose anyone have a clue ?
This is how I feed it the sound file:
soundStream = new IsolatedStorageFileStream(mediaUri, FileMode.Open, FileAccess.Read, FilesAndFolders.isoStore);
mediaSound.SetSource(soundStream);
And this is the event the mediaelement uses to start playing,:
void mediaSound_MediaOpened(object sender, RoutedEventArgs e)
{
mediaSound.Play();
}

Related

Can't play .mp3 audio file as Background music

I want to play my audio file as background music when my window opens in my Wpf project.I use MediaElement control. Like
<MediaElement MediaOpened="myMediaElement_MediaOpened" MediaFailed="myMediaElement_MediaFailed"
Volume="10" x:Name="myMediaElement" IsMuted="False"
LoadedBehavior="Manual" UnloadedBehavior="Manual"
Source="Resources/Media/Audio/StartPageMusic.mp3"/>
I can not get any sound when window opens. And can not catch any Exception. What is the problem in it? Or have I done anything wrong?
I handled the event and try to play file.
private void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
MediaElement _element = sender as MediaElement;
_element.Play();
}
My file is in folder Resources/Media/Audio. This folder is in my project. And I have set my audio file as CONTENT for BuildAction and CopyAlways for CopyToOutputDirectory in the Properties window.
As i can see the problem is that MediaOpened is not raised.
The reason is this :
' MediaElement.MediaOpened Event Occurs when media loading has finished.'
MSDN : MediaElement.MediaOpened Event
If you want to play the mp3 file as background music when the main window opens then you can add WindowLoaded event and there you can start the playing.
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
myMediaElement.Play();
}

How to stop video replay in silverlight?

Ive used silverlight player in our internal builds and one issue we are facing is that the video is automatically replaying as soon as its over.
Does anyone know a fix?
You can try to hook up the MediaEnded event and stop the video when it finishes.
private void player_MediaEnded(object sender, RoutedEventArgs e)
{
player.Stop();
}

mediaelement.source cannot play video

on my window_loaded, i put this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
mediaElement1.Source = new Uri("/WpfTest;component/Videos/acar_ikan_masin.mp4", UriKind.Relative);
}
and the file acar_ikan_masin.mp4 is already in my project (I add it using Add>Existing Item> to a new folder Videos which I created). but it seems vs can't find the video. the mediaelement is working fine because i could just drag and drop any video file to the mediaelement and it will play. what is the correct way to set the source, for a file which has already been added to the project?
it seems i just need to change the path to
mediaElement1.Source = new Uri(#"Videos/acar_ikan_masin.mp4", UriKind.Relative);
and set the file's copy to output directory to 'copy always'
MediaElement uses window media player in background so to use this.
make sure you have the latest WMP in you system.
and that WMP also support the mp4 type codecs. if not than install the wmp codec pack.
try with wmv it will work.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Media Files (*.wmv)|*.wmv";
ofd.ShowDialog();
mediaplayer1.source = new Uri(ofd.FileName);

What is the reason for adding MediaElement to VisualTree?

I'm trying to find out why the last commented command is necessary in order to play a sound:
private void Window_ContentRendered(object sender, EventArgs e)
{
MediaElement MediaElement1 = new MediaElement();
MediaElement1.Source = new Uri(#"My-absolute-path");
//myGrid.Children.Add(MediaElement1);
}
Does anybody know?
Thanks!
If you don't want to add it to the visual tree, you probably want to use MediaPlayer instead of MediaElement.
private void Window_ContentRendered(object sender, EventArgs e)
{
MediaPlayer MediaPlayer1 = new MediaPlayer();
MediaPlayer1.Open(new Uri(#"My-absolute-path"));
MediaPlayer1.Play();
}
See Multimedia Overview:
MediaElement is a UIElement that is
supported by The Layout System and can
be consumed as the content of many
controls. It is also usable in
Extensible Application Markup Language
(XAML) as well as code. MediaPlayer,
on the other hand, is designed for
Drawing objects and lacks layout
support. Media loaded using a
MediaPlayer can only be presented
using a VideoDrawing or by directly
interacting with a DrawingContext.
MediaPlayer cannot be used in XAML.

How to fetch attributes of image in silverlight?

I am loading an image to my silverlight application. This image will fit in a 3d model as a texture map. I need to fetch image attributes. For that I am using ImageOpened event, like this:
public MainPage()
{
BitmapImage img = new BitmapImage(new Uri("imagens/textura.jpg", UriKind.Relative));
img.ImageOpened += new EventHandler<RoutedEventArgs>(img_ImageOpened);
img.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(img_ImageFailed);
imageBrush.ImageSource = img;
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
this.MouseLeftButtonUp += new MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
(...)
and then:
private void img_ImageOpened(object sender, RoutedEventArgs e)
{
BitmapImage i = sender as BitmapImage;
ImgSize.Width = i.PixelWidth;
ImgSize.Height = i.PixelHeight;
MessageBox.Show("LOADED IMAGE SIZE\n W:" + ImgSize.Width.ToString() + " H:" + ImgSize.Height.ToString());
}
The messagebox is showing the correct values for the loaded picture. But this is running after the scene is loaded, so the size is always default (0,0) ... I don't know how to fix this. I've runned the debugger, i've noticed that the scene and the model is rendered and the picture width and height is zero. After this, the event is fired ... I can't figure it out.
Thanks in advance,
Jose'
First off this is a little confusing:-
imageBrush.ImageSource = img;
InitializeComponent();
Unless you have something quite unusual happening the imageBrush object will be null until after the InitializeComponent has run.
As to your question at a guess you load the 3D model in MainPage_Loaded. The problem then is that his happens asynchronously with the arrival of the bitmap. Hence you do not want to actually load the 3D Model until both Loaded and ImageOpened events have both occured. Note it would be dangerous to assume that ImageOpened would always happen last.
The simplest solution I can think of would be to move all your existing code in the MainPage_Loaded event to ImageOpened then move the code that fetches the image to the MainPage_Loaded. This serialises the sequence, when execution of ImageOpened you are guaranteed that the page has loaded.
Not the most sophisticated solution and doesn't make use of benefits of the asynchronous nature of SL. However it should get you going and you can assess whether there is any benefit in having Page loading and bitmap downloading operating in parallel.

Resources