团结引擎 OpenHarmony getRecordNumDriver返回0 无法录音

在团结引擎中调用以下代码:

FMOD.System system;
FMOD.RESULT result = FMOD.Factory.System_Create(out system);
ERRCHECK(result);

IntPtr extraDriverData = new IntPtr();
result = system.init(100, FMOD.INITFLAGS.NORMAL, extraDriverData);
ERRCHECK(result);
        
int numDrivers, numConnected;
result = system.getRecordNumDrivers(out numDrivers, out numConnected);
ERRCHECK(result);
Debug.Log("numDrivers : " + numDrivers);
Debug.Log("numConnected : " + numConnected);
if (numDrivers == 0)
{
    Debug.LogError("No recording devices found/plugged in!  Aborting.");
    return;
}
        
result = system.getRecordDriverInfo(0, out string name, 512, out guid, out Guid nativeRate, out FMOD.SPEAKERMODE speakermode, out int nativeChannels, out FMOD.DRIVER_STATE state);
ERRCHECK(result);
Debug.Log("nativeRate : " + nativeRate);

FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
exinfo.cbsize = Marshal.SizeOf(exinfo);
exinfo.numchannels = nativeChannels;
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.defaultfrequency = nativeRate;
exinfo.length = (uint)(nativeRate * sizeof(short) * nativeChannels);
byte[] data = null;
result = system.createSound(data, FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exinfo, out sound);
ERRCHECK(result);

result = system.recordStart(0, sound, true);
ERRCHECK(result);

getRecordNumDrivers函数始终返回0,导致无法录音。

若不判断设备数量:

……
// if (numDrivers == 0)
// {
//    Debug.LogError("No recording devices found/plugged in!  Aborting.");
//    return;
// }
        
int nativeRate = 24000;
int nativeChannels = 1;

FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
exinfo.cbsize = Marshal.SizeOf(exinfo);
exinfo.numchannels = nativeChannels;
exinfo.format = FMOD.SOUND_FORMAT.PCM16;
exinfo.defaultfrequency = nativeRate;
exinfo.length = (uint)(nativeRate * sizeof(short) * nativeChannels);
byte[] data = null;
result = system.createSound(data, FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exinfo, out sound);
ERRCHECK(result);

result = system.recordStart(0, sound, true);
ERRCHECK(result);

则会在recordStart时报FMOD.RESULT.ERR_INVALID_POSITION错误
同样的代码在PC,Android,IOS上都能正常录音

你好,非常感谢你分享的代码和信息!

可以请你提供一下以下信息吗:

  • 团结引擎版本号
  • FMOD OpenHarmony插件的版本号

还有就是可否麻烦你在 FMOD Settings里进行如下操作,并将团结引擎控制台里的完整日志信息分享给我们进行调查:

  • 设置日志级别:​将 Logging Level 设置为 Log
  • 启用 API 错误日志:​勾选 Enable API Error Logging

你好,谢谢你的回复,以下是版本信息:

  • 团结引擎版本号:1.4.4

  • FMOD OpenHarmony插件版本号:2.02.25

以下是运行时的完整log:

[FMOD] FMOD_OS_Init : Cannot access Resource Manager, rawfile support unavailable, ensure fmod.init has been called with UIAbility

[FMOD] System::create : Header version = 2.02.25. Current version = 2.02.25.

[FMOD] SystemI::setOutputInternal : Setting output to 'FMOD OpenHarmony Audio Output'

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.cbSize                   = 104

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.maxFADPCMCodecs          = 32

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.defaultDecodeBufferSize  = 400

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.distanceFilterCenterFreq = 1500.000000

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.DSPBufferPoolSize        = 8

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.resamplerMethod          = 2

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.randomSeed               = 1252847472

[FMOD] SystemI::setAdvancedSettings : mAdvancedSettings.maxConvolutionThreads    = 3

[FMOD] Manager::init : maxchannels = 128 studioflags = 00000008 flags 00000000 extradriverdata 0.

[FMOD] SystemI::init : Initialize version=20225 (147536), maxchannels=128, flags=0x00020000

[FMOD] OHAudio_Init : Output buffer size: 1114 samples, DSP buffer: 2048 * 4.

