Hello all,
We've been working away at getting HostVisual and VisualTarget working, and I'm hoping someone out there can give us a little more insight.
At this point we are inheriting from FrameworkElement and using DrawingVisual and HostVisual to host a MediaPlayer. The next step, rigging up VisualTarget and getting a secondary thread on the go, is where we're at a loss.
Below is the code we currently have; I've included the important bits. If you want to test this, just comment out the event handlers and come up with a WMV file.
public class HostedMediaPlayer : FrameworkElement
{
public event EventHandler<MediaEventData> ErrorHappened;
DrawingVisual _dv = null;
HostVisual _hv = null;
public HostedMediaPlayer()
{
_dv = new DrawingVisual();
_hv = new HostVisual();
MediaPlayer mp = new MediaPlayer();
// rig up the Media Player
mp.MediaFailed += new EventHandler<ExceptionEventArgs>(mp_MediaFailed);
mp.Open(new Uri(@"C:\video\test.wmv"));
mp.MediaEnded += new EventHandler(mp_MediaEnded);
// tie the video to the DrawingVisual's context
using (DrawingContext dc = _dv.RenderOpen())
{
dc.DrawVideo(mp, new Rect(0, 0, 320, 240));
}
// configure the HostVisual to host the DrawingVisual and add it
// to the FrameworkElement's visual/logical children
_hv.Children.Add(_dv);
AddVisualChild(_hv);
AddLogicalChild(_hv);
// start the video
mp.Play();
}
//<snipped MediaPlayer event handlers />
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
if (index != 0)
throw new ArgumentOutOfRangeException("index");
return _hv;
}
}
This code works just fine. In fact, if you create an instance of the above class and put it on a WPF form the video plays almost instantly.
We know that VisualTarget accepts a HostVisual as a param in the constructor, but this doesn't really clarify the roles each play to each other.
My questions would be as follows:
1) how do you involve VisualTarget from here
2) where does the secondary thread come from
Any help is appreciated.
Cheers,
James