Found out what was going on -- Accelerometers and VideoBrushes running at the same time will freeze the VideoBrush. To fix this, stop the accelerometer before starting the VideoBrush
With minimal code (noted just below), it will reliably crash at around 2 minutes and 15 seconds after the VideoBrush starts on the app. This is on the Windows 7 emulator for a Windows Phone 7.5 app, using c# and Xaml. It happens if VideoBrush stretch is also set to fill, etc (there was a Silverlight 1.0 bug with this)
For the story of how I derived to this, please check out the wall of text answer below : )
Why does this happen? I do not know. Perhaps there is a memory leak or something? Of note -- this actually does not crash the app itself. You will not get any exception popping up.
Code to reproduce the problem (make a new application titled PhoneApp1 and try it yourself!):
Xaml Code:
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="False">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Rectangle x:Name="viewfinderCanvas" Width="480" Height="800" >
<Rectangle.Fill>
<VideoBrush x:Name="viewfinderBrush"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</phone:PhoneApplicationPage>
C# code:
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices;
using Microsoft.Devices.Sensors;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
private Accelerometer AccelerometerSensor;
// Constructor
public MainPage()
{
InitializeComponent();
AccelerometerSensor = new Accelerometer();
AccelerometerStartup();
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true))
{
viewfinderCanvas.Visibility = Visibility.Visible;
var cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
viewfinderBrush.SetSource(cam);
}
}
#region Accelerometer Startup Function
private void AccelerometerStartup()
{
try
{
if (AccelerometerSensor != null)
{
AccelerometerSensor.Start();
}
}
catch (AccelerometerFailedException)
{
}
catch (UnauthorizedAccessException)
{
}
}
#endregion
}
}
Original Question Below: (code noted below preserved, but will not reproduce problem as no accelerometer is attached)
This is a fun one, I promise.
I decided to put in my wp7.5 app a rectangle with which its fill property is a VideoBrush. I've used the code found from a few websites, and thought everything was all right. THis was until I noticed that the VideoBrush from the camera would freeze... anywhere from 6 - 40 seconds of running it.
Baffled, I thought my dispatch timers were interfering with the camera. Commenting them out so that they could not run did not fix the problem. I then tried disabling my ads in the app. Also didn't fix it. I disabled everything that could render an update throughout the app (timers, dispatch timers, ad rotations, any loops) and it STILL freezes.
I did the same thing and even unplugged my device from the computer as I read debugging with the device while connected to the computer with zune open could disrupt things. Still no dice.
I created a new project and copy pasted just the VideoBrush code in, and it works without freezing.
The XAML code:
<Rectangle x:Name="viewfinderCanvas" Width="480" Height="800" Visibility="Collapsed" DoubleTap="viewfinderCanvas_DoubleTap">
<Rectangle.Fill>
<VideoBrush x:Name="videoBrush">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="previewTransform"
CenterX=".5"
CenterY=".5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Rectangle.Fill>
</Rectangle>
C# code:
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true))
{
viewfinderCanvas.Visibility = Visibility.Visible;
var cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
if (Orientation == PageOrientation.PortraitUp || Orientation ==
PageOrientation.PortraitDown || Orientation == PageOrientation.Portrait)
{
videoBrush.RelativeTransform =
new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 };
}
videoBrush.SetSource(cam);
}
So truly, I have no idea what is causing this freezing of the VideoBrush display. I put breakpoints on every function and nothing picked up. No error messages exist... the video just freezes.
Has anyone encountered this before? It happens on both my device and on the computer I code from too -- the emulator's white box just stops.
for clarity -- purpose of this is to just show what the camera sees in the app -- I am not taking photos or recording video. I am merely making this show. To close the window, a user will double tap the rectangle to close the raw view.