我正在尝试使用PJSUA2将来电直接记录到wav文件中,但是听不到wav文件中的任何音频。

代码如下:

void SipCall::onCallMediaState(pj::OnCallMediaStateParam& /*prm*/)
{
    pj::CallInfo call_info = getInfo();
    pj::AudioMedia* audio_media = 0;

    for (unsigned int i = 0; i < call_info.media.size(); ++i) {
        if (call_info.media[i].type == PJMEDIA_TYPE_AUDIO) {
            audio_media = static_cast<pj::AudioMedia*>(getMedia(i));
            break;
        }
    }

     if (audio_media != 0) {
            try {
                pj::AudioMediaRecorder recorder;
                recorder.createRecorder("file.wav");
                audio_media->startTransmit(recorder);
                pj_thread_sleep(5000);
                audio_media->stopTransmit(recorder);
            }
            catch (pj::Error& err) {
                qWarning("[SipAccount::onIncomingCall] : Failed to record call to %s. Error = %s", "file.wav", err.info().data());
           }
      }
 }

关于PJSUA2的文档不多,所以有人知道我在做什么错吗?

最佳答案

存在一个问题,因为AudioMediaRecorder对象在创建后就超出了范围。如果您使其成为类(class)的成员,则将可以进行以下操作。

void SipCall::onCallMediaState(pj::OnCallMediaStateParam& /*prm*/)
{
   pj::CallInfo call_info = getInfo();
   pj::AudioMedia* audio_media = 0;
   for (unsigned int i = 0; i < call_info.media.size(); ++i) {
      if (call_info.media[i].type == PJMEDIA_TYPE_AUDIO) {
        audio_media = static_cast<pj::AudioMedia*>(getMedia(i));
        break;
      }
   }

   if (audio_media != 0) {
        try {
            recorder.createRecorder("file.wav");
            audio_media->startTransmit(recorder);
        }
        catch (pj::Error& err) {
            qWarning("[SipAccount::onIncomingCall] : Failed to record call to %s. Error = %s", "file.wav", err.info().data());
       }
    }
 }

在下面的onCallState()方法中进行任何必要的清理:
void SipCall::onCallState(pj::OnCallStateParam& prm)
{
    int i = prm.e.type;
    i = 0;

    pj::CallInfo call_info = getInfo();

    switch (call_info.state) {
       case PJSIP_INV_STATE_DISCONNECTED:
          // Add clean up code here
          delete this;
          break;
       case PJSIP_INV_STATE_CONFIRMED:
          break;
       default:
          break;
   }
}

关于c++ - PJSUA2-将通话音频录制到WAV文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32949357/

10-11 18:00