I have been working on a Windows Phone 7 application for viewing and consuming web content, including videos. During testing of the application, I came across a problem that was driving me crazy: Videos would not play in the emulator. While I was working on this, I was also testing new video links, so I was thinking there was a problem with the video content or a incompatibility I was not aware of. Needless to say, I spent a decent amount of time researching and digging for answers… and then it hit me. I tried it on the phone to no avail. Then I finally disconnected the sync cable from the phone and boom, it starts working perfectly — no errors to be seen.
So the quick point is, when running across a problem that just seems crazy, deploy to device and disconnect.
To explain the problem, I will just give short example that allows you to dupe the issue. I have not yet been able to nail down completely what the problem is, but it does seem that all videos having the issue are VC-1 encoded for video. I have tested this on three different dev machines and had the same issue across all.
In your WP7 application you can just add a MediaElement to any page.
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY Video Test" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Video Viewer" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
<MediaElement x:Name="MediaElement" Grid.Row="2"/>
</Grid>Next, in the cs file for the page you can set the source for the MediaElement, and by default the control is set to AutoPlay. Once the source is set, it should start playing when you run the app in the emulator.
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
ConfigureVideoPlayer();
}
public void ConfigureVideoPlayer()
{
MediaElement.Source = new Uri("http://content5.catalog.video.msn.com/e2/ds/b2ee41d1-8d9f-4c70-b40d-303ebe154f6f.wmv", UriKind.Absolute);
}
}The video in this sample is a WMV that is using Windows Media Audio 10 Professional for audio and VC-1 video. If you deploy this application to a device for testing, it will not work until you remove the device from its connection to the host computer. Also, if you want to see a video that plays with no problems, just change the Uri to use http://download.microsoft.com/download/B/7/4/B741136A-7672-4B74-88A1-04DE9443574F/Springboard_061809_MBR.wmv and then run the app. This second video is Windows Media Audio 9.2 and Windows Media Video 9.
Now with the new video lets hook up to the MediaFailed event and take a look at what is getting reported back:
// Constructor
public MainPage()
{
InitializeComponent();
ConfigureVideoPlayer();
}
public void ConfigureVideoPlayer()
{
MediaElement.Source = new Uri("http://content5.catalog.video.msn.com/e2/ds/b2ee41d1-8d9f-4c70-b40d-303ebe154f6f.wmv", UriKind.Absolute);
MediaElement.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(MediaElement_MediaFailed);
}
void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
var errorException = e.ErrorException;
}When running this code and looking the exception being reported, we see that the message is “AG_E_NETWORK_ERROR” and appears to be a generic error that can be given when there are authentication issues, cross domain access issues, or just problems opening files.