[英]ios pjsip - play a sound during sip call
在饮时,有时我想发送dtmf数字。
为此,我创建了一个自定义拨号盘,当按下某个键时,该拨号盘应播放该键的声音,但在Sip通话期间不会播放该声音(没有通话时,会播放声音)。
这些声音是使用AudioToolbox.h
库( AudioServicesPlaySystemSound(soundID)
)中的函数播放的。
我需要在pjsip(pjsua)或AudioToolbox库中设置一些属性,以便在sip通话期间播放声音吗?
我知道这是可能的( Bria也有, Groundwire也有,不确定他们是否正在使用pjsip来实现sip)。
该答案是来自以下两个链接的代码片段的组合: PJSUA-API Media Manipulation和pjsipDll_PlayWav.cpp 。
当pjsua拨打电话时,它使用端口(会议端口)将呼叫目的地的媒体传输到设备扬声器。 您可以同时打开多个端口 。
因此,要播放键盘按钮的点击声音,我们要做的是再打开一个端口并播放声音(在这种情况下,它是一个wav文件,并且您会注意到,还有一个pjsua函数可用于传输avi文件) 。
为此,我们将使用以下功能:
pj_status_t pjsua_conf_connect (pjsua_conf_port_id source, pjsua_conf_port_id sink)
我们的接收器端口是设备扬声器端口,在这种情况下(大多数情况下)为0。
以下所有功能均添加到pjsua_app.c文件中。 在Objective-C类中使用它们的地方之前,您必须添加以下行:
pj_status_t play_sound_during_call(pj_str_t sound_file);
在这里播放声音的功能是:
pj_status_t play_sound_during_call(pj_str_t sound_file)
{
pjsua_player_id player_id;
pj_status_t status;
status = pjsua_player_create(&sound_file, 0, &player_id);
if (status != PJ_SUCCESS)
return status;
pjmedia_port *player_media_port;
status = pjsua_player_get_port(player_id, &player_media_port);
if (status != PJ_SUCCESS)
{
return status;
}
pj_pool_t *pool = pjsua_pool_create("my_eof_data", 512, 512);
struct pjsua_player_eof_data *eof_data = PJ_POOL_ZALLOC_T(pool, struct pjsua_player_eof_data);
eof_data->pool = pool;
eof_data->player_id = player_id;
pjmedia_wav_player_set_eof_cb(player_media_port, eof_data, &on_pjsua_wav_file_end_callback);
status = pjsua_conf_connect(pjsua_player_get_conf_port(player_id), 0);
if (status != PJ_SUCCESS)
{
return status;
}
return status;
}
以下是回调函数,可在您的wav文件读取(播放)结束时侦听:
struct pjsua_player_eof_data
{
pj_pool_t *pool;
pjsua_player_id player_id;
};
static PJ_DEF(pj_status_t) on_pjsua_wav_file_end_callback(pjmedia_port* media_port, void* args)
{
pj_status_t status;
struct pjsua_player_eof_data *eof_data = (struct pjsua_player_eof_data *)args;
status = pjsua_player_destroy(eof_data->player_id);
PJ_LOG(3,(THIS_FILE, "End of Wav File, media_port: %d", media_port));
if (status == PJ_SUCCESS)
{
return -1;// Here it is important to return a value other than PJ_SUCCESS
//Check link below
}
return PJ_SUCCESS;
}
pjmedia_wav_player_set_eof_cb回调函数应返回除PJ_SUCCESS以外的值的原因是因为此处pjmedia_wav_player_set_eof_cb的文档说:
请注意,如果应用程序破坏了回调中的文件端口,则它必须在此处返回non-PJ_SUCCESS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.