Init
APIInit
API once.
The billing will not start after initialization. Receiving or sending a voice message in speech-to-text service is counted as a voice message DAU.QAVError.OK
will be returned with the value being 0
.Poll
API should be called periodically for GME to trigger event callbacks.Class | Description |
ITMGContext | Core APIs |
ITMGPTT | Voice messaging and speech-to-text APIs |
Init
API before you can use the voice chat and speech-to-text services.Init
API before calling any APIs of GME.API | Description |
Init | Initializes GME. |
Poll | Triggers an event callback. |
Pause | Pauses the system. |
Resume | Resumes the system. |
Uninit | Uninitializes GME. |
UnInit
to uninitialize the SDK. No fee is incurred for calling Init API.ITMGContext
object first.import com.tencent.TMG.ITMGContext;ITMGContext.getInstance(this);
Delegate
method to send callback notifications to the application. Register the callback function to the SDK for receiving callback messages.static public abstract class ITMGDelegate {public void OnEvent(ITMG_MAIN_EVENT_TYPE type, Intent data){}}
Parameter | Type | Description |
type | ITMGContext.ITMG_MAIN_EVENT_TYPE | Event type in the callback response |
data | Intent message type | Callback message, i.e., event data |
private ITMGContext.ITMGDelegate itmgDelegate = null;itmgDelegate = new ITMGContext.ITMGDelegate() {@Overridepublic void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if (ITMGContext.ITMG_MAIN_EVENT_TYPE.ITMG_MAIN_EVENT_TYPE_ENTER_ROOM == type){// Analyze the returned dataint nErrCode = data.getIntExtra("result" , -1);String strErrMsg = data.getStringExtra("error_info");}}}
public abstract int SetTMGDelegate(ITMGDelegate delegate);
Parameter | Type | Description |
delegate | ITMGDelegate | SDK callback function |
ITMGContext.GetInstance(this).SetTMGDelegate(itmgDelegate);
sdkAppID
parameter, see Activating Services.openID
uniquely identifies a user with the rules stipulated by you. It must be unique in the application and can only be in Int64 type.public abstract int Init(String sdkAppId, String openId);
Parameter | Type | Description |
sdkAppId | String | |
openId | String | OpenId can only be in Int64 type, which is passed in after being converted to a string. |
Returned Value | Description |
QAVError.OK= 0 | The SDK was initialized successfully. |
AV_ERR_SDK_NOT_FULL_UPDATE=7015 | Checks whether the SDK file is complete. We recommend that you delete it and then import it again. |
AV_ERR_SDK_NOT_FULL_UPDATE
is only a reminder but will not cause an initialization failure.String sdkAppID = "14000*****";String openID = "100";int ret = ITMGContext.GetInstance(this).Init(sdkAppId, openId);if(ret != 0){Log.e(TAG,"SDK initialization failed");}
Poll
API in update
. The Poll
API should be called periodically for GME to trigger event callbacks; otherwise, the entire SDK service will run exceptionally.
You can refer to the EnginePollHelper.java
file in the demo.Poll
API must be called periodically and in the main thread to avoid abnormal API callbacks.public abstract int Poll();
private Handler mhandler = new Handler();private Runnable mRunnable = new Runnable() {@Overridepublic void run() {if (s_pollEnabled) {if (ITMGContext.GetInstance(null) != null)ITMGContext.GetInstance(null).Poll();}mhandler.postDelayed(mRunnable, 33);}};
Pause
event occurs in the system, the engine should also be notified for pause.
If you need to pause the audio when switching to the background, you can call the Pause
API in the listening code used to switch to the background, and call the Resume
API in the listening event used to resume the foreground.public abstract int Pause();
Resume
event occurs in the system, the engine should also be notified for resumption. The Resume
API only supports resuming voice chat.public abstract int Resume();
public abstract int Uninit();
API | Description |
ApplyPTTAuthbuffer | Initializes authentication. |
SetMaxMessageLength | Specifies the maximum duration of a voice message. |
StartRecording | Starts recording. |
StartRecordingWithStreamingRecognition | Starts streaming recording. |
PauseRecording | Pauses recording. |
ResumeRecording | Resumes recording. |
StopRecording | This API is used to stop audio recording. |
CancelRecording | Cancels recording. |
GetMicLevel | Gets the real-time mic volume level. |
SetMicVolume | Sets the recording volume level. |
GetMicVolume | Gets the recording volume level. |
GetSpeakerLevel | Gets the real-time speaker volume level. |
SetSpeakerVolume | Sets the playback volume level. |
GetSpeakerVolume | Gets the playback volume level. |
UploadRecordedFile | Uploads an audio file. |
DownloadRecordedFile | Downloads an audio file. |
PlayRecordedFile | Plays back an audio file. |
StopPlayFile | Stops playing back an audio file. |
GetFileSize | Gets the audio file size. |
GetVoiceFileDuration | Gets the audio file duration. |
SpeechToText | Converts speech to text. |
SetMaxMessageLength
API to set it after initialization.Init
API before you can use the voice chat and voice message services.AuthBuffer
for encryption and authentication of relevant features. For release in the production environment, use the backend deployment key as detailed in Authentication Key. To get the authentication key for voice messaging and speech-to-text services, the room ID parameter must be set to null.AuthBuffer public native byte[] genAuthBuffer(int sdkAppId, String roomId, String openId, String key)
Parameter | Type | Description |
appId | int | AppId from the Tencent Cloud console |
roomId | string | Room ID, which must be set to null. |
openId | string | User ID, which is the same as openId during initialization. |
key | string |
import com.tencent.av.sig.AuthBuffer;// Header filebyte[] authBuffer = AuthBuffer.getInstance().genAuthBuffer(Integer.parseInt(sdkAppId), "0",openId, key);
authBuffer
, see genAuthBuffer
(the voice chat authentication information API).public abstract int ApplyPTTAuthbuffer(byte[] authBuffer);
Parameter | Type | Description |
authBuffer | String | Authentication |
byte[] authBuffer = AuthBuffer.getInstance().genAuthBuffer(Integer.parseInt(sdkAppId), "0", openId, key);ITMGContext.GetInstance(this).GetPTT().ApplyPTTAuthbuffer(authBuffer);
StopRecording
. The callback will be returned after the recording is stopped.public abstract int StartRecordingWithStreamingRecognition (String filePath);public abstract int StartRecordingWithStreamingRecognition (String filePath,String language,String translatelanguage);public abstract int StopRecording();
Parameter | Type | Description |
filePath | String | Path of the stored audio file |
speechLanguage | String | The language in which the audio file is to be converted into text. For parameters, see Language Parameter Reference List. |
translateLanguage | String | The language into which the audio file is to be translated into text. For parameters, see Language Parameter Reference List. (This parameter is currently unavailable. Enter the same value as that of speechLanguage .) |
String temple = getActivity().getExternalFilesDir(null).getAbsolutePath() + "/test_"+(index++)+".ptt";ITMGContext.GetInstance(getActivity()).GetPTT().StartRecordingWithStreamingRecognition(temple,"cmn-Hans-CN","cmn-Hans-CN");
onEvent
. Event messages are divided into:ITMG_MAIN_EVNET_TYPE_PTT_STREAMINGRECOGNITION_COMPLETE
returns text after the recording is stopped and the recognition is completed, which is equivalent to returning the recognized text after a paragraph of speech.ITMG_MAIN_EVNET_TYPE_PTT_STREAMINGRECOGNITION_IS_RUNNING
returns the recognized text in real time during the recording, which is equivalent to returning the recognized text while speaking.OnEvent function
based on the actual needs. The delivered event message contains the following four parameters.Parameter | Description |
result | Return code indicating whether streaming speech-to-text conversion is successful |
text | Text converted from speech |
file_path | Local path of the stored recording file |
file_id | Backend URL address of the recording file, which will be retained for 90 days. This field is fixed at http://gme-v2-. |
Error Code | Description | Solution |
32775 | Streaming speech-to-text conversion failed, but recording succeeded. | Call the UploadRecordedFile API to upload the recording file and then call the SpeechToText API to perform speech-to-text conversion. |
32777 | Streaming speech-to-text conversion failed, but recording and upload succeeded. | The message returned contains a backend URL after successful upload. Call the SpeechToText API to perform speech-to-text conversion. |
32786 | Streaming speech-to-text conversion failed. | During streaming recording, wait for the execution result of the streaming recording API to return. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if (ITMGContext.ITMG_MAIN_EVENT_TYPE.ITMG_MAIN_EVNET_TYPE_PTT_STREAMINGRECOGNITION_COMPLETE == type){/Callback for streaming voice message and speech-to-textHandler mainHander = new Handler(Looper.getMainLooper());mainHander.post(new Runnable() {@Overridepublic void run() {if (nErrCode ==0) {String textString = templeData.getStringExtra("text");EditText _editText = (EditText) root.findViewById(R.id.edit_audio_to_text);_editText.setText(textString);recordfilePath = templeData.getStringExtra("file_path");mEditTextfiletoupload.setText(recordfilePath);donwLoadUrlPath = templeData.getStringExtra("file_id");mEditTextDownloadurl.setText(donwLoadUrlPath);Log.e(TARGET, "STREAMINGRECOGNITION" + "nErrCode=" + nErrCode + ", content=" + textString+"recordfilePath"+recordfilePath+"donwLoadUrlPath"+donwLoadUrlPath);}else{Toast.makeText(getActivity(), String.format("Streaming speech-to-text conversion failed. Error code: %d",nErrCode), Toast.LENGTH_SHORT).show();Log.e(TARGET, "Streaming speech-to-text conversion failed. Error code"+nErrCode);}}});}}
public abstract int SetMaxMessageLength(int msTime);
Parameter | Type | Description |
msTime | int | Audio duration in ms. Value range: 1000 < msTime < 58000. |
ITMGContext.GetInstance(this).GetPTT().SetMaxMessageLength(msTime);
StopRecording
.public abstract int StartRecording(String filePath);
Parameter | Type | Description |
filePath | String | Path of the stored audio file |
ITMGContext.GetInstance(this).GetPTT().StartRecording(filePath);
public abstract int StopRecording();
ITMGContext.GetInstance(this).GetPTT().StopRecording();
StopRecording
. The callback for recording start will be returned after the recording is stopped.OnEvent
will be called after recording is started. The event message ITMG_MAIN_EVNET_TYPE_PTT_RECORD_COMPLETE
will be returned, which will be identified in the OnEvent
function.
The passed parameter includes result
and file_path
.Error Code | Reasons | Suggested Solution |
4097 | Empty parameters. | Check whether the API parameters in the code are correct. |
4098 | An initialization error occurred. | Check whether the device is being used, whether the permissions are normal, and whether the initialization is normal. |
4099 | Recording is in progress. | Make sure that the SDK recording feature is used at the right time. |
4100 | No audio data is captured. | Check whether the mic is working properly. |
4101 | An error occurred while accessing the file during recording. | Ensure the existence of the file and the validity of the file path. |
4102 | The mic is not authorized. | The mic permission is required for using the SDK. To add the permission, see the SDK project configuration document for the corresponding engine or platform. |
4103 | The recording duration is too short. | The recording duration should be in ms and longer than 1,000 ms. |
4104 | No recording operation is started. | Check whether the recording starting API has been called. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if (ITMGContext.ITMG_MAIN_EVENT_TYPE.ITMG_MAIN_EVNET_TYPE_PTT_RECORD_COMPLETE == type){// Callback for recording startif (nErrCode ==0){recordfilePath = templeData.getStringExtra("file_path");mEditTextfiletoupload.setText(recordfilePath);}}}
ResumeRecording
API.public abstract int PauseRecording();
ITMGContext.GetInstance(this).GetPTT().PauseRecording();
public abstract int ResumeRecording();
ITMGContext.GetInstance(this).GetPTT().ResumeRecording();
public abstract int CancelRecording();
ITMGContext.GetInstance(this).GetPTT().CancelRecording();
ITMGPTT.java
.public abstract int GetMicLevel();
ITMGContext.GetInstance(this).GetPTT().GetMicLevel();
ITMGPTT.java
.public abstract int SetMicVolume(int volume);
ITMGContext.GetInstance(this).GetPTT().SetMicVolume(100);
ITMGPTT.java
.public abstract int GetMicVolume();
ITMGContext.GetInstance(this).GetPTT().GetMicVolume();
ITMGPTT.java
.public abstract int GetSpeakerLevel();
ITMGContext.GetInstance(this).GetPTT().GetSpeakerLevel();
ITMGPTT.java
.public abstract int SetSpeakerVolume(int volume);
ITMGContext.GetInstance(this).GetPTT().SetSpeakerVolume(100);
ITMGPTT.java
.public abstract int GetSpeakerVolume();
ITMGContext.GetInstance(this).GetPTT().GetSpeakerVolume();
public abstract int PlayRecordedFile(String filePath);public abstract int PlayRecordedFile(String filePath,int voicetype);
Parameter | Type | Description |
downloadFilePath | String | Local audio file path |
voicetype | int |
Error Code | Reasons | Suggested Solution |
20485 | Playback is not started. | Ensure the existence of the file and the validity of the file path. |
ITMGContext.GetInstance(this).GetPTT().PlayRecordedFile(downloadFilePath);
ITMG_MAIN_EVNET_TYPE_PTT_PLAY_COMPLETE
will be returned, which will be identified in the OnEvent
function.
The passed parameter includes result
and file_path
.Error Code | Reasons | Suggested Solution |
20481 | An initialization error occurred. | Check whether the device is being used, whether the permissions are normal, and whether the initialization is normal. |
20482 | During playback, the client tried to interrupt and play back the next one but failed (which should succeed normally). | Check whether the code logic is correct. |
20483 | Empty parameters. | Check whether the API parameters in the code are correct. |
20484 | An internal error occurred. | An error occurred while initializing the player. This error code is generally caused by failure in decoding, and the error should be located with the aid of logs. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if(ITMGContext.ITMG_MAIN_EVNET_TYPE_PTT_PLAY_COMPLETE== type){// Callback for audio playback}}
public abstract int StopPlayFile();
ITMGContext.GetInstance(this).GetPTT().StopPlayFile();
public abstract int GetFileSize(String filePath);
Parameter | Type | Description |
filePath | String | Path of the audio file, which is a local path. |
ITMGContext.GetInstance(this).GetPTT().GetFileSize(path);
public abstract int GetVoiceFileDuration(String filePath);
Parameter | Type | Description |
filePath | String | Path of the audio file, which is a local path. |
ITMGContext.GetInstance(this).GetPTT().GetVoiceFileDuration(path);
public abstract int UploadRecordedFile(String filePath);
Parameter | Type | Description |
filePath | String | Path of the uploaded audio file, which is a local path. |
ITMGContext.GetInstance(this).GetPTT().UploadRecordedFile(filePath);
ITMG_MAIN_EVNET_TYPE_PTT_UPLOAD_COMPLETE
will be returned, which will be identified in the OnEvent
function.
The passed parameters include result
, file_path
, and file_id
.Error Code | Reasons | Suggested Solution |
8193 | An error occurred while accessing the file during upload. | Ensure the existence of the file and the validity of the file path. |
8194 | Signature verification failed. | Check whether the authentication key is correct and whether the voice messaging and speech-to-text feature is initialized. |
8195 | Network error | Check whether the device can access the internet. |
8196 | The network failed while getting the upload parameters. | Check whether the authentication is correct and whether the device network can normally access the internet. |
8197 | The packet returned during the process of getting the upload parameters is empty. | Check whether the authentication is correct and whether the device network can normally access the internet. |
8198 | Failed to decode the packet returned during the process of getting the upload parameters. | Check whether the authentication is correct and whether the device network can normally access the internet. |
8200 | appinfo is not set. | Check whether the apply API is called or whether the input parameter is not specified or null. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if(ITMGContext.ITMG_MAIN_EVENT_TYPE.ITMG_MAIN_EVNET_TYPE_PTT_UPLOAD_COMPLETE== type){// Callback for audio file upload completion}}
public abstract int DownloadRecordedFile(String fileID, String filePath);
Parameter | Type | Description |
fileID | String | File URL |
downloadFilePath | String | Local path of the saved file |
ITMGContext.GetInstance(this).GetPTT().DownloadRecordedFile(url,path);
ITMG_MAIN_EVNET_TYPE_PTT_DOWNLOAD_COMPLETE
will be returned, which will be identified in the OnEvent
function. The passed parameters include result
、file_path
and file_id
.Error Code | Reasons | Suggested Solution |
12289 | An error occurred while accessing the file during download. | Check whether the file path is valid. |
12290 | Signature verification failed. | Check whether the authentication key is correct and whether the voice messaging and speech-to-text feature is initialized. |
12291 | A network storage system exception occurred. | The server failed to get the audio file. Check whether the API parameter fileid is correct, whether the network is normal, and whether the file exists in COS. |
12292 | A server file system error occurred. | Check whether the device can access the internet and whether the file exists on the server. |
12293 | The HTTP network failed while getting the download parameters. | Check whether the device can access the internet. |
12294 | The packet returned during the process of getting the download parameters is empty. | Check whether the device can access the internet. |
12295 | Failed to decode the packet returned during the process of getting the download parameters. | Check whether the device can access the internet. |
12297 | appinfo is not set. | Check whether the authentication key is correct and whether the voice messaging and speech-to-text feature is initialized. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if(ITMGContext.ITMG_MAIN_EVNET_TYPE_PTT_DOWNLOAD_COMPLETE== type){//Download succeeded}}
public abstract int SpeechToText(String fileID);
Parameter | Type | Description |
fileID | String | Audio file URL |
ITMGContext.GetInstance(this).GetPTT().SpeechToText(fileID);
public abstract int SpeechToText(String fileID, String speechLanguage,String translatelanguage);
Parameter | Type | Description |
fileID | String | URL of the audio file, which will be retained on the server for 90 days. |
speechLanguage | String | The language in which the audio file is to be converted into text. For parameters, see Language Parameter Reference List. |
translatelanguage | String | The language into which the audio file is to be translated into text. For parameters, see Language Parameter Reference List. (This parameter is currently unavailable. Enter the same value as that of speechLanguage .) |
ITMGContext.GetInstance(this).GetPTT().SpeechToText(fileID,"cmn-Hans-CN","cmn-Hans-CN");
OnEvent
function.
The passed parameters include result
, file_path
and text
(recognized text).Error Code | Reasons | Suggested Solution |
32769 | An internal error occurred. | Analyze logs, get the actual error code returned from the backend to the client, and ask backend personnel for assistance. |
32770 | Network connection failed. | Check whether the device can access the internet. |
32772 | Failed to decode the returned packet. | Analyze logs, get the actual error code returned from the backend to the client, and ask backend personnel for assistance. |
32774 | appinfo is not set. | Check whether the authentication key is correct and whether the voice messaging and speech-to-text feature is initialized. |
32776 | authbuffer check failed. | Check whether authbuffer is correct. |
32784 | The speech-to-text conversion parameter is incorrect. | Check whether the API parameter fileid in the code is empty. |
32785 | A speech-to-text translation error occurred. | An error occurred in the voice messaging and speech-to-text feature on the backend. Analyze logs, get the actual error code returned from the backend to the client, and ask backend personnel for assistance. |
public void OnEvent(ITMGContext.ITMG_MAIN_EVENT_TYPE type, Intent data) {if(ITMGContext.ITMG_MAIN_EVNET_TYPE_PTT_SPEECH2TEXT_COMPLETE == type){//Recognized audio file successfully}}
public abstract String GetSDKVersion();
ITMGContext.GetInstance(this).GetSDKVersion();
public abstract ITMG_RECORD_PERMISSION CheckMicPermission();
Parameter | Value | Description |
ITMG_PERMISSION_GRANTED | 0 | The mic permission is granted. |
ITMG_PERMISSION_Denied | 1 | Mic disabled. |
ITMG_PERMISSION_NotDetermined | 2 | No authorization box has been popped up to request the permission. |
ITMG_PERMISSION_ERROR | 3 | An error occurred while calling the API. |
ITMGContext.GetInstance(this).CheckMicPermission();
public abstract int SetLogLevel(int levelWrite, int levelPrint);
Parameter | Type | Description |
levelWrite | ITMG_LOG_LEVEL | Sets the level of logs to be written. TMG_LOG_LEVEL_NONE indicates not to log. Default value: TMG_LOG_LEVEL_INFO . |
levelPrint | ITMG_LOG_LEVEL | Sets the level of logs to be printed. TMG_LOG_LEVEL_NONE indicates not to print. Default value: TMG_LOG_LEVEL_ERROR . |
ITMG_LOG_LEVEL | Description |
TMG_LOG_LEVEL_NONE | Does not print logs |
TMG_LOG_LEVEL_ERROR | Prints error logs (default) |
TMG_LOG_LEVEL_INFO | Prints info logs |
TMG_LOG_LEVEL_DEBUG | Prints debug logs |
TMG_LOG_LEVEL_VERBOSE | Prints verbose logs |
ITMGContext.GetInstance(this).SetLogLevel(TMG_LOG_LEVEL_INFO,TMG_LOG_LEVEL_INFO);
public abstract int SetLogPath(String logDir);
Parameter | Type | Description |
logDir | String | Path |
ITMGContext.GetInstance(this).SetLogPath(path);
Message | Description |
ITMG_MAIN_EVNET_TYPE_PTT_RECORD_COMPLETE | PTT recording is completed. |
ITMG_MAIN_EVNET_TYPE_PTT_UPLOAD_COMPLETE | PTT upload is completed. |
ITMG_MAIN_EVNET_TYPE_PTT_DOWNLOAD_COMPLETE | PTT download is completed. |
ITMG_MAIN_EVNET_TYPE_PTT_PLAY_COMPLETE | PTT playback is completed. |
ITMG_MAIN_EVNET_TYPE_PTT_SPEECH2TEXT_COMPLETE | Speech-to-text conversion is completed. |
Message | Data | Example |
ITMG_MAIN_EVNET_TYPE_PTT_RECORD_COMPLETE | result; file_path | {"file_path":"","result":0} |
ITMG_MAIN_EVNET_TYPE_PTT_UPLOAD_COMPLETE | result; file_path;file_id | {"file_id":"","file_path":"","result":0} |
ITMG_MAIN_EVNET_TYPE_PTT_DOWNLOAD_COMPLETE | result; file_path;file_id | {"file_id":"","file_path":"","result":0} |
ITMG_MAIN_EVNET_TYPE_PTT_PLAY_COMPLETE | result; file_path | {"file_path":"","result":0} |
ITMG_MAIN_EVNET_TYPE_PTT_SPEECH2TEXT_COMPLETE | result; text;file_id | {"file_id":"","text":"","result":0} |
ITMG_MAIN_EVNET_TYPE_PTT_STREAMINGRECOGNITION_COMPLETE | result; file_path; text;file_id | {"file_id":"","file_path":","text":"","result":0} |
この記事はお役に立ちましたか?