成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS上視頻跨設備協同技術超全詳解

開發 OpenHarmony
文章由鴻蒙社區產出,想要了解更多內容請前往:51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com

[[388995]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1. 介紹

您將會學到什么

● 如何使用PageSlider、PageSliderIndicator和ListContainer編寫定時滾動及可滑動的頁面。

● 如何使用分布式能力實現跨設備視頻播放。

● 如何使用HarmonyOS IDL跨進程通信實現遠程控制視頻播放。

技能要求

● HarmonyOS Player接口熟練使用

● 基本組件熟練使用

🕮 說明

本篇Codelab所附代碼適合在真機運行。運行時需要至少兩臺手機處于同一個分布式網絡中,可以通過操作如下配置實現:

● 所有手機接入同一網絡

● 所有手機登錄相同華為賬號

● 所有手機上開啟“設置->更多連接->多設備協同 ”

2. 代碼結構

在鴻蒙上實現本地和Internet視頻資源播放已對視頻播放和播放界面代碼結構做了講解,本次Codelab只對視頻列表頁、視頻遷移設備列表、遷移后控制界面及遷移服務核心代碼做講解,對于完整代碼,我們會在參考提供下載方式。代碼結構圖如下:

● provider:該目錄包含CommonProvider、ViewProvider和AdvertisementProvider。CommonProvider是一個ListContainer 多樣式提供者管理類。ViewProvider結合CommonProvider使用,可以把布局文件中需要賦值的控件單獨提取出來進行賦值。AdvertisementProvider實現廣告視頻資源定時滾動的效果。

● ImplVideoMigration.idl:接口中定義了視頻遷入、遷出、根據控制碼對視頻進行遠程控制方法。

● data:該目錄包括滾動視頻廣告對象封裝、即將上映視頻對象封裝以及視頻圖片格式定義。

● VideoMigrateService:供遠端連接的Service Ability。

● manager:該目錄下的文件為ImplVideoMigration.idl在編譯時自行生成,初始生成位置為entry\build\generated\source\idl\com\huawei\codelab。

● MediaUtil:對廣告和視頻列表對象初始化賦值。

● config.json:配置文件,新增權限配置如下圖:

1. ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE:用于允許監聽分布式組網內的設備狀態變化。

2. ohos.permission.GET_DISTRIBUTED_DEVICE_INFO:用于允許獲取分布式組網內的設備列表和設備信息。

3. ohos.permission.GET_BUNDLE_INFO:用于查詢其他應用的信息。

4. ohos.permission.DISTRIBUTED_DATASYNC:用于允許不同設備間的數據交換。

5. ohos.permission.INTERNET:用于允許設備訪問網絡。

3. 創建應用程序布局文件

在路徑"resources/base/layout"文件夾下創建video.xml為應用主頁面,展示要播放的視頻列表。

  1. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"  
  2.                    ohos:width="match_parent"  
  3.                    ohos:height="match_parent"  
  4.                    ohos:orientation="vertical">  
  5.     <DirectionalLayout  
  6.         ohos:height="match_content"  
  7.         ohos:width="match_parent"  
  8.         ohos:orientation="vertical"  
  9.         >  
  10.         <!--滾動的視頻圖片-->  
  11.         <DependentLayout  
  12.             ohos:id="$+id:video_advertisement_container_view"  
  13.             ohos:width="match_parent"  
  14.             ohos:left_margin="20vp"  
  15.             ohos:height="175vp"  
  16.             ohos:top_margin="20vp"  
  17.             ohos:right_margin="12vp"  
  18.             >  
  19.             <PageSlider  
  20.                 ohos:id="$+id:video_advertisement_viewpager"  
  21.                 ohos:width="match_parent"  
  22.                 ohos:height="match_parent"  
  23.                 ohos:orientation="horizontal"/>  
  24.    
  25.             <PageSliderIndicator  
  26.                 ohos:id="$+id:video_advertisement_indicator"  
  27.                 ohos:right_margin="8vp"  
  28.                 ohos:bottom_margin="7vp"  
  29.                 ohos:width="match_content"  
  30.                 ohos:height="match_content"  
  31.                 ohos:align_parent_bottom="true"  
  32.                 ohos:align_parent_right="true" />  
  33.         </DependentLayout>  
  34.         <!--即將上映-->  
  35.         <DirectionalLayout  
  36.             ohos:width="match_parent"  
  37.             ohos:height="22vp"  
  38.             ohos:top_margin="12vp"  
  39.             ohos:left_margin="24vp"  
  40.             ohos:right_margin="12vp"  
  41.             ohos:orientation="horizontal">  
  42.             <Text  
  43.                 ohos:id="$+id:video_play_title"  
  44.                 ohos:text="Coming soon"  
  45.                 ohos:text_size="16fp"  
  46.                 ohos:text_color="#ff000000"  
  47.                 ohos:text_alignment="4"  
  48.                 ohos:layout_alignment="vertical_center"  
  49.                 ohos:width="match_content"  
  50.                 ohos:height="match_content" />  
  51.             <Image  
  52.                 ohos:left_margin="6vp"  
  53.                 ohos:width="13vp"  
  54.                 ohos:height="13vp"  
  55.                 ohos:layout_alignment="vertical_center"  
  56.                 ohos:image_src="$media:ic_next"/>  
  57.    
  58.         </DirectionalLayout>  
  59.         <!--可橫向滑動的視頻圖片-->  
  60.         <DirectionalLayout  
  61.             ohos:width="match_parent"  
  62.             ohos:height="500vp"  
  63.             ohos:orientation="vertical">  
  64.             <ListContainer  
  65.                 ohos:id="$+id:video_list_play_view"  
  66.                 ohos:width="match_parent"  
  67.                 ohos:height="match_content"  
  68.                 ohos:orientation="horizontal"  
  69.                 ohos:left_margin="18vp"  
  70.                 ohos:top_margin="12vp"  
  71.                 >  
  72.             </ListContainer>  
  73.         </DirectionalLayout>  
  74.     </DirectionalLayout>  
  75.    
  76. </DirectionalLayout> 

video.xml采用垂直方向的線性布局方式。整個頁面分為三部分的內容。從上至下依次是PageSlider滾動廣告布局,即將上映視頻圖標布局,可左右滑動的listContainer布局。

PageSlider是一個描述滾動頁面的組件,PageSliderIndicator是一個將滾動頁面組件和其它組件比如圖標、按鈕等組合管理的管理器。本應用程序展示的滾動廣告頁面采取的是三組廣告圖片和圖片title組成的PageSlider,廣告圖片和圖片title組合樣式由AdvertisementProvider定義。AdvertisementMo初始化代碼如下:

  1. public AdvertisementMo(int sourceId, String description) {  
  2.     this.sourceId = sourceId;  
  3.     this.description = description;  
  4. }  
  5. videoAdvertisementMos.add(new AdvertisementMo(ResourceTable.Media_video_advertisement0, "玩心釋放 盡情創想"));  
  6. videoAdvertisementMos.add(new AdvertisementMo(ResourceTable.Media_video_advertisement1, "玩心釋放 盡情創想"));  
  7. videoAdvertisementMos.add(new AdvertisementMo(ResourceTable.Media_video_advertisement2, "一起創造 煥新假期")); 

AdvertisementProvider對滾動視頻廣告組件以list形式進行封裝。

  1. public class AdvertisementProvider<T extends Component> extends PageSliderProvider {  
  2.     private List<T> componentList;  
  3.     public AdvertisementProvider(List<T> componentList) {  
  4.         this.componentList = componentList;  
  5.     }  

通過PageSlider對象的setProvider(CommProvider)方法即可達到對圖片列表地滾動顯示效果。

  1. advertisementProvider = new AdvertisementProvider<Component>(getAdvertisementComponents());  
  2. Component advViewPager = findComponentById(ResourceTable.Id_video_advertisement_viewpager);  
  3. if (advViewPager instanceof PageSlider) {  
  4.     advPageSlider = (PageSlider) advViewPager;  
  5.     advPageSlider.setProvider(advertisementProvider);  

getAdertisementCompoents方法將滾動視頻廣告添加到list。

  1. private List<Component> getAdvertisementComponents() {  
  2.     List<AdvertisementMo> advertisementMos = MediaUtil.getVideoAdvertisementInfo();  
  3.     List<Component> componentList = new ArrayList<>(advertisementMos.size());  
  4.     Font.Builder fb = new Font.Builder(VideoTabStyle.BOLD_FONT_NAME);  
  5.     fb.setWeight(Font.BOLD);  
  6.     Font newFont = fb.build();  
  7.     for (AdvertisementMo advertisementMo : advertisementMos) {  
  8.         Component advRootView = LayoutScatter.getInstance(getContext()).parse(  
  9.                 ResourceTable.Layout_video_advertisement_item, nullfalse);  
  10.         Image imgTemp = null;  
  11.         if (advRootView.findComponentById(ResourceTable.Id_video_advertisement_poster) instanceof Image) {  
  12.             imgTemp = (Image) advRootView.findComponentById(ResourceTable.Id_video_advertisement_poster);  
  13.         }  
  14.         imgTemp.setPixelMap(advertisementMo.getSourceId());  
  15.         Text titleTmp = null;  
  16.         if (advRootView.findComponentById(ResourceTable.Id_video_advertisement_title) instanceof Text) {  
  17.             titleTmp = (Text) advRootView.findComponentById(ResourceTable.Id_video_advertisement_title);  
  18.         }  
  19.         titleTmp.setText(advertisementMo.getDescription());  
  20.         titleTmp.setFont(newFont);  
  21.         componentList.add(advRootView);  
  22.     }  
  23.    
  24.     return componentList;  

想要實現滾動到某一特定圖片時呈現標志,在圖片上方加上一組空心圓,當滾動到第一張圖片時,第一個圓變為實心,此聯動實現效果可通過PageSliderIndicator實現。

  1. PageSliderIndicator advIndicator = null;  
  2. if (findComponentById(ResourceTable.Id_video_advertisement_indicator) instanceof PageSliderIndicator) {  
  3.     advIndicator = (PageSliderIndicator) findComponentById(  
  4.             ResourceTable.Id_video_advertisement_indicator);  
  5. }  
  6. advIndicator.setItemOffset(VideoTabStyle.INDICATOR_OFFSET); 

實心圓效果:

  1. ShapeElement normalDrawable = new ShapeElement();  
  2. normalDrawable.setRgbColor(RgbColor.fromRgbaInt(Color.WHITE.getValue()));  
  3. normalDrawable.setAlpha(VideoTabStyle.INDICATOR_NORMA_ALPHA);  
  4. normalDrawable.setShape(ShapeElement.OVAL);  
  5. normalDrawable.setBounds(0, 0, VideoTabStyle.INDICATOR_BONDS, VideoTabStyle.INDICATOR_BONDS); 

空心圓效果:

  1. ShapeElement selectedDrawable = new ShapeElement();  
  2. selectedDrawable.setRgbColor(RgbColor.fromRgbaInt(Color.WHITE.getValue()));  
  3. selectedDrawable.setShape(ShapeElement.OVAL);  
  4. selectedDrawable.setBounds(0, 0, VideoTabStyle.INDICATOR_BONDS, VideoTabStyle.INDICATOR_BONDS); 

實心圓、空心圓效果如下圖:

PageSliderIndicator通過設置可選類型將會實現圖片被選中時,將會顯示實心圓。

  1. advIndicator.setItemElement(normalDrawable, selectedDrawable);  
  2. advIndicator.setViewPager((PageSlider) advViewPager); 

本節任務完成的效果如下圖:

視頻播放業務本次Codelab不再描述,下面直接進入視頻流轉環節。

4. 視頻跨設備協同

HarmonyOS提供了分布式跨設備能力,本小節可以實現將視頻遷移到分布式環境中的其它設備上,被遷移設備可以實現對遷移設備的視頻操作控制。

首先對視頻播放界面中遷移按鈕增加監聽事件,在點擊時,從窗口底部滑出分布式設備列表界面可供選擇遷移。

  1. tv = (Image) simplePlayerController.findComponentById(ResourceTable.Id_tv);  
  2. tv.setClickedListener(new Component.ClickedListener() {  
  3.     @Override  
  4.     public void onClick(Component component) {  
  5.         initDevices();  
  6.         showDeviceList();  
  7.     }  
  8. }); 

通過分布式設備管理器DeviceManager獲取到當前分布式網絡中可發現的所有設備并全部添加到設備列表。如果設備列表初始不為空,先將列表清空,再添加,以達到刷新設備列表效果。

  1. private void initDevices() {  
  2.     if (devices.size() > 0) {  
  3.         devices.clear();  
  4.     }  
  5.     // 通過FLAG_GET_ONLINE_DEVICE標記獲得在線設備列表  
  6.     List<DeviceInfo> deviceInfos = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);  
  7.     devices.addAll(deviceInfos);  

顯示設備列表使用單樣式的內容提供器CommonProvider,設置設備名字樣式。

  1. private void showDeviceList() {  
  2.     CommonProvider commonProvider = new CommonProvider<DeviceInfo>(devices,getContext(), ResourceTable.Layout_device_list_item) {  
  3.         @Override  
  4.         protected void convert(ViewProvider viewProvider, DeviceInfo item, int position) {  
  5.             viewProvider.setText(ResourceTable.Id_device_text, item.getDeviceName());  
  6.         }  
  7.     };  
  8.     // 對deviceListContainer注入commonProvider,完成設備列表資源樣式設置  
  9.     deviceListContainer.setItemProvider(commonProvider);  
  10.     // 通知列表數據發生變化更新設備列表  
  11.     commonProvider.notifyDataChanged();  
  12.     transWindow.show();  

創建設備列表顯示組件SlidePopupWindow。設備列表是一個從底部滑出的一個窗口,屬于自定義組件。核心功能是設備列表的顯示與隱藏。

  1. public void show() {  
  2.     if (!isShow) {  
  3.         isShow = true;  
  4.         animatorProperty  
  5.                 .moveFromX(startX)  
  6.                 .moveToX(endX)  
  7.                 .moveFromY(startY)  
  8.                 .moveToY(endY)  
  9.                 .setCurveType(Animator.CurveType.LINEAR)  
  10.                 .setDuration(ANIM_DURATION)  
  11.                 .start();  
  12.     }  
  13. }  
  14.    
  15. public void hide() {  
  16.     if (isShow) {  
  17.         isShow = false;  
  18.         animatorProperty  
  19.                 .moveFromX(endX)  
  20.                 .moveToX(startX)  
  21.                 .moveFromY(endY)  
  22.                 .moveToY(startY)  
  23.                 .setCurveType(Animator.CurveType.LINEAR)  
  24.                 .setDuration(ANIM_DURATION)  
  25.                 .start();  
  26.     }  

設備列表效果如下圖:

點擊列表中某一個設備,將在已選設備端拉起該視頻應用。

  1. deviceListContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {  
  2.     @Override  
  3.     public void onItemClicked(ListContainer listContainer, Component component, int num, long l) {  
  4.         // 列表窗口隱藏  
  5.         transWindow.hide();  
  6.         startAbilityFa(devices.get(num).getDeviceId());  
  7.     }  
  8. }); 

通過startAbilityFa()跨設備拉起視頻FA,再調用connectAbility()異步對遠端服務連接,成功連接后,在回調onAbilityConnectDone中服務端恢復視頻數據。

  1. private void startAbilityFa(String devicesId) {  
  2.     Intent intent = new Intent();  
  3.     Operation operation =  
  4.             new Intent.OperationBuilder()  
  5.                     .withDeviceId(devicesId)  
  6.                     .withBundleName(getBundleName())  
  7.                     .withAbilityName(VideoMigrateService.class.getName())  
  8.                     .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)  
  9.                     .build();// 開發者需要在Intent中設置支持分布式的標記FLAG_ABILITYSLICE_MULTI_DEVICE,否則無法獲得分布式能力  
  10.     intent.setOperation(operation);  
  11.     boolean connectFlag = connectAbility(intent,  
  12.                     new IAbilityConnection() {  
  13.                         @Override  
  14.                         public void onAbilityConnectDone(  
  15.                                 ElementName elementName, IRemoteObject remoteObject, int i) {  
  16.                             // asInterface的作用是根據調用的服務是否屬于同進程而返回不同的實例對象  
  17.                             implVideoMigration = VideoMigrationStub.asInterface(remoteObject);  
  18.                             try {  
  19.                                 implVideoMigration.flyIn(startMillisecond);  
  20.                             } catch (RemoteException e) {  
  21.                                 LogUtil.error(TAG, "connect successful,but have remote exception");  
  22.                             }  
  23.                         }  
  24.    
  25.                         @Override  
  26.                         public void onAbilityDisconnectDone(ElementName elementName, int i) {  
  27.                             disconnectAbility(this);  
  28.                         }  
  29.                     });  
  30.     if (connectFlag) {  
  31.         Toast.toast(this, "migrate successful!", TOAST_DURATION);  
  32.         remoteController.show();  
  33.         startMillisecond = implPlayer.getAudioCurrentPosition();// 獲取視頻當前播放進度  
  34.         implPlayer.release();// 釋放資源  
  35.     } else {  
  36.         Toast.toast(this, "migrate failed!Please try again later.", TOAST_DURATION);  
  37.     }  

通過指定abilityName為VideoMigrateService,執行VideoMigrateService中onConnect(intent)方法,返回binder對象,回調onAbilityConnectDone拿到具體的binder對象。VideoMigrationStub.asInterface(remoteObject)根據調用是否屬于同進程而返回不同的實例對象, 由于返回的binder不是本進程的,所以返回的是VideoMigrationProxy對象。

接下來我們分別把本端設備稱為設備A,跨設備協同端稱為設備B。 implVideoMigration.flyIn(startMillisecond)由設備A即VideoMigrationProxy執行,通過sendRequest發送到設備B。

  1. remote.sendRequest(COMMAND_FLY_IN, data, reply, option); 

設備B通過接收到的code類型為COMMAND_FLY_IN在服務端執行視頻數據恢復。

  1. @Override  
  2. public void flyIn(int startTimemiles) throws RemoteException {  
  3.     Intent intent = new Intent();  
  4.     Operation operation =  
  5.             new Intent.OperationBuilder()  
  6.                     .withBundleName(getBundleName())  
  7.                     .withAbilityName(MainAbility.class.getName())  
  8.                     .withAction("action.video.play")  
  9.                     .build();  
  10.     intent.setOperation(operation);  
  11.     intent.setParam(Constants.INTENT_STARTTIME_PARAM, startTimemiles);  
  12.     startAbility(intent);  

設備B呈現播放界面并跳轉到Intent中攜帶的播放位置。在設備A的視頻應用跨設備協同到設備B時,設備A會釋放掉視頻資源并展示RemoteController。

  1. if (connectFlag) {  
  2.     Toast.toast(this, "migrate successful!", TOAST_DURATION);  
  3.     remoteController.show();// 控制界面出現  
  4.     startMillisecond = implPlayer.getAudioCurrentPosition();  
  5.     implPlayer.release();  

設備A的RemoteController在創建時初始化界面布局。通過操作界面控件來控制設備B視頻播放。例如點擊前進按鈕,RemoteController發送FORWARD 控制碼。SimplePlayerAbilitySlice通過添加RemoteController.RemoteControllerListener來執行回調方法sendControl,再通過implVideoMigration代理對象與對端進行通信。

  1. remoteController.setRemoteControllerCallback(new RemoteController.RemoteControllerListener() {  
  2. @Override  
  3.     public void sendControl(int code, int extra) {  
  4.     try {  
  5.         if (implVideoMigration != null) {  
  6.             // 調用設備A服務代理對象的playControl方法通過binder對象調用設備B服務端的playControl方法  
  7.             implVideoMigration.playControl(code, extra);  
  8.         }  
  9.     } catch (RemoteException e) {  
  10.         LogUtil.error(TAG, "RemoteException occurs ");  
  11.     }  
  12.   }  
  13. }); 

設備A效果如下圖:

設備B效果如下圖:

當設備A在RemoteController界面執行返回操作時,會隱藏RemoteController,同時設備A繼續播放。

  1. public void hide() {  
  2.     if (isShown) {  
  3.         isShown = false;  
  4.         setVisibility(INVISIBLE);  
  5.         if (remoteControllerListener != null) {  
  6.             remoteControllerListener.controllerDismiss();  
  7.         }  
  8.     }  
  9. }  
  10. remoteController.setRemoteControllerCallback(new RemoteController.RemoteControllerListener() {  
  11. @Override  
  12. public void controllerDismiss() {  
  13.     int progress = 0;  
  14.     try {  
  15.         if (implVideoMigration!= null) {  
  16.             // 遷回視頻時獲取進度條進度  
  17.             progress = implVideoMigration.flyOut();  
  18.         }  
  19.     } catch (RemoteException e) {  
  20.         LogUtil.e(TAG, "RemoteException occurs");  
  21.     }  
  22.     // 設備A視頻按照遷回的視頻進度繼續播放  
  23.     implPlayer.reload(url, progress);  
  24. }  
  25. }); 

🕮 說明

以上代碼僅demo演示參考使用,產品化的代碼需要使用國際化。

5. 恭喜你

● 通過使用PageSlider、PageSliderIndicator結合ListContainer編寫定時滾動及可滑動的頁面。

● HarmonyOS通過DeviceManger獲取分布式網絡中設備列表,選中設備ID之后,再通過IDL跨進程通信方式將FA或PA攜帶數據跨設備拉起。

● 整體運行效果圖如下:

設備A視頻跨設備協同后效果圖如下:

至此,您已經完成HarmonyOS上視頻跨設備協同的體驗!

6. 參考

gitee源碼

github源碼

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2022-06-09 14:08:34

多設備協同鴻蒙

2021-01-14 05:16:09

MyBatis動態代理

2022-07-25 17:57:43

技術跨平臺

2010-06-24 09:54:50

HTML 5Video標簽

2020-11-05 10:05:25

App

2011-03-07 17:25:33

ProFTPD啟動

2011-03-07 17:15:52

ProFTPD配置

2011-03-07 17:24:33

ProFTPD安裝

2021-06-23 15:48:08

鴻蒙HarmonyOS應用

2021-01-06 11:21:56

鴻蒙HarmonyOS應用開發

2021-06-16 15:18:03

鴻蒙HarmonyOS應用

2021-01-06 10:59:14

鴻蒙HarmonyOSPage Abilit

2024-11-07 16:39:42

SpringBoo常用注解Bean

2020-12-28 09:41:40

MySQL數據庫函數

2021-07-08 09:42:04

鴻蒙HarmonyOS應用

2018-01-23 07:07:54

2015-07-20 17:12:57

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线天堂免费中文字幕视频 | 在线观看国产wwwa级羞羞视频 | 午夜电影网站 | 国产精品视频中文字幕 | 天天操天天摸天天爽 | 99精彩视频| 久久久久亚洲精品 | 一级免费毛片 | 6080亚洲精品一区二区 | 国产视频h | 一区在线视频 | 亚洲精品自在在线观看 | 久久99视频免费观看 | 我想看国产一级毛片 | 久久久久国产一区二区三区四区 | 7777久久 | 亚洲精品天堂 | 成人不卡视频 | 九九99九九精彩46 | 一级片在线观看 | 欧美一级视频 | 日韩中文在线视频 | 亚洲一区二区三区在线 | 福利社午夜影院 | 天天插天天狠天天透 | 亚洲精品一区二区 | 欧美福利| 99re视频在线观看 | 亚洲欧美视频 | 久草视| 在线视频国产一区 | 日韩在线视频一区 | 精品国产视频 | 成人1区2区 | 日日操夜夜操天天操 | 色播视频在线观看 | 日韩精品一区二区三区在线播放 | 欧美精品一区三区 | 精品视频一区二区三区四区 | 亚洲视频 欧美视频 | 日本免费一区二区三区 |