[Daily HarmonyOS Next Knowledge] TensorFlow Lite Compilation, Audio Encoding Threads, Immersive Status Bar, TextArea Byte Limit, etc.
1. How to compile TensorFlow Lite libraries?
Previously, the project used the TFLite inference engine for face recognition. How can TFLite models be reused on HarmonyOS?
TFLite natively supports GPU acceleration for Android and iOS, but HarmonyOS currently does not offer this. Instead, HarmonyOS provides its own inference engine that supports converting TFLite models to HarmonyOS inference engine models. It is recommended to use the official HarmonyOS solution:
- Model conversion document: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hiaifoundation-model-conversion-V5
- On-device inference document: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hiaifoundation-on-device-deployment-V5
2. Do audio encoding tasks require separate threading?
Encoding does not need an additional thread, as the system's codecs handle threading. Writing files should be handled by the app in a separate thread based on specific requirements.
3. Error when configuring audio encoder parameters
// Create encoder by codec name
OH_AVCapability *capability = OH_AVCodec_GetCapability(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, true);
const char *name = OH_AVCapability_GetName(capability);
audioEnc_ = OH_AudioCodec_CreateByName(name);
// Configure maximum input length (optional for per-frame audio data size)
uint32_t DEFAULT_MAX_INPUT_SIZE = inSamplerate * TIME_PER_FRAME * inChannel * sizeof(short); // AAC
OH_AVFormat *format = OH_AVFormat_Create();
// Write to format
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, inChannel);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, inSamplerate);
OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, outBitrate);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_FORMAT);
OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, CHANNEL_LAYOUT);
// OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, DEFAULT_MAX_INPUT_SIZE);
// Configure encoder
ret = OH_AudioCodec_Configure(audioEnc_, format);
if (ret != AV_ERR_OK) {
printLog("configure failed");
}
Parameters: Sampling rate 48000, channels 2, bitrate 128.
Error: ret = 3
(AV_ERR_INVALID_VAL: invalid argument).
Solution: The output bitrate setting is too low. Change it to 128000.
4. How to set up an immersive status bar?
Set the immersive status bar in the UIAbility's onWindowStageCreate
method:
async function enterImmersion(windowClass: window.Window) {
// Set window layout to full-screen immersive mode
await windowClass.setWindowLayoutFullScreen(true);
}
async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
await enterImmersion(windowClass);
}
5. How to set the maximum input byte limit for TextArea?
Calculate the current input byte length in the onChange
callback (refer to the example below) and compare it with the server-defined maximum. Block input if exceeded:
export function CalUtf8BytesLen(str: string): number {
let length = 0;
for (const char of str) {
let code = char.charCodeAt(0);
if (code <= 0x7f) {
length += 1; // 0xxxxxxx
} else if (code <= 0x7ff) {
length += 2; // 110xxxxx 10xxxxxx
} else if (code >= 0xd800 && code <= 0xdfff) {
// Surrogate pair
if (char.length === 2) {
code = (char.charCodeAt(0) - 0xd800) * 0x400 + char.charCodeAt(1) - 0xdc00 + 0x10000;
length += 4; // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
} else {
throw new Error('Invalid surrogate pair');
}
} else {
length += 3; // 1110xxxx 10xxxxxx 10xxxxxx
}
}
return length;
}
Top comments (0)