點擊藍字 ╳ 關注我們
開源項目 OpenHarmony是每個人的 OpenHarmony![880c44d6-5b88-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/43/wKgZomTnMN6AZ353AA_vv_QUwQE752.png)
馬迪欣
OpenHarmony知識體系工作組
以下內容來自嘉賓分享,不代表開放原子開源基金會觀點 OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)應用開發自API 8及其更早版本一直使用的是FA模型進行開發。FA模型是Feature Ability的縮寫,它和PA(Particle Ability)兩種類型是過往長期推廣的術語,深入人心。 然而從API 9開始,Ability框架引入了Stage模型作為第二種應用框架形態,Stage模型將Ability分為PageAbility和ExtensionAbility兩大類,其中ExtensionAbility又被擴展為ServiceExtensionAbility、FormExtensionAbility、DataShareExtensionAbility等一系列ExtensionAbility,以便滿足更多的使用場景。新模型接口中有AbilityStage/WindowStage的概念,這個Stage本身有舞臺的意思,寓意是給開發者一個新的展現舞臺。Stage模型的設計,主要是為了開發者更加方便地開發出分布式環境下的復雜應用。下表給出了兩種模型在設計上的差異:![88356d8e-5b88-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/43/wKgZomTnMN6AE4fKAAItWIC5NoI544.png)
![885ce5e4-5b88-11ed-a3b6-dac502259ad0.png](https://file1.elecfans.com//web2/M00/97/43/wKgZomTnMN6AeSuwAAE1d54a4ao612.png)
import featureAbility from '@ohos.ability.featureAbility';
featureAbility.startAbility({
want: wantValue
}).then((data) => {
CommonLog.info('startAbilityContinuation finished, ' + JSON.stringify(data))
//拉起后,自我關閉
featureAbility.terminateSelf((error) => {
CommonLog.info('startAbilityContinuation terminateSelf finished, error=' + JSON.stringify(error))
})
}).catch((error) => {
CommonLog.info('startAbilityContinuation error ' + JSON.stringify(error))
})
而在stage模型里,由于不再有featureAbility,因此無法import featureAbility,進而無法使用featureAbility.startAbility拉起應用,進而使用getContext獲取上下文后,調用startAbility拉起應用。
getContext(this).startAbility(want).then((data) => {
CommonLog.info('startAbilityContinuation finished, ' + JSON.stringify(data))
//自我關閉
getContext(this).terminateSelf((error) => {
CommonLog.info('startAbilityContinuation terminateSelf finished, error=' + JSON.stringify(error))
})
}).catch((error) => {
CommonLog.info('startAbilityContinuation error ' + JSON.stringify(error))
})
除了startAbility外,樣例里使用到的獲取包含bundleName,設備發現deviceManager的相關API都需要按照上述方法進行修改。
修改點3:數據從組件分離,提取到Ability中
在分布式拉起時,需要傳遞當前播放的音樂和音樂的播放進度。在兩種模型里,這些參數都是被設置在wantValue的parameters里,通過startAbility傳出去。
let params = {
index: this.playerManager.getCurrentMusicIndex(),
seekTo: this.playerManager.getCurrentTimeMs(),
isPlaying: this.isPlaying
}
let wantValue = {
bundleName: this.bundleName,
abilityName: 'com.madixin.music.MainAbility',
deviceId: remoteDevice.deviceId,
parameters: params
}
但在接收參數時,FA模型里,是在當前組件的代碼里,通過featureAbility.getWant來獲取參數,如下代碼。
featureAbility.getWant((error, want) => {
CommonLog.info('restoreFromWant featureAbility.getWant=' + JSON.stringify(want))
let status = want.parameters
if (status != null && status.index != null) {
this.playerManager.playSpecifyMusic(status.seekTo, status.index)
this.isPlaying = true
this.playAnimation()
}
})
而使用Stage模型后,雖然參數傳遞的方式是一致的,但是無法直接在組件UI中獲取參數,而需要先在MainAbility.ts獲取參數want。此時如果要傳遞給組件,有多種方式,這里我是使用的如下方式,即在MainAbility.ts的onCreate和onNewWant里,把want賦值到globalThis里,然后在UI組件里,通過globalThis獲取參數。
// MainAbility.ts
onNewWant(want, launchParams) {
globalThis.newWant = want
hilog.info(0x0000, 'MyOpenHarmonyPlayer', '%{public}s', 'onNewWant launchParam:' + JSON.stringify(launchParams) ?? '');
}
onCreate(want, launchParam) {
globalThis.newWant = want
hilog.info(0x0000, 'MyOpenHarmonyPlayer', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
hilog.info(0x0000, 'MyOpenHarmonyPlayer', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
}
// index.ets
let newWant = globalThis.newWant
CommonLog.info("aboutToAppear newWant:" + JSON.stringify(newWant))
if (newWant !== null && newWant.parameters.hasOwnProperty("seekTo")) {
this.playerManager.playSpecifyMusic(newWant.parameters.seekTo, newWant.parameters.index)
}
另外,了解到還有一種方式傳遞數據是使用AppStorage來關聯,比如在MainAbility.ts里使用AppStorage.SetOrCreate傳入數據,在UI組件里,使用@StorageLink標簽修飾變量來獲取數據。
除以上三點修改外,還有兩點值得說明下
首先是因OpenHarmony 3.2后分布式能力限制智能系統應用使用,需要提升apl等級:找到所使用API版本對應toolchains>版本號>lib>UnsgnedReleasedProfileTemplate.json,更改 "apl": "normal"為 "apl": "system_core"。
其次是API 9以后區分了public-SDK和Full SDK。DevEco Studio默認下載的是public-SDK,它不包含系統應用所需要的高權限API。當我們import deviceManager from '@ohos.distributedHardware.deviceManager'時,會發現里面只有一個空的接口,沒有任何方法。雖然這不影響功能,但代碼中必須使用@ts-ignore忽略typescript的告警,而且沒有語法提示。此時,需要使用full-SDK替換。
相關文檔請參考https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/full-sdk-switch-guide.md
新增首頁頁面,和播放列表頁的動畫,不是本文的重點,大家可以參考代碼自行學習。總結
OpenHarmony的FA模型能力已經停止演進,后續將會增強Stage模型。此次將現有的樣例代碼適配Stage模型,雖然整體代碼修改量不大,但因為慣性思維以及API的變化,期間還是踩了不少坑。我已在OpenHarmony知識體系倉中更新了樣例代碼,歡迎開發者來參考和指正問題,建議新上手OpenHarmony的開發者可以直接學習使用新的Stage模型來開發應用。 前面提到在Stage模型里,ExtensionAbility又被擴展為ServiceExtensionAbility、FormExtensionAbility、DataShareExtensionAbility等一系列ExtensionAbility,這個樣例目前還沒有涉及到,待后續進一步學習,通過ExtensionAbility把音樂播放實現成一個后臺服務,從而實現應用在后臺時也能繼續播放音樂,屆時將持續更新這個應用,也歡迎大家一起共建。 分布式音樂播放器樣例地址https://growing.openharmony.cn/mainPlay/detail?sampleId=3742
原文標題:我把分布式音樂播放器適配了Stage模型
文章出處:【微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
鴻蒙
+關注
關注
57文章
2392瀏覽量
42972 -
OpenHarmony
+關注
關注
25文章
3744瀏覽量
16487
原文標題:我把分布式音樂播放器適配了Stage模型
文章出處:【微信號:gh_e4f28cfa3159,微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
基于戰艦板的遙控音樂播放器
2012年買的戰艦板,有較長時間放著吃灰覺得很可惜。感覺戰艦板的音樂播放器音質蠻不錯的,于是想就折騰一個遙控音樂播放器。貌似很簡單的事,實際上我
發表于 07-01 04:35
基于潤和DAYU200開發套件的OpenHarmony分布式音樂播放器
RJ45以太網口,可滿足NVR、工業網關等多網口產品需求。分布式音樂播放器這里給大家分享一個樣例,分布式音樂
發表于 03-14 09:07
DistributedVideoPlayer分布式視頻播放器的設計資料
1、DistributedVideoPlayer分布式視頻播放器的設計資料(一)本示例是在官方Video Play Ability 模板基礎上做了擴展開發,官方模板提供基本的視頻播放功能,并允許您在
發表于 03-22 11:55
求一種基于DAYU200開發套件的分布式音樂播放器設計方案
、音頻、視頻和攝像頭等功能,擁有豐富的擴展接口,支持多種視頻輸入輸出接口;配置雙千兆自適應RJ45以太網口,可滿足NVR、工業網關等多網口產品需求。分布式音樂播放器這里給大家分享一個樣例,分布
發表于 09-08 17:22
我把分布式音樂播放器適配了Stage模型
Ability使用一個VM實例,而Stage模型整個進程只使用一個VM實例,減少進程內存占用,應用內狀態在進程內共享。分布式音樂播放器,是今
發表于 11-07 11:43
Windows Media Player音樂播放器代碼
Windows Media Player音樂播放器代碼
一、Windows Media Player音樂播放器代碼:
發表于 01-10 11:14
?2277次閱讀
基于Android音樂播放器的研究
Android平臺是目前智能移動終端的主流系統。隨著人們生活、工作節奏的加快,乘車、運動、學習等碎片時間的增多,音樂播放器成為人們所關心的必備應用之一,廣受大家歡迎。 目前,Android市場上以酷
發表于 12-11 11:44
?7次下載
![基于Android<b class='flag-5'>音樂</b><b class='flag-5'>播放器</b>的研究](https://file.elecfans.com/web2/M00/49/7A/poYBAGKhwLuAS1jzAAAatSawbTk057.jpg)
鴻蒙HarmonyOS開發實戰:【分布式音樂播放】
本示例使用fileIo獲取指定音頻文件,并通過AudioPlayer完成了音樂的播放完成了基本的音樂播放、暫停、上一曲、下一曲功能;并使用DeviceManager完成了
![鴻蒙HarmonyOS開發實戰:【<b class='flag-5'>分布式</b><b class='flag-5'>音樂</b><b class='flag-5'>播放</b>】](https://file1.elecfans.com/web2/M00/C7/CE/wKgZomYWYNyAFSiFAAQDf9vAtiw310.png)
評論