我正在製作一個C#Windows窗體應用程序,使用DirectX託管代碼在某些時候播放視頻。我想讓應用在播放視頻後立即退出,因此我試圖處理視頻的Ending事件,並引發異常。 下面是代碼:NullReferenceException處理視頻結束事件
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;
using System.Diagnostics;
namespace Picture_Button
{
public partial class Form1 : Form
{
Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
//Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
private int clicks = 0;
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
video.Ending += new System.EventHandler(this.Video_Ending);
//video.Ending += Video_Ending;
}
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.Owner = this;
video.Play();
/*video.Dispose();
Application.Exit();*/
}
break;
}
}
private void Video_Ending(object sender, EventArgs e)
{
//throw new NotImplementedException();
video.Dispose();
Application.Exit();
}
}
}
例外:
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:
另外,我注意到,該方案完美地工作而無需結束事件的代碼。
如果代碼是完全託管的,你甚至需要手動處理視頻嗎? –
我已經看過那篇文章,但是我看不到我的代碼究竟有什麼問題 –
我不確定是否需要手動處理它 –