自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用鴻蒙的分布式助力七夕

系統(tǒng) 分布式
如果升級了最新的B站服務(wù)卡片,那么當(dāng)桌面上添加頭像卡片時,只要點擊頭像,就會看到下圖的效果。一個應(yīng)用鴻蒙分布式能力的小功能。

[[418672]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

在情人節(jié)后,為之前的B站卡片項目增加一個隱藏功能。如果升級了最新的B站服務(wù)卡片,那么當(dāng)桌面上添加頭像卡片時,只要點擊頭像,就會看到下圖的效果。一個應(yīng)用鴻蒙分布式能力的小功能。

視頻預(yù)覽地址:https://harmonyos.51cto.com/show/7762

完整項目地址:https://gitee.com/liangzili/bilibili-cards

1.添加一個播放頁

比如PlayerSlice,這個頁面用來實現(xiàn)視頻的播放。

用鴻蒙的分布式助力七夕-鴻蒙HarmonyOS技術(shù)社區(qū)

2.為頭像卡片添加點擊事件

當(dāng)點擊卡片上的頭像時實現(xiàn)頁面跳轉(zhuǎn),代碼如下

src/main/js/fans/pages/index/index.hml

  1. <div class="card_root_layout" else
  2.     <div class="div_left_container"
  3.         <stack class="stack-parent" onclick="sendRouterEvent"
  4.             <image src="{{src}}" class="image_src"></image> 
  5.             <image src="{{vip}}" class="image_vip"></image> 
  6.         </stack> 
  7.     </div> 
  8.     <text class="item_title">{{follower}}</text> 
  9. </div> 

actions中設(shè)置跳轉(zhuǎn)到剛才新建的播放頁面。

src/main/js/fans/pages/index/index.json

  1. "actions": { 
  2.   "sendRouterEvent": { 
  3.     "action""router"
  4.     "abilityName""com.liangzili.demos.Player"
  5.     "params"true 
  6.   } 

3.在播放頁判斷拉起方式

從intent中提取參數(shù)params,如果播放頁是服務(wù)卡片拉起的,得到true。如果是分布式拉起的得到false。

  1. params = intent.getStringParam("params");//從intent中獲取 跳轉(zhuǎn)事件定義的params字段的值 
  2. if(params.equals("true")){ 
  3.     Intent intent0 = new Intent(); 
  4.     Operation op = new Intent.OperationBuilder() 
  5.         .withDeviceId(DistributedUtils.getDeviceId())//參數(shù)1.是否跨設(shè)備,空,不跨設(shè)備 
  6.         .withBundleName("com.liangzili.demos")//參數(shù)2.在config.json中的bundleName 
  7.         .withAbilityName("com.liangzili.demos.Player")//參數(shù)3.要跳轉(zhuǎn)的ability名 
  8.         .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) 
  9.         .build(); 
  10.     intent0.setOperation(op); 
  11.     intent0.setParam("params","false"); 
  12.     startAbility(intent0); 
  13.     videoSource = "resources/base/media/right.mp4"
  14. }else
  15.     videoSource = "resources/base/media/left.mp4"

4.申請分布式拉起頁面權(quán)限

如果params就調(diào)用分布式拉起頁面,得提前為應(yīng)用獲取權(quán)限。

在app首次啟動時提醒用戶獲取分布式權(quán)限。

src/main/java/com/liangzili/demos/MainAbility.java

  1. requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"},0); 

5.獲取遠(yuǎn)端設(shè)備ID

要拉起遠(yuǎn)端設(shè)備上的頁面,得先獲取設(shè)備的ID。

  1. public class DistributedUtils { 
  2.     public static String getDeviceId(){ 
  3.         //獲取在線設(shè)備列表,getDeviceList拿到的設(shè)備不包含本機。 
  4.         List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); 
  5.         if(deviceList.isEmpty()){ 
  6.             return null
  7.         } 
  8.         int deviceNum = deviceList.size(); 
  9.         List<String> deviceIds = new ArrayList<>(deviceNum);    //提取設(shè)備Id 
  10.         List<String> deviceNames = new ArrayList<>(deviceNum);  //提取設(shè)備名 
  11.         deviceList.forEach((device)->{ 
  12.             deviceIds.add(device.getDeviceId()); 
  13.             deviceNames.add(device.getDeviceName()); 
  14.         }); 
  15.  
  16.         String devcieIdStr = deviceIds.get(0); 
  17.         return devcieIdStr; 
  18.     } 

6.獲取資源地址播放視頻

視頻播放參考的是軟通動力HarmonyOS學(xué)院的拜年視頻代碼,官方的demo和CadeLabs還沒跑通,時間有點來不及了,原諒我大段復(fù)制。

  1. //設(shè)置沉浸式狀態(tài)欄 
  2. getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS); 
  3. initPlayer(); 
  4.  
  5. //需要重寫兩個回調(diào):VideoSurfaceCallback 、VideoPlayerCallback 
  6. private void initPlayer() { 
  7.     sfProvider=(SurfaceProvider) findComponentById(ResourceTable.Id_surfaceProvider); 
  8.     //        image=(Image) findComponentById(ResourceTable.Id_img); 
  9.     sfProvider.getSurfaceOps().get().addCallback(new VideoSurfaceCallback()); 
  10.     // sfProvider.pinToZTop(boolean)--如果設(shè)置為true, 視頻控件會在最上層展示,但是設(shè)置為false時,雖然不在最上層展示,卻出現(xiàn)黑屏, 
  11.     // 需加上一行代碼:WindowManager.getInstance().getTopWindow().get().setTransparent(true); 
  12.     sfProvider.pinToZTop(true); 
  13.     //WindowManager.getInstance().getTopWindow().get().setTransparent(true); 
  14.     player=new Player(getContext()); 
  15.     //sfProvider添加監(jiān)聽事件 
  16.     sfProvider.setClickedListener(new Component.ClickedListener() { 
  17.         @Override 
  18.         public void onClick(Component component) { 
  19.             if(player.isNowPlaying()){ 
  20.                 //如果正在播放,就暫停 
  21.                 player.pause(); 
  22.                 //播放按鈕可見 
  23.                 image.setVisibility(Component.VISIBLE); 
  24.             }else { 
  25.                 //如果暫停,點擊繼續(xù)播放 
  26.                 player.play(); 
  27.                 //播放按鈕隱藏 
  28.                 image.setVisibility(Component.HIDE); 
  29.             } 
  30.         } 
  31.     }); 
  32. private class VideoSurfaceCallback implements SurfaceOps.Callback { 
  33.     @Override 
  34.     public void surfaceCreated(SurfaceOps surfaceOps) { 
  35.         HiLog.info(logLabel,"surfaceCreated() called."); 
  36.         if (sfProvider.getSurfaceOps().isPresent()) { 
  37.             Surface surface = sfProvider.getSurfaceOps().get().getSurface(); 
  38.             playLocalFile(surface); 
  39.         } 
  40.     } 
  41.     @Override 
  42.     public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) { 
  43.         HiLog.info(logLabel,"surfaceChanged() called."); 
  44.     } 
  45.     @Override 
  46.     public void surfaceDestroyed(SurfaceOps surfaceOps) { 
  47.         HiLog.info(logLabel,"surfaceDestroyed() called."); 
  48.     } 
  49. private void playLocalFile(Surface surface) { 
  50.     try { 
  51.         RawFileDescriptor filDescriptor = getResourceManager().getRawFileEntry(videoSource).openRawFileDescriptor(); 
  52.         Source source = new Source(filDescriptor.getFileDescriptor(),filDescriptor.getStartPosition(),filDescriptor.getFileSize()); 
  53.         player.setSource(source); 
  54.         player.setVideoSurface(surface); 
  55.         player.setPlayerCallback(new VideoPlayerCallback()); 
  56.         player.prepare(); 
  57.         sfProvider.setTop(0); 
  58.         player.play(); 
  59.     } catch (Exception e) { 
  60.         HiLog.info(logLabel,"playUrl Exception:" + e.getMessage()); 
  61.     } 

參考文章:

【軟通動力】SurfaceProvider實現(xiàn)視頻播放Demo-熱乎乎的拜年視頻-鴻蒙HarmonyOS技術(shù)社區(qū)-鴻蒙官方合作伙伴-51CTO.COM

鴻蒙應(yīng)用開發(fā)入門(六):頁面間跳轉(zhuǎn)-鴻蒙HarmonyOS技術(shù)社區(qū)-鴻蒙官方合作伙伴-51CTO.CO

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-08-23 10:49:02

鴻蒙HarmonyOS應(yīng)用

2021-08-23 11:03:54

鴻蒙HarmonyOS應(yīng)用

2023-08-22 21:39:25

2012-08-24 10:49:51

備份恢復(fù)

2020-08-26 06:04:25

信息泄露密鑰加密信息安全

2018-08-19 14:30:42

女性分析網(wǎng)站

2015-08-21 17:10:03

云安全

2021-08-15 19:00:14

算法floydDijkstra

2019-06-19 15:40:06

分布式鎖RedisJava

2021-05-17 09:32:18

分布式存儲問題數(shù)據(jù)

2011-08-06 23:25:49

筆記本導(dǎo)購

2013-08-13 09:24:25

程序員七夕節(jié)

2019-05-05 08:37:39

分布式PyTorchGPU

2021-05-28 09:52:00

鴻蒙HarmonyOS應(yīng)用

2019-08-07 17:17:54

華為

2018-08-17 16:30:20

2021-08-13 10:38:23

人工智能AI深度學(xué)習(xí)

2020-11-16 12:55:41

Redis分布式鎖Zookeeper

2019-10-10 09:16:34

Zookeeper架構(gòu)分布式
點贊
收藏

51CTO技術(shù)棧公眾號