Skip to content
Ondrej Medek edited this page Oct 16, 2017 · 8 revisions

Headless Examples

Examples of using WPF-MediaKit headless - without GUI, e.g. as a service.

Note, these examples demonstrates just basic functionality. E.g. you should hook on MediaOpened, MediaFailed events to catch the errors, etc.

Play Sounds

static void Main(string[] args)
{
    _player = new MediaUriPlayer();
    _player.EnsureThread(ApartmentState.MTA);
    _player.Dispatcher.BeginInvoke(new Action(() =>
    {
        _player.Source = new Uri("[MyUri]"); // mp3, wma, m3u, ...
        _player.Play();
    }));
    Console.ReadKey();
}

Grab Screenshots

static void Main(string[] args)
{
    IntPtr backBuffer;
    _player = new MediaUriPlayer();
    _player.EnsureThread(ApartmentState.MTA);
    _player.NewAllocatorFrame += () => GrabScreenShot(backBuffer);
    _player.NewAllocatorSurface += (s, b) => backBuffer = b;
    _player.Dispatcher.BeginInvoke(new Action(() =>
    {
        _player.AudioDecoder = null;
        _player.AudioRenderer = null;
        _player.Source = source;
        _player.MediaPosition = second * MediaPlayerBase.DSHOW_ONE_SECOND_UNIT;
        _player.Pause();
    }));
    Console.ReadKey();
}

private static void GrabScreenShot(IntPtr backBuffer)
{
    // grab the screenshot, e.g.
    D3DImage d3d = new D3DImage();
    D3DImageUtils.SetBackBufferWithLock(d3d, backBuffer);
    // display or process the d3d
}

Also, see the Gist VideoScreenGrabber.cs for the more elaborate example.

Clone this wiki locally