C++ 如何使用Microsoft::DirectX::AudioVideoPlayback播放音频?

C++ 如何使用Microsoft::DirectX::AudioVideoPlayback播放音频?,c++,audio,visual-studio-2015,audiovideoplayback,C++,Audio,Visual Studio 2015,Audiovideoplayback,我正在编写一个简单的音乐播放器,我需要能够暂停音乐,因此我尝试使用Microsoft::DirectX::AudioVideoPlayback API来完成此操作,因为PlaySound不支持该功能。但是,程序会不断触发断点并引发异常 我创建了一个空的VisualC++ C++ CLULL空项目,只添加了代码来初始化一个新的音频对象,播放一个音频文件,而我仍然得到了同样的异常。出现问题的行是创建音频对象的新实例的行 我对VisualStudio不是很有经验,我以前从未使用过这个API。我已将对M

我正在编写一个简单的音乐播放器,我需要能够暂停音乐,因此我尝试使用Microsoft::DirectX::AudioVideoPlayback API来完成此操作,因为PlaySound不支持该功能。但是,程序会不断触发断点并引发异常

<>我创建了一个空的VisualC++ C++ CLULL空项目,只添加了代码来初始化一个新的音频对象,播放一个音频文件,而我仍然得到了同样的异常。出现问题的行是创建音频对象的新实例的行

我对VisualStudio不是很有经验,我以前从未使用过这个API。我已将对Microsoft::DirectX和Microsoft::DirectX::AudioVideoPlayback.dll文件的引用添加到项目中,音频文件位于目录中。我尝试了一些在网上找到的东西,但没有任何效果。任何帮助都将不胜感激

以下是MyForm.h文件:

#pragma once

namespace Project3 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace Microsoft::DirectX::AudioVideoPlayback;

/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
    MyForm(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~MyForm()
    {
        if (components)
        {
            delete components;
        }
    }

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->components = gcnew System::ComponentModel::Container();
        this->Size = System::Drawing::Size(300,300);
        this->Text = L"MyForm";
        this->Padding = System::Windows::Forms::Padding(0);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

        Audio^ myAudio;

        myAudio = gcnew Audio("Carl Grimes - Carl Poppa.wav", false);
        myAudio->Play();

    }
#pragma endregion
};
}
#pragma一次
命名空间项目3{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
使用名称空间Microsoft::DirectX::AudioVideoPlayback;
/// 
///我的表格摘要
/// 
公共引用类MyForm:public System::Windows::Forms::Form
{
公众:
我的表格(作废)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~MyForm()
{
if(组件)
{
删除组件;
}
}
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
这->组件=gcnewsystem::ComponentModel::Container();
此->尺寸=系统::图纸::尺寸(300300);
此->文本=L“MyForm”;
这->填充=System::Windows::Forms::Padding(0);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
音频^myAudio;
myAudio=gcnew Audio(“卡尔·格里姆-卡尔·波帕.wav”,假);
myAudio->Play();
}
#布拉格端区
};
}

我得到的错误如下:

An unhandled exception of type 'System.IO.FileLoadException' occurred in 
System.Windows.Forms.dll

Additional information: Mixed mode assembly is built against version 
'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without 
additional configuration information.
在研究了stackoverflow的几个问题和答案后,我想出了解决这个问题的方法

  • 创建一个.config文件并将其放置在.exe文件所在的文件夹中
  • 将.config文件命名为.exe文件的名称,包括扩展名(例如MusicPlayer.exe和MusicPlayer.exe.config)
  • 将以下代码粘贴到.config文件中

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
    </configuration>
    
    
    
    通过右键单击解决方案资源管理器->属性中的项目,然后单击配置属性->常规,可以检查.NETFramework的版本。您应该在Project Defaults->.NET目标框架版本下看到它