2013-07-03 108 views
1

現在我可以錄製自己的聲音或在pjsip中傳出(通過麥克風),但我只需要錄製通過揚聲器傳輸的傳入音頻。這裏是我用.wav格式錄製外發語音的代碼。 請幫我完成這個任務。感謝Adv。PJSIP中只有傳入錄音

-(int)startRecordAudio 
{ 
    pj_caching_pool cp; 
    pjmedia_endpt *med_endpt; 
    pj_pool_t *pool; 
    pjmedia_port *file_port; 
    pjmedia_snd_port *snd_port; 
    char tmp[10]; 

    pj_status_t status; 

    /* Must init PJLIB first: */ 
    status = pj_init(); 
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); 

    /* Must create a pool factory before we can allocate any memory. */ 
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); 
    status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); 
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); 

    /* Create memory pool for our file player */ 
    pool = pj_pool_create(&cp.factory, /* pool factory  */ 
          "app",   /* pool name.  */ 
          4000,   /* init size  */ 
          4000,   /* increment size  */ 
          NULL   /* callback on error */ 
         ); 

    /* Create WAVE file writer port. */ 
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString* foofile = [documentsPath stringByAppendingPathComponent:@"output.wav"]; 

    status = pjmedia_wav_writer_port_create(pool, 
              [foofile cStringUsingEncoding:NSUTF8StringEncoding], 
              CLOCK_RATE, 
              NCHANNELS, 
              SAMPLES_PER_FRAME, 
              BITS_PER_SAMPLE, 
              0, 0, 
              &file_port); 

    if (status != PJ_SUCCESS) { 
     NSLog(@"Unable to write file"); 
     return 1; 
    } 

    /* Create sound player port. */ 
    status = pjmedia_snd_port_create_rec(pool,  /* pool   */ 
             -1,   /* use default dev.  */ 
             PJMEDIA_PIA_SRATE(&file_port->info),/* clock rate.  */ 
             PJMEDIA_PIA_CCNT(&file_port->info),/* # of channels.  */ 
             PJMEDIA_PIA_SPF(&file_port->info), /* samples per frame. */ 
             PJMEDIA_PIA_BITS(&file_port->info),/* bits per sample.  */ 
             0,  /* options  */ 
             &snd_port  /* returned port  */ 
             ); 

    if (status != PJ_SUCCESS) { 
     // app_perror(THIS_FILE, "Unable to open sound device", status); 
     return 1; 
    } 

    /* Connect file port to the sound player. 
    * Stream playing will commence immediately. 
    */ 

    status = pjmedia_snd_port_connect(snd_port, file_port); 
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); 

    /* Sleep to allow log messages to flush */ 
    pj_thread_sleep(10); 

    if (fgets(tmp, sizeof(tmp), stdin) == NULL) { 
     puts("EOF while reading stdin, will quit now.."); 
    } 

    return 0; 
} 

回答

0

你不需要做太多。 在呼叫媒體狀態檢查呼叫激活並連接以下在Java中但功能調用完全相同。

if (call_media_status == pjsua_call_media_status.PJSUA_CALL_MEDIA_ACTIVE || 
       call_media_status == pjsua_call_media_status.PJSUA_CALL_MEDIA_REMOTE_HOLD) { 
    int callConfSlot = call_info.getConf_slot(); 
    int status = pjsua.pjsua_recorder_create(pjsua.pj_str_copy("/storage/sdcard0/test.wav"), 0, null, 0, 0, ids); 
    if (status != pjsuaConstants.PJ_SUCCESS) { 
     App.logd("NOT SUCCESSFULL IN RECORDING=" + status);  
    } else { 
     App.logd("Scccuessfull in recording"); 
     int recordPort = pjsua.pjsua_recorder_get_conf_port(ids[0]); 
     pjsua.conf_connect(callConfSlot, recordPort); 
     pjsua.conf_connect(0, recordPort);//this line includes microphone 
    }}