[Learn HarmonyOS Next Knowledge Every Day] Network, Performance, and Player-related
1. How to handle the error code request.agent.State.FAILED when there is an exception in request file download?
When using the request.agent.create method to download files, the error code in the callback Progress is request.agent.State.FAILED. Currently, other devices can download normally, but only one device fails to download and reports an error.
A State of FAILED indicates that the task has failed. You can consider restarting the task or removing the file and starting a new task.
2. How to determine whether the current network can access the internet?
You can use the capabilities of @ohos.net.connection
to determine whether the current network can access the internet each time the network connection changes, and then store the judgment result in AppStorage. When you need to determine whether you can access the internet, you can directly obtain the result from AppStorage. The sample code is as follows:
import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
export class NetJudge {
public static conn: connection.NetConnection | undefined = undefined;
private static JUDGE_NET_TAG: string = 'NetJudge.currNet.isUseful';
public static netFlag: string = 'false'
private static init() {
NetJudge.conn = connection.createNetConnection();
NetJudge.conn.register(() => {
console.info('connection register success')
})
NetJudge.conn.on('netAvailable', (data) => {
console.info('NetJudge netAvailable ')
AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet())
})
NetJudge.conn.on('netUnavailable', () => {
console.info('NetJudge netUnavailable ')
AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet())
})
NetJudge.conn.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => {
AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet())
})
// Subscribe to events of changes in network connection information. This event notification can only be received after calling register
NetJudge.conn.on('netConnectionPropertiesChange', (data: connection.NetConnectionPropertyInfo) => {
AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet())
});
NetJudge.conn.on('netLost', () => {
AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet())
})
}
public static regist() {
if (NetJudge.conn == undefined) {
NetJudge.init()
}
}
public static judgeHasNet(): boolean {
try { // Get the current network connection
let netHandle = connection.getDefaultNetSync()
// 0-100 are reserved connections of the system
if (!netHandle || netHandle.netId < 100) {
return false;
}
// Get the properties of the connection
let netCapa = connection.getNetCapabilitiesSync(netHandle);
let cap = netCapa.networkCap;
if (!cap) {
return false;
}
for (let em of cap) {
if (connection.NetCap.NET_CAPABILITY_VALIDATED == em) {
return true;
}
}
} catch (e) {
let err = e as BusinessError
console.info('get netInfo error :' + JSON.stringify(err))
}
return false;
}
}
3. How to solve the卡顿 problem caused by frequently calling createModuleContext to read cross-package resources?
Frequent calls to createModuleContext to load resources of specified modules cause functional卡顿.
createModuleContext involves multiple package information queries and loading all resources of the specified module, which is an interface with relatively large delay. If it needs to be used multiple times, it is best to cache the context to avoid repeated creation.
It is recommended that users save the context during the first initialization and directly use the saved context to obtain the corresponding resources.
4. How to handle the卡顿 problem caused by the long time spent generating key-values in the third parameter of ForEach?
For the performance impact of ForEach's time consumption, pay attention to the rules for generating keys.
When using ForEach for rendering, if the third parameter KeyGenerator function is in the default state, the ArkUI framework will use the default key-value generation function, that is, (item: any, index: number) => { return index + '_' + JSON.stringify(item); }
. In complex object collections and complex interactions, this will affect rendering performance.
To ensure the uniqueness of key-values, for object data types, it is recommended to use the unique id in the object data as the key-value. It is not recommended to leave the third parameter KeyGenerator function in the default state or include the data item index index in the key-value generation rules.
For more ForEach key-value rules, refer to: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-foreach-V5#%E4%BD%BF%E7%94%A8%E5%BB%BA%E8%AE%AE
5. Questions about the specifications of the system player AVplayer?
- Does the system player AVplayer support these streaming media protocols: HLS-TS/HLS-FMP4/HTTP-MP4/HTTPS?
- Does the system player AVplayer support encoding formats: H264/H265/AAC?
- Does the system player AVPlayer support functions such as play, pause, seek, episode switching, and full screen?
Answers:
- Supports HLS/HTTP/HTTPS.
- Supports H264/H265/AAC.
- Supports functions such as play, pause, and seek. Episode switching and full-screen functions need to be implemented by users themselves.
Top comments (0)