[FMOD] Thread::initThread : Init FMOD stream thread. Affinity: 0x4000000000000003, Priority: 0xFFFF7FFB, Stack Size: 98304, Semaphore: No, Sleep Time: 10, Looping: Yes.

[FMOD] SystemI::DSPCodecPoolRegister : register codec pool for pool type 7

[FMOD] Thread::initThread : Init FMOD mixer thread. Affinity: 0x4000000000000001, Priority: 0xFFFF7FFA, Stack Size: 81920, Semaphore: Yes, Sleep Time: 0, Looping: Yes.

[FMOD] AsyncManager::init : manager 0x5ccba89808 isAsync 1 updatePeriod 0.02

[FMOD] Thread::initThread : Init FMOD Studio update thread. Affinity: 0x4000000000000002, Priority: 0xFFFF7FFD, Stack Size: 98304, Semaphore: No, Sleep Time: 1, Looping: No.

[FMOD] AsyncManager::init : done

[FMOD] PlaybackSystem::init : 

[FMOD] Thread::initThread : Init FMOD Studio sample load thread. Affinity: 0x4000000000000003, Priority: 0xFFFF7FFD, Stack Size: 98304, Semaphore: No, Sleep Time: 1, Looping: No.

[FMOD] OutputRingBuffer::read : Buffer starvation detected, requested 35664 bytes, 29872 / 65536 bytes available.

[FMOD] PlaybackSystem::init : done

[FMOD] Thread::initThread : Init FMOD Studio bank load thread. Affinity: 0x4000000000000003, Priority: 0xFFFF7FFD, Stack Size: 98304, Semaphore: No, Sleep Time: 1, Looping: No.

[FMOD] Manager::init : done.

[FMOD] FOpenFile::open : fopen failed to open 'Data/StreamingAssets/Master.bank', errno = 2

[FMOD] RuntimeBankModel::openFile : Failed to open file 'Data/StreamingAssets/Master.bank'

[FMOD] System::loadBankFile(Data/StreamingAssets/Master.bank, 0, 0x5caad0bfb0) returned ERR_FILE_NOTFOUND for STUDIO_SYSTEM (0x1FFF1F).
BankLoadException: [FMOD] Could not load bank 'Data/StreamingAssets/Master.bank' : ERR_FILE_NOTFOUND : File not found.
system output: OHAUDIO
system drivers #: 2
system driver info: rate 48000, mode STEREO, channels 2
system driver info: rate 48000, mode STEREO, channels 2
fmod setting : rate 48000, mode STEREO, channels 2
fmod setting : DSP buffer size 2048, num 4
latency by DSP: 170
[FMOD] SystemI::init : Initialize version=20225 (147536), maxchannels=100, flags=0x00000000

[FMOD] SystemI::setOutputInternal : Setting output to 'FMOD OpenHarmony Audio Output'

[FMOD] OHAudio_Init : Output buffer size: 1114 samples, DSP buffer: 1024 * 4.

[FMOD] Thread::initThread : Init FMOD stream thread. Affinity: 0x4000000000000003, Priority: 0xFFFF7FFB, Stack Size: 98304, Semaphore: No, Sleep Time: 10, Looping: Yes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

MicRecorder numDrivers : 0
MicRecorder numConnected : 0
No recording devices found/plugged in!  Aborting.
[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

[FMOD] OutputRingBuffer::read : Insufficient buffer size detected, requested 35664 bytes, buffer size is 32768 bytes.

谢谢你提供的版本号和完整Log信息!

我这边已经成功复现了你提到的 getRecordNumDriver 在OpenHarmony平台上函数始终返回0的问题,我已将此问题提交给开发团队进行深入调查。​一旦有最新进展,我会第一时间通知你。​非常感谢你让我们注意到这个问题。

此外,我们注意到日志中存在以下信息:

这表明在 OpenHarmony 平台上,FMOD 可能未正确初始化,可能导致无法播放声音。​我们在目前最新的 2.02.27 版本中已修复了此问题。​建议你可以尝试升级到此版本以解决该问题。

这个信息表示缓冲区大小不足,可能导致无法听到声音。​遇到问题的话你可以参考这个帖子里的解决方法:FMOD 在OpenHarmony 上播放不出声音,日志里面很多如下错误 - #2 by li_fmod

非常感谢你们对这个问题的重视! :+1:

1 Like