Cosmin Petolea
Cosmin Petolea

Reputation: 119

C# DirectX Video Not Playing

I want to make a C# Windows Forms App That displays a pen which changes into a pineapple when you click it, turns into an apple when you click the pineapple and the back into a pen, which, when you click, starts a music video. What doesn't work for me is the video, which I do not want to display in a Windows Media Player plainly because I don't like it. Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using Microsoft.DirectX;

namespace Picture_Button
{
    public partial class Form1 : Form
    {
        Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
        private int clicks = 0;
        public Form1()
        {
            InitializeComponent();
            video.Owner = this;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            clicks++;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (clicks)
            {
                case 0: pictureBox1.Image = Properties.Resources.Pineapple; break;
                case 1: pictureBox1.Image = Properties.Resources.Apple; break;
                case 2: pictureBox1.Image = Properties.Resources.Pen; break;
                case 3: video.Play(); break;
                case 4: video.Dispose(); break;
            }
        }
    }
}

And literally nothing happens, the program simply freezes as if it were into an infinite loop here:

Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");

Nothing shows up. Any ideas what the problem is?

EDIT: I'm trying to handle the Ending event so I can make the app exit when the video ends and somehow I managed to get this exception:

System.NullReferenceException occurred
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.DirectX.AudioVideoPlayback
StackTrace:
at VideoWndProc(HWND__* hWnd, UInt32 uMsg, UInt32 wParam, Int32 lParam)
InnerException: 

By adding this code:

video.Ending += new System.EventHandler(this.Video_Ending);
//some code
private void Video_Ending(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        video.Dispose();
        Application.Exit();
    }

Upvotes: 3

Views: 2918

Answers (1)

NineBerry
NineBerry

Reputation: 28559

There are two separate issues here:

Support for Legacy .net

First issue: "DirectX for Managed Code" is very old and based on .net version 1.1. In order to use this assembly in .net 4 or newer, you need to enable loading these older formats. You do this by changing the file "app.config" in your project and setting useLegacyV2RuntimeActivationPolicy to true on the startup node, so it looks similar to this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true" > 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
</configuration>

Note that during debugging, you can get an exception "Managed Debugging Assistant 'LoaderLock' has detected a problem". You can ignore this exception. Tell Visual Studio that it should not stop at this exception during debugging.

Related question/answer

Codec support

The second issue is that you need to have a codec installed that allows DirectX to play mp4 files.

These codecs are not included by default in most versions of Windows (also not in Windows 10). Even when your Windows Media Player can playback an mp4 file, this doesn't mean the correct codec is available that can be used from DirectX.

I found that installing the LAV filters is a simple and non-intrusive way of making most video formats available to DirectX on Windows.

Video Playback in Debug Mode

You will find that often when you start the application from Visual Studio, video playback will be choppy and have a low quality. Quality will be perfect when starting the application without debugging.

Upvotes: 6

Related Questions