我想制作一个C#Windows Forms应用程序,该应用程序显示一支笔,当您单击它时它会变成一个菠萝,当您单击该菠萝时会变成一个苹果,然后变成笔,单击时会启动一个音乐视频。对我来说不起作用的是视频,我不想因为不喜欢而将其显示在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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
实际上什么也没发生,程序只是冻结,就好像进入了一个无限循环:
Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
Run Code Online (Sandbox Code Playgroud)
什么都没有出现。任何想法是什么问题?
编辑:我正在尝试处理Ending事件,以便我可以在视频结束时退出应用程序,并以某种方式设法获得此异常:
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:
Run Code Online (Sandbox Code Playgroud)
通过添加以下代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
这里有两个单独的问题:
第一个问题:“ DirectX for Managed Code”非常老,并且基于.net 1.1版。为了在.net 4或更高版本中使用此程序集,您需要启用加载这些旧格式。您可以通过在项目中更改文件“ app.config”并 在节点上将其设置useLegacyV2RuntimeActivationPolicy为来true执行此操作startup,因此它类似于以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true" >
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)
请注意,在调试过程中,您会收到异常“ Managed Debugging Assistant'LoaderLock'已检测到问题”。您可以忽略此异常。告诉Visual Studio在调试过程中不应在此异常处停止。
第二个问题是您需要安装允许DirectX播放mp4文件的编解码器。
这些编解码器默认情况下不包含在大多数Windows版本中(Windows 10中也不包含)。即使您的Windows Media Player可以播放mp4文件,也并不意味着可以从DirectX使用正确的编解码器。
我发现安装LAV筛选器是一种使Windows上DirectX可以使用大多数视频格式的简单且非侵入性的方式。
您会发现,通常当您从Visual Studio启动应用程序时,视频播放会断断续续并且质量低下。在不调试的情况下启动应用程序时,质量将是完美的。
| 归档时间: |
|
| 查看次数: |
1566 次 |
| 最近记录: |