HarmonyOS鸿蒙Next中从后端请求到的视音频地址,含有中文名字的展示不出来,用encodeURI编码也不行,是不是有其它方法可以解决
HarmonyOS鸿蒙Next中从后端请求到的视音频地址,含有中文名字的展示不出来,用encodeURI编码也不行,是不是有其它方法可以解决 从后端请求到的视音频地址,全是英文名字的都能正常显示出来,含有中文名字的展示不出来,用encodeURI编码也不行,是不是有其它方法可以解决
您可以使用AVPlayer API来播放。参考文档如下:
视频播放:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/video-playback-0000001774120630
更多关于HarmonyOS鸿蒙Next中从后端请求到的视音频地址,含有中文名字的展示不出来,用encodeURI编码也不行,是不是有其它方法可以解决的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
A页面消息发送后,B页面接收消息。此时您还在A页面。当您路由到B页面 message会重新赋值Hello World ( @State message: string = 'Hello World';)。
如果作用域的问题,可以通过const that = this解决
PageA.ets
import { router } from '@kit.ArkUI';
import { LiveEventBus } from '@ohos/liveeventbus';
@Component
struct PageA {
build() {
Scroll() {
Column() {
Button('跳转B页面3s后发送消息').margin({ bottom: '10vp' }).onClick(() => {
setTimeout(()=>{
LiveEventBus.get<string>("aaa").post("lalala");
},3000)
router.pushUrl({
url: 'pages/PageB'
})
})
}
}
}
}
PageB.ets
import { LiveEventBus, Lifecycle, MState } from '@ohos/liveeventbus';
import { router } from '@kit.ArkUI';
@Component
struct PageB {
@State message: string = 'Hello World';
@StorageLink('aaa') msg: string = '';
observeFlag: boolean = false;
mLifecycle: Lifecycle = new Lifecycle(MState.STARTED);
aboutToAppear() {
//创建生命周期感知对象
this.mLifecycle = new Lifecycle(MState.STARTED)
//订阅消息
//如果作用域的问题,可以通过const that = this解决
const that = this
LiveEventBus
.get<string>('aaa')
.observe(this, {
onChanged(s: string) {
that.message = s
// AppStorage.setOrCreate('aaa', s)
}
});
}
build() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
router.back()
})
Text(this.msg)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
router.back()
})
Button('手动修改').onClick(() => {
this.message = "手动修改"
})
Button('发送消息').onClick(() => {
LiveEventBus.get<string>("aaa").post("lalala2222");
})
}
.height('100%')
.width('100%')
}
//生命周期感知对象
getLifecycle(): Lifecycle {
return this.mLifecycle
}
}
在HarmonyOS鸿蒙Next中,如果从后端请求到的视音频地址含有中文名字无法正常展示,即使使用encodeURI编码仍然无效,可能是因为编码方式或处理逻辑不匹配。可以尝试以下方法:
-
使用
encodeURIComponent:与encodeURI不同,encodeURIComponent会对URL中的所有特殊字符进行编码,包括中文。尝试将中文部分单独提取并使用encodeURIComponent进行编码。let url = "http://example.com/视频/" + encodeURIComponent("中文名字.mp4"); -
确保编码一致性:检查后端返回的URL是否已经经过编码,如果后端已经编码,前端无需再次编码,避免双重编码导致问题。
-
检查网络请求库:如果使用的是鸿蒙提供的网络请求库,确保其正确处理了URL编码。某些库可能会自动处理URL编码,导致手动编码失效。
-
调试和日志:在请求前后打印URL,确认编码后的URL是否符合预期。如果URL格式不正确,可能需要调整编码逻辑。
-
使用Base64编码:如果URL中的中文无法通过常规编码解决,可以尝试将中文部分转换为Base64编码,再拼接到URL中。
let chineseName = "中文名字.mp4"; let encodedName = btoa(unescape(encodeURIComponent(chineseName))); let url = "http://example.com/视频/" + encodedName; -
检查服务器配置:确保服务器支持处理包含编码字符的URL,某些服务器可能对URL的编码格式有特定要求。
通过这些方法,可以解决HarmonyOS鸿蒙Next中中文URL展示问题。
在HarmonyOS鸿蒙Next中,如果从后端请求到的视音频地址含有中文名字无法展示,可以尝试使用encodeURIComponent对整个URL进行编码,而不仅仅是部分字符。encodeURIComponent会对更多的字符进行编码,包括中文。例如:
let url = "http://example.com/中文视频.mp4";
let encodedUrl = encodeURIComponent(url);
如果仍然无法解决,建议检查服务器的返回内容是否已经正确编码,或者尝试在服务器端对URL进行编码后再返回。

