zl程序教程

您现在的位置是:首页 >  其他

当前栏目

用鸿蒙的分布式助力七夕

2023-03-15 22:45:38 时间

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

在情人节后,为之前的B站卡片项目增加一个隐藏功能。如果升级了最新的B站服务卡片,那么当桌面上添加头像卡片时,只要点击头像,就会看到下图的效果。一个应用鸿蒙分布式能力的小功能。

视频预览地址:https://harmonyos.51cto.com/show/7762

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

1.添加一个播放页

比如PlayerSlice,这个页面用来实现视频的播放。

用鸿蒙的分布式助力七夕-鸿蒙HarmonyOS技术社区

2.为头像卡片添加点击事件

当点击卡片上的头像时实现页面跳转,代码如下

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中设置跳转到刚才新建的播放页面。

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中提取参数params,如果播放页是服务卡片拉起的,得到true。如果是分布式拉起的得到false。

  1. params = intent.getStringParam("params");//从intent中获取 跳转事件定义的params字段的值 
  2. if(params.equals("true")){ 
  3.     Intent intent0 = new Intent(); 
  4.     Operation op = new Intent.OperationBuilder() 
  5.         .withDeviceId(DistributedUtils.getDeviceId())//参数1.是否跨设备,空,不跨设备 
  6.         .withBundleName("com.liangzili.demos")//参数2.在config.json中的bundleName 
  7.         .withAbilityName("com.liangzili.demos.Player")//参数3.要跳转的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.申请分布式拉起页面权限

如果params就调用分布式拉起页面,得提前为应用获取权限。

在app首次启动时提醒用户获取分布式权限。

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

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

5.获取远端设备ID

要拉起远端设备上的页面,得先获取设备的ID。

  1. public class DistributedUtils { 
  2.     public static String getDeviceId(){ 
  3.         //获取在线设备列表,getDeviceList拿到的设备不包含本机。 
  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);    //提取设备Id 
  10.         List<String> deviceNames = new ArrayList<>(deviceNum);  //提取设备名 
  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学院的拜年视频代码,官方的demo和CadeLabs还没跑通,时间有点来不及了,原谅我大段复制。

  1. //设置沉浸式状态栏 
  2. getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS); 
  3. initPlayer(); 
  4.  
  5. //需要重写两个回调: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)--如果设置为true, 视频控件会在最上层展示,但是设置为false时,虽然不在最上层展示,却出现黑屏, 
  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添加监听事件 
  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.                 //如果暂停,点击继续播放 
  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实现视频播放Demo-热乎乎的拜年视频-鸿蒙HarmonyOS技术社区-鸿蒙官方合作伙伴-51CTO.COM

鸿蒙应用开发入门(六):页面间跳转-鸿蒙HarmonyOS技术社区-鸿蒙官方合作伙伴-51CTO.CO

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com