I have a C++ class that utilizes winrt::Windows::Storage::Streams::InMemoryRandomAccessStream. This class will compile, but it will not link... I get the following error:
LNK2019 unresolved external symbol "public: __cdecl winrt::Windows::Storage::Streams::InMemoryRandomAccessStream::InMemoryRandomAccessStream(void)"
I have tried to complie this code with the following variations all with the same linker error:
- different versions of the Windows SDK.
- different versions of the C++ compiler (17 & 20) always with the same result.
I have reduced this class down to the following:
Microphone.h
#pragma once
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Composition.h>
#include <winrt/Windows.UI.Input.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Media.Capture.h>
#include <winrt/Windows.Media.MediaProperties.h>
using namespace winrt::Windows::Media::Capture;
using namespace winrt::Windows::Media::MediaProperties;
class Microphone
{
public:
Microphone();
void StartRecording();
void StopRecording();
winrt::Windows::Storage::Streams::InMemoryRandomAccessStream AudioStream();
private:
MediaCapture mediaCapture;
winrt::Windows::Storage::Streams::InMemoryRandomAccessStream audioStream;
};
Microphone.cpp
#include "Microphone.h"
Microphone::Microphone()
{
// Initialize the MediaCapture object
mediaCapture = MediaCapture();
auto settings = MediaCaptureInitializationSettings();
settings.StreamingCaptureMode(StreamingCaptureMode::Audio);
mediaCapture.InitializeAsync(settings).get();
}
void Microphone::StartRecording()
{
// Set up the audio encoding properties
auto audioProfile = MediaEncodingProfile::CreateWav(AudioEncodingQuality::High);
// Start recording audio
mediaCapture.StartRecordToStreamAsync(audioProfile, audioStream).get();
}
void Microphone::StopRecording()
{
// Stop recording audio
mediaCapture.StopRecordAsync().get();
}
winrt::Windows::Storage::Streams::InMemoryRandomAccessStream Microphone::AudioStream()
{
return audioStream;
}
MicMain.cpp
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/base.h>
#include <thread>
#include <iostream>
#include <future>
#include "Microphone.h"
using namespace winrt;
using namespace Windows::Storage::Streams;
using namespace Windows::Foundation;
void DumpStreamToConsol(InMemoryRandomAccessStream stream)
{
auto size = stream.Size();
if (size > std::numeric_limits<uint32_t>::max())
{
return;
}
auto reader = DataReader(stream.GetInputStreamAt(0));
reader.LoadAsync(static_cast<uint32_t>(size)).get();
for (uint32_t i = 0; i < size; ++i)
{
uint8_t byte = reader.ReadByte();
std::cout << byte;
}
}
int main(int argc, char* argv[])
{
winrt::init_apartment();
// Print out the command line arguments
for (int i = 0; i < argc; ++i) {
std::cout << "Argument " << i << ": " << argv[i] << std::endl;
}
// Your code here
Microphone mic;
// capture 5 seconds of audio
mic.StartRecording();
std::this_thread::sleep_for(std::chrono::seconds(5));
mic.StopRecording();
auto stream = mic.AudioStream();
DumpStreamToConsol(stream);
return 0;
}```