Answered by:
Programmatic MediaElement plays Audio but no visual

Question
-
I've created a mediaelement using the following code:
var vFolder = await Windows.Storage.KnownFolders.VideosLibrary.GetFolderAsync(myFolder); MediaElement AV1 = new MediaElement(); var file1 = await vFolder.GetFileAsync("movie (1).mp4"); var stream1 = await file1.OpenAsync(Windows.Storage.FileAccessMode.Read); AV1.SetSource(stream1, file1.ContentType); AV1.Height = 400; AV1.Width = 300; AV1.Visibility = Windows.UI.Xaml.Visibility.Visible; //chainPanel.Children.Add(AV1); AV1.Play();
When I run the code I can hear the movie play but I'm not seeing any vision. I've also tried using Margin with no luck. Eventually I want to programmatically add the mediaelement to the stack panel. Any ideas?
thanks.
Tuesday, October 22, 2013 8:10 AM
Answers
-
What is chainPanel and why is it commented out? The MediaElement needs to be in the visual tree. You need to add it to a visible container such as your StackPanel before it will be displayed.
--Rob
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Thursday, October 24, 2013 7:22 AM
Tuesday, October 22, 2013 3:10 PMModerator -
Solved. The mediaelement needs to be added to the visual tree BEFORE setting the source, like this:
var vFolder = await Windows.Storage.KnownFolders.VideosLibrary.GetFolderAsync(myFolder); MediaElement AV1 = new MediaElement(); var file1 = await vFolder.GetFileAsync("movie (1).mp4"); var stream1 = await file1.OpenAsync(Windows.Storage.FileAccessMode.Read); chainPanel.Children.Add(AV1); AV1.SetSource(stream1, file1.ContentType);
- Marked as answer by FSound Thursday, October 24, 2013 5:44 AM
Thursday, October 24, 2013 5:44 AM
All replies
-
What is chainPanel and why is it commented out? The MediaElement needs to be in the visual tree. You need to add it to a visible container such as your StackPanel before it will be displayed.
--Rob
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Thursday, October 24, 2013 7:22 AM
Tuesday, October 22, 2013 3:10 PMModerator -
Thanks that makes sense. chainPanel is the stackPanel but if I run the code it throws the error "Object reference not set to an instance of an object.". However if I create a new button and run "chainPanel.Children.Add(newbutton)" that works and I see the button show up. Am I missing a step with the media element somewhere?Wednesday, October 23, 2013 1:19 AM
-
Solved. The mediaelement needs to be added to the visual tree BEFORE setting the source, like this:
var vFolder = await Windows.Storage.KnownFolders.VideosLibrary.GetFolderAsync(myFolder); MediaElement AV1 = new MediaElement(); var file1 = await vFolder.GetFileAsync("movie (1).mp4"); var stream1 = await file1.OpenAsync(Windows.Storage.FileAccessMode.Read); chainPanel.Children.Add(AV1); AV1.SetSource(stream1, file1.ContentType);
- Marked as answer by FSound Thursday, October 24, 2013 5:44 AM
Thursday, October 24, 2013 5:44 AM