想使用miniaudio这个c库(音频相关,可录音和播放等)封装到自己的c++动态链接库中,让后由自己的Maui程序来调用库中的方法。这样做的目的是利用这个库的跨平台特性,让自己的Maui程序写一次就能运行到win、ios、android、Mac上。但是目前我封装的库是VS写的 C++库, 能被C#控制台应用调用,但是集成到Maui中就不行。请问有什么思路吗?
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
ma_encoder* pEncoder = (ma_encoder*)pDevice->pUserData;
MA_ASSERT(pEncoder != NULL);
ma_encoder_write_pcm_frames(pEncoder, pInput, frameCount, NULL);
(void)pOutput;
}
int RecordSong(wchar_t* outFileName)
{
ma_result result;
ma_encoder_config encoderConfig;
ma_encoder encoder;
ma_device_config deviceConfig;
ma_device device;
encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 2, 44100);
if (ma_encoder_init_file((char*)outFileName, &encoderConfig, &encoder) != MA_SUCCESS) {
printf("Failed to initialize output file.\n");
return -1;
}
deviceConfig = ma_device_config_init(ma_device_type_capture);
deviceConfig.capture.format = encoder.config.format;
deviceConfig.capture.channels = encoder.config.channels;
deviceConfig.sampleRate = encoder.config.sampleRate;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &encoder;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
printf("Failed to initialize capture device.\n");
return -2;
}
result = ma_device_start(&device);
if (result != MA_SUCCESS) {
ma_device_uninit(&device);
printf("Failed to start device.\n");
return -3;
}
printf("Press Enter to stop recording...\n");
getchar();
ma_device_uninit(&device);
ma_encoder_uninit(&encoder);
return 0;
}
int PlaySong(wchar_t* fileName) {
ma_result result;
ma_engine engine;
result = ma_engine_init(NULL, &engine);
if (result != MA_SUCCESS) {
printf("%d", result);
return result;
}
ma_sound sound;
result = ma_sound_init_from_file(&engine, (char*)fileName, 0, NULL, NULL, &sound);
if (result != MA_SUCCESS) {
printf("%d", result);
return result;
}
ma_sound_start(&sound);
getchar();
return 0;
}
[DllImport("miniaudioapi.dll", EntryPoint = "HelloWorld", CallingConvention = CallingConvention.Cdecl)]
public static extern void HelloWorld(string name);
[DllImport("miniaudioapi.dll", EntryPoint = "PlaySong", CallingConvention = CallingConvention.Cdecl)]
public static extern void PlaySong(string fileName);
[DllImport("miniaudioapi.dll", EntryPoint = "RecordSong", CallingConvention = CallingConvention.Cdecl)]
public static extern void RecordSong(string outFileName);