我想制作一个C# Windows窗体应用程序,当你点击它时,会显示一个笔,然后变成菠萝,再点击它会变成苹果,最后又变回笔。当你点击笔时,会播放一段音乐视频。 但是我不喜欢使用Windows Media Player来显示视频,这对我来说不起作用。以下是代码:
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;
}
}
}
}
实际上什么事情也没有发生,程序就像陷入无限循环一样冻结在这里:
Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
没有任何内容显示。你有什么想法是问题出在哪里了吗?
编辑:我正在尝试处理“结束”事件,以便在视频结束时使应用程序退出,但不知何故,我遇到了这个异常:
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:
通过添加以下代码:
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();
}