I'm writing a simple music player and I need to be able to pause the music so I'm trying to use the Microsoft::DirectX::AudioVideoPlayback API to do this since PlaySound doesn't support that functionality. However the program keeps triggering a breakpoint and throwing an exception.
So I created a new empty Visual C++ CLR Empty Project with an empty form and only added the code to initialize a new audio object and play an audio file and I am still getting the same exception. The line where the issue occurs at is the line where the new instance of the audio object is created.
I'm not very experienced with Visual Studio and I've never used this API before. I have added references to Microsoft::DirectX and Microsoft::DirectX::AudioVideoPlayback .dll files to the project, the audio file is in the directory. I've tried a few things I found online but nothing has worked. Any help would be greatly appreciated!
Here's the MyForm.h file:
#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
};
}
The error I was getting was the following:
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.
After going through several questions and answers on stackoverflow I figured how to solve the issue.
Paste the following code in the .config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
You can check what version of .NETFramework you have by right clicking on the project in Solution Explorer -> Properties, then in Configuration Properties -> General. you should see it under Project Defaults -> .NET Target Framework Version.