Qwen-Audio-Realtime iOS SDK
Use Qwen-Audio-Realtime real-time voice dialogue iOS SDK to enable real-time audio input and speech or text output.
For connection and authentication, see WebSocket access. For event fields and JSON examples, see Client events and Server events.
User guide: For the model introduction and selection advice, see Real-time voice conversation.
Prerequisites
- Obtain and configure an API key. Do not hardcode a long-lived API key in a mobile app. We recommend generating a temporary API key on your application server and sending it to the client.
Install the SDK
- Download the latest SDK package.
- Extract the ZIP package and add the nuisdk.xcframework in it to the project.
- In Build Phases → Link Binary With Libraries, add nuisdk.xcframework.
- In General → Frameworks, Libraries, and Embedded Content, set nuisdk.xcframework to Embed & Sign.
Quick start
Open the sample project in Xcode. The sample code is located in DashQwenAudioChatViewController.m. Replace the API Key to try the feature.
Call procedure
- Initialize the SDK
- Set parameters based on your business needs: use the nui_initialize method to set the connection and control parameters; use the nui_set_params method to set the voice conversation parameters.
- Call nui_dialog_start to start the conversation.
- In the onNuiAudioStateChanged callback, start the recording device based on the audio state.
- Continuously provide recording data in the onNuiNeedAudioData callback, or continuously push recording data through nui_update_audio_data.
- Continuously obtain the voice data returned by the model in the onNuiAssistEventCallback callback.
- Listen for events and obtain event information in the onNuiEventCallback callback.
- Call nui_dialog_cancel to stop the conversation, and confirm that the conversation has ended by listening for the EVENT_TRANSCRIBER_COMPLETE event.
- When the conversation feature is no longer used, call the nui_release method to release the SDK resources.
Request parameters
Connection and control options
Configure by passing a JSON string in the parameters parameter of the nui_initialize method.
Parameter example: The following is a JSON string example, which does not list all parameters. Add them based on your actual needs when coding:
The example uses qwen-audio-3.1-realtime-plus. To use another model, update both model in the URL and nls_config.model in Voice conversation parameters.
{
"url": "wss://dashscope.aliyuncs.com/api-ws/v1/realtime?model=qwen-audio-3.1-realtime-plus",
"apikey": "st-****",
"device_id": "my_device_id",
"service_mode": "1"
}
Parameter descriptions
| Parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Service endpoint:
{WorkspaceId} with your actual Workspace ID. |
|
| Yes | API Key. |
|
| Yes | Runtime mode. Fixed to |
|
| Yes | A unique string that identifies the end user, which can be set to an in-app user ID or a client-generated unique device identifier. This ID is mainly used for log tracing and troubleshooting. |
|
| No | Whether to enable the active audio data push mode. Default: "false". |
|
| No | The storage path of on-device resource files. This parameter must be set when |
|
| No | The storage path of log files. This parameter takes effect only when |
|
| No | Whether to save audio files for debugging. The audio files are saved under
save_log is set to true when calling the nui_initialize method. In addition, debug_path must also be set. |
|
| No | The maximum number of bytes of the log file. This parameter takes effect only when |
|
| No | Controls the filter level of the log content sent out through the log callback (onNuiLogTrackCallback).
log_track_level and level (set through the nui_initialize method) jointly determine the logs that are finally called back. A log is called back only when its level value is greater than or equal to both log_track_level and level. For example, if log_track_level is set to 2 (INFO) and level is set to 3 (WARNING), only logs at the WARNING level and above (value >= 3) are called back. |
Voice conversation parameters
Configure by passing a JSON string in the params parameter of the nui_set_params method.
Parameter example: The following is a JSON string example, which does not list all parameters. Add them based on your actual needs when coding:
{
"service_type": 4,
"nls_config": {
"model": "qwen-audio-3.1-realtime-plus",
"sr_format": "pcm"
}
}
Parameter descriptions
| Top-level parameter | Type | Required | Description |
|---|---|---|---|
|
| Yes | Voice service type. Fixed to |
|
| Yes | The core voice conversation configuration object, containing key parameters such as model selection and conversation effect control. |
|
| Yes | Specifies the model name. Supports the qwen-audio-3.1-realtime-plus, qwen-audio-3.1-realtime-flash, qwen-audio-3.0-realtime-plus, and qwen-audio-3.0-realtime-flash model series. |
|
| Yes | Input audio format. Currently, only |
|
| No | A string in array format. Model output modality settings. Valid values:
|
nls_config.voice |
| No | TTS voice name. The default value is
|
nls_config.enable_speech_emotion |
| No | Whether to enable the emotion enhancement feature. When enabled, the emotional changes of the response voice are more noticeable. Default: |
nls_config.instructions |
| No | System instructions, used to set the model's role identity, response style, and behavioral preferences. Takes effect for the entire session. |
nls_config.max_history_turns | int | No | The maximum number of historical QA turns allowed in a single request. The value range is 1-50, and the default value is 20. |
nls_config.tools |
| No | A string in array format. A list of Function Calling tool definitions. After configuration, the model can autonomously decide whether to call a tool based on the user input. |
nls_config.turn_detection |
| No | The following turn detection field descriptions apply to A string in JSON object format. Turn detection configuration. If it is not set, the mode switches to push-to-talk (manually committing audio and triggering inference); otherwise, duplex conversation mode is enabled. |
nls_config.turn_detection.type |
| No | VAD type. Valid values:
|
nls_config.turn_detection.threshold | float | No | VAD sensitivity. Applies to |
nls_config.turn_detection.silence_duration_ms |
| No | The minimum silence duration after the speech ends (in milliseconds), effective only in server_vad mode (invalid in smart_turn and smart_turn_v2 modes). When the silence reaches this duration, the model response is triggered. The lower the value, the faster the response, but a brief pause may also falsely trigger it. |
nls_config.turn_detection.voiceprint_audio_urls |
| No | A string in array format. Effective in smart_turn and smart_turn_v2 modes. A list of publicly accessible URLs of the target user's prerecorded audio, used for speaker enhancement. The model uses these audio clips to lock onto the target speaker in the duplex conversation, ignoring the voices of others and background noise. Up to 5 URLs are supported. Audio format requirements: 16kHz PCM or WAV. |
API reference
NeoNui
nui_initialize
Initializes the voice conversation SDK instance. The SDK uses singleton mode. Do not initialize it again before calling nui_release.
- (NuiResultCode) nui_initialize:(const char *)parameters
logLevel:(NuiSdkLogLevel)level
saveLog:(BOOL)save_log;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| A JSON string containing authentication, connection, and debugging parameters. See Connection and control parameters. |
|
| Controls the print level of the SDK's own logs. |
| BOOL | Whether to save local logs. If it is |
nui_set_params
Sets the voice conversation parameters in JSON format. Call it before nui_dialog_start.
- (NuiResultCode) nui_set_params:(const char *)params;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
|
nui_dialog_start
Starts the conversation.
Method signature- (NuiResultCode) nui_dialog_start:(NuiVadMode)vad_mode
dialogParam:(const char *)dialog_params;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| VAD mode. Fixed to |
|
| If the |
nui_dialog_cancel
Ends the conversation or immediately cancels the current interaction.
Method signature- (NuiResultCode) nui_dialog_cancel:(BOOL)force;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| Whether to forcibly end the task and ignore the final result.
|
nui_dialog_action
Sends conversation action commands during the interaction, used to update runtime behaviors such as the conversation context.
Method signature- (NuiResultCode) nui_dialog_action:(const char *)params;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| A JSON string used to update runtime behaviors such as the conversation context. |
|
| Fixed to |
|
| The specific runtime command. Currently,
|
|
| When |
|
| Event type, which must be set when
|
|
| Must be set when |
|
| Can be set when |
context.item parameters:
| Parameter | Type | Description |
|---|---|---|
id |
| Optional. The unique identifier of the conversation item. If it is not passed, it is generated automatically by the server. If the specified ID already exists in the conversation, an error is returned. |
type |
| Required. The conversation item type. Valid values:
|
role |
| Required for the |
content | array | Required for the
output_text: assistant text output, with required field text. |
call_id |
| ( |
name |
| ( |
arguments |
|
|
output |
| ( |
context.response parameters:
| Parameter | Type | Description |
|---|---|---|
modalities |
| A string in array format. Model output modality settings. Valid values:
|
voice | string | Overrides the TTS voice for this round. |
Example:
{
"type": "action",
"command": "function_call",
"context": {
"item": {
"call_id": "call_xxxx",
"output": "{\"city\":\"杭州\",\"condition\":\"晴\",\"temperature\":18}",
"type": "function_call_output"
},
"type": "conversation.item.create"
}
}
{
"type": "action",
"command": "function_call",
"context": {
"response": {
"modalities": [
"text",
"audio"
]
},
"type": "response.create"
}
}
nui_update_audio_data
When audio_update_manually is set to "true", recording data is no longer filled in through onNuiNeedAudioData; instead, it is actively pushed through this method.
- (NuiResultCode) nui_update_audio_data:(const char *)data
Len:(int)length
FirstPack:(BOOL)first_pack;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| The pushed audio data. |
|
| The number of bytes of the pushed audio data. |
|
| You do not need to pay attention to this parameter. |
nui_push_reference_data
When audio_update_manually is set to "true" and the on-device AEC echo cancellation capability is enabled, you need to push the audio data played by the player as the reference signal through this method.
- (NuiResultCode) nui_push_reference_data:(const char *)data
Len:(int)length
FirstPack:(BOOL)first_pack;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| The pushed audio data. |
|
| The number of bytes of the pushed audio data. |
|
| You do not need to pay attention to this parameter. |
nui_release
Releases all internal SDK resources and forcibly terminates all ongoing tasks. After this method is called, the SDK instance becomes unavailable. To use it again, you must call nui_initialize again for initialization.
- (NuiResultCode) nui_release;
nui_get_version
Obtains the current SDK version information. This method returns a value only when it is called after nui_initialize.
- (const char*) nui_get_version;
Return value description
The current SDK version information.
nui_get_all_response
Obtains the complete information of the current event callback.
Method signature- (const char*) nui_get_all_response;
Return value description
The complete event information in JSON string format.
NeoNuiSdkDelegate: callback listeners
onNuiEventCallback: listen for event information
Method signature- (void) onNuiEventCallback:(NuiCallbackEvent)nuiEvent
dialog:(long)dialog
kwsResult:(const char *)wuw
asrResult:(const char *)asr_result
ifFinish:(BOOL)finish
retCode:(int)code;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| Callback event. |
|
| Session code. You do not need to pay attention to this parameter. |
|
| Voice wake-up function. You do not need to pay attention to this parameter. |
|
| Speech recognition result. |
|
| Flag indicating whether this round of recognition has ended. |
|
| Valid only when the |
onNuiAudioStateChanged: listen for the audio state
The SDK uses this callback to notify you when recording should start or stop.
Method signature- (void) onNuiAudioStateChanged:(NuiAudioState)state;
NuiAudioState state descriptions
| Parameter | Description |
|---|---|
| The interaction has started. You can open the recording device to record. |
| The interaction has stopped. You can stop recording. |
| The SDK instance has been released. You can completely close the recording device. |
onNuiNeedAudioData: fill in the audio data to be processed
After the conversation starts, this callback is triggered continuously, and you must provide the audio data to be processed in it. You can ignore this callback when the audio_update_manually parameter is set to "true".
Method signature- (int) onNuiNeedAudioData:(char *)audioData length:(int)len;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| The audio data to fill in. |
|
| The number of bytes of the filled audio data. |
onNuiAssistEventCallback: auxiliary data and information results
This callback is used to receive auxiliary events and related data from inside the SDK.
Method signature- (void) onNuiAssistEventCallback:(NuiCallbackEvent)nuiEvent
info:(char*)info
infoLen:(int)info_len
buffer:(char*)buffer
len:(int)len;
Parameter descriptions
| Parameter | Type | Description |
|---|---|---|
|
| Callback event. |
|
| You do not need to pay attention to this parameter. |
|
| You do not need to pay attention to this parameter. |
|
| Auxiliary data, such as the TTS audio returned by the model. |
|
| The number of bytes of the auxiliary data. |
onNuiLogTrackCallback: listen for tracking logs
This callback is used to receive detailed internal logs of the SDK, making it easier to locate and debug issues.
- (void) onNuiLogTrackCallback:(NuiSdkLogLevel)level
logMessage:(const char *)log;
NuiCallbackEvent: event types
| Event | Description |
|---|---|
EVENT_TRANSCRIBER_STARTED | The task started successfully. |
EVENT_VAD_START | Triggered immediately after the task starts. It does not mean that the start of speech has been detected. |
EVENT_VAD_END | The end of speech was detected. |
EVENT_ASR_PARTIAL_RESULT | An intermediate speech recognition result. |
EVENT_ASR_ERROR | An error occurred during the voice conversation. |
EVENT_MIC_ERROR | Triggered because no audio data has been received for 2 consecutive seconds. |
EVENT_SENTENCE_END | The end of a sentence was detected, and a complete recognition result is returned at this point. |
EVENT_TRANSCRIBER_COMPLETE | The voice conversation has ended. |
EVENT_AUDIO_TRANSCRIPTION | An incremental text subtitle event in audio mode, returning subtitle fragments in streaming mode. |
EVENT_AUDIO_TRANSCRIPTION_COMPLETED | The subtitle output completion event in audio mode. |
EVENT_OTHER_RESULT | Other uncategorized event information, such as the return results of function_call. |
EVENT_ASR_TTS_START | The model starts returning TTS data. |
EVENT_ASR_TTS_DATA | The TTS data returned by the model. |
EVENT_ASR_TTS_COMPLETE | The model has finished returning TTS data. |
EVENT_AEC_DATA | The audio data after AEC echo cancellation. |