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

HarmonyOS 分布式之仿抖音應(yīng)用

開發(fā) 分布式 OpenHarmony
使用Java UI開發(fā)分布式仿抖音應(yīng)用,上下滑動切換視頻,評論功能,設(shè)備遷移功能:記錄播放的視頻頁和進度、評論數(shù)據(jù)。

[[430075]]

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

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

https://harmonyos.51cto.com

項目介紹

使用Java UI開發(fā)分布式仿抖音應(yīng)用,上下滑動切換視頻,評論功能,設(shè)備遷移功能:記錄播放的視頻頁和進度、評論數(shù)據(jù)。

效果演示

1.上下滑動切換視頻、點擊遷移圖標,彈框選擇在線的設(shè)備,完成視頻數(shù)據(jù)的遷移。

2.點擊評論圖標查看評論,編輯評論內(nèi)容并發(fā)送。點擊遷移圖標,彈框選擇在線的設(shè)備,完成評論數(shù)據(jù)的遷移。

項目結(jié)構(gòu)

主要代碼

1、上下滑動頁面

頁面切換用到系統(tǒng)組件PageSlider,默認左右切換,設(shè)置為上下方向:setOrientation(Component.VERTICAL);

  1. import ohos.aafwk.ability.AbilitySlice; 
  2. import ohos.aafwk.content.Intent; 
  3. import ohos.agp.components.*; 
  4.  
  5. import java.util.ArrayList; 
  6. import java.util.List; 
  7.  
  8. public class MainAbilitySlice extends AbilitySlice { 
  9.     @Override 
  10.     public void onStart(Intent intent) { 
  11.         super.onStart(intent); 
  12.         super.setUIContent(ResourceTable.Layout_ability_main); 
  13.         // 查找滑動頁面組件 
  14.         PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pageSlider); 
  15.         // 設(shè)置滑動方向為上下滑動 
  16.         pageSlider.setOrientation(Component.VERTICAL); 
  17.         // 集合測試數(shù)據(jù) 
  18.         List<String> listData=new ArrayList<>(); 
  19.         listData.add("第一頁"); 
  20.         listData.add("第二頁"); 
  21.         listData.add("第三頁"); 
  22.          
  23.         // 設(shè)置頁面適配器 
  24.         pageSlider.setProvider(new PageSliderProvider() { 
  25.             /** 
  26.              * 獲取當(dāng)前適配器中可用視圖的數(shù)量 
  27.              */ 
  28.             @Override 
  29.             public int getCount() { 
  30.                 return listData.size(); 
  31.             } 
  32.             /** 
  33.              * 創(chuàng)建頁面 
  34.              */ 
  35.             @Override 
  36.             public Object createPageInContainer(ComponentContainer container, int position) { 
  37.                 // 查找布局 
  38.                 Component component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item_page, nullfalse); 
  39.                 Text textContent = (Text) component.findComponentById(ResourceTable.Id_text_item_page_content); 
  40.                 // 設(shè)置數(shù)據(jù) 
  41.                 textContent.setText(listData.get(position)); 
  42.                 // 添加到容器中 
  43.                 container.addComponent(component); 
  44.                 return component; 
  45.             } 
  46.             /** 
  47.              * 銷毀頁面 
  48.              */ 
  49.             @Override 
  50.             public void destroyPageFromContainer(ComponentContainer container, int position, Object object) { 
  51.                 // 從容器中移除 
  52.                 container.removeComponent((Component) object); 
  53.             } 
  54.             /** 
  55.              * 檢查頁面是否與對象匹配 
  56.              */ 
  57.             @Override 
  58.             public boolean isPageMatchToObject(Component page, Object object) { 
  59.                 return true
  60.             } 
  61.         }); 
  62.  
  63.         // 添加頁面改變監(jiān)聽器 
  64.         pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() { 
  65.             /** 
  66.              * 頁面滑動時調(diào)用 
  67.              */ 
  68.             @Override 
  69.             public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {} 
  70.             /** 
  71.              * 當(dāng)頁面滑動狀態(tài)改變時調(diào)用 
  72.              */ 
  73.             @Override 
  74.             public void onPageSlideStateChanged(int state) {} 
  75.             /** 
  76.              * 選擇新頁面時回調(diào) 
  77.              */ 
  78.             @Override 
  79.             public void onPageChosen(int itemPos) { 
  80.                 // 在此方法下,切換頁面獲取當(dāng)前頁面的視頻源,進行播放 
  81.                 String data = listData.get(itemPos); 
  82.             } 
  83.         }); 
  84.     } 

2、播放視頻

視頻播放使用Player,視頻畫面窗口顯示使用SurfaceProvider。

  1. import ohos.aafwk.ability.AbilitySlice; 
  2. import ohos.aafwk.content.Intent; 
  3. import ohos.agp.components.surfaceprovider.SurfaceProvider; 
  4. import ohos.agp.graphics.SurfaceOps; 
  5. import ohos.global.resource.RawFileDescriptor; 
  6. import ohos.media.common.Source; 
  7. import ohos.media.player.Player; 
  8.  
  9. import java.io.IOException; 
  10.  
  11. public class MainAbilitySlice extends AbilitySlice { 
  12.     // 視頻路徑 
  13.     private final String videoPath = "resources/rawfile/HarmonyOS.mp4"
  14.     // 播放器 
  15.     private Player mPlayer; 
  16.  
  17.     @Override 
  18.     public void onStart(Intent intent) { 
  19.         super.onStart(intent); 
  20.         super.setUIContent(ResourceTable.Layout_ability_main); 
  21.         // 初始化播放器 
  22.         mPlayer = new Player(getContext()); 
  23.         // 查找視頻窗口組件 
  24.         SurfaceProvider surfaceProvider = (SurfaceProvider) findComponentById(ResourceTable.Id_surfaceProvider); 
  25.         // 設(shè)置視頻窗口在頂層 
  26.         surfaceProvider.pinToZTop(true); 
  27.         // 設(shè)置視頻窗口操作監(jiān)聽 
  28.         if (surfaceProvider.getSurfaceOps().isPresent()) { 
  29.             surfaceProvider.getSurfaceOps().get().addCallback(new SurfaceOps.Callback() { 
  30.                 /** 
  31.                  * 創(chuàng)建視頻窗口 
  32.                  */ 
  33.                 @Override 
  34.                 public void surfaceCreated(SurfaceOps holder) { 
  35.                     try { 
  36.                         RawFileDescriptor fileDescriptor = getResourceManager().getRawFileEntry(videoPath).openRawFileDescriptor(); 
  37.                         Source source = new Source(fileDescriptor.getFileDescriptor(), 
  38.                                 fileDescriptor.getStartPosition(), 
  39.                                 fileDescriptor.getFileSize() 
  40.                         ); 
  41.                         // 設(shè)置媒體文件 
  42.                         mPlayer.setSource(source); 
  43.                         // 設(shè)置播放窗口 
  44.                         mPlayer.setVideoSurface(holder.getSurface()); 
  45.                         // 循環(huán)播放 
  46.                         mPlayer.enableSingleLooping(true); 
  47.                         // 準備播放環(huán)境并緩沖媒體數(shù)據(jù) 
  48.                         mPlayer.prepare(); 
  49.                         // 開始播放 
  50.                         mPlayer.play(); 
  51.                     } catch (IOException e) { 
  52.                         e.printStackTrace(); 
  53.                     } 
  54.  
  55.                 } 
  56.                 /** 
  57.                  * 視頻窗口改變 
  58.                  */ 
  59.                 @Override 
  60.                 public void surfaceChanged(SurfaceOps holder, int format, int width, int height) {} 
  61.                 /** 
  62.                  * 視頻窗口銷毀 
  63.                  */ 
  64.                 @Override 
  65.                 public void surfaceDestroyed(SurfaceOps holder) {} 
  66.             }); 
  67.         } 
  68.     } 
  69.  
  70.     @Override 
  71.     protected void onStop() { 
  72.         super.onStop(); 
  73.         // 頁面銷毀,釋放播放器 
  74.         if (mPlayer != null) { 
  75.             mPlayer.stop(); 
  76.             mPlayer.release(); 
  77.         } 
  78.     } 

3、跨設(shè)備遷移示例

跨設(shè)備遷移使用IAbilityContinuation接口。

1、在entry下的config.json配置權(quán)限

  1. "reqPermissions": [ 
  2.       { 
  3.         "name""ohos.permission.DISTRIBUTED_DATASYNC" 
  4.       }, 
  5.       { 
  6.         "name""ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" 
  7.       }, 
  8.       { 
  9.         "name""ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" 
  10.       } 
  11.     ] 

2、實現(xiàn)IAbilityContinuation接口,說明:一個應(yīng)用可能包含多個Page,僅需要在支持遷移的Page中通過以下方法實現(xiàn)IAbilityContinuation接口。同時,此Page所包含的所有AbilitySlice也需要實現(xiàn)此接口。

  1. import ohos.aafwk.ability.AbilitySlice; 
  2. import ohos.aafwk.ability.IAbilityContinuation; 
  3. import ohos.aafwk.content.Intent; 
  4. import ohos.aafwk.content.IntentParams; 
  5. import ohos.agp.components.Button; 
  6. import ohos.agp.components.Text; 
  7. import ohos.bundle.IBundleManager; 
  8. import ohos.distributedschedule.interwork.DeviceInfo; 
  9. import ohos.distributedschedule.interwork.DeviceManager; 
  10.  
  11. import java.util.List; 
  12.  
  13. public class MainAbilitySlice extends AbilitySlice implements IAbilityContinuation { 
  14.     private String data = ""
  15.     String PERMISSION = "ohos.permission.DISTRIBUTED_DATASYNC"
  16.  
  17.     @Override 
  18.     public void onStart(Intent intent) { 
  19.         super.onStart(intent); 
  20.         super.setUIContent(ResourceTable.Layout_ability_main); 
  21.         // 申請權(quán)限 
  22.         if (verifySelfPermission(PERMISSION) != IBundleManager.PERMISSION_GRANTED) { 
  23.             requestPermissionsFromUser(new String[]{PERMISSION}, 0); 
  24.         } 
  25.         Button button = (Button)findComponentById(ResourceTable.Id_button); 
  26.         Text text = (Text)findComponentById(ResourceTable.Id_text); 
  27.          
  28.         // 點擊遷移 
  29.         button.setClickedListener(component -> { 
  30.             // 查詢分布式網(wǎng)絡(luò)中所有在線設(shè)備(不包括本地設(shè)備)的信息。 
  31.             List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); 
  32.             if (deviceList.size()>0) { 
  33.                 // 啟動遷移,指定的設(shè)備ID 
  34.                 continueAbility(deviceList.get(0).getDeviceId()); 
  35.             } 
  36.         }); 
  37.         // 顯示遷移的數(shù)據(jù) 
  38.         text.setText("遷移的數(shù)據(jù):"+data); 
  39.     } 
  40.     /** 
  41.      * 啟動遷移時首次調(diào)用此方法 
  42.      * @return 是否進行遷移 
  43.      */ 
  44.     @Override 
  45.     public boolean onStartContinuation() { 
  46.         return true
  47.     } 
  48.     /** 
  49.      * 遷移時存入數(shù)據(jù) 
  50.      */ 
  51.     @Override 
  52.     public boolean onSaveData(IntentParams intentParams) { 
  53.         intentParams.setParam("data","測試數(shù)據(jù)"); 
  54.         return true
  55.     } 
  56.     /** 
  57.      * 獲取遷移存入的數(shù)據(jù),在生命周期的onStart之前執(zhí)行 
  58.      */ 
  59.     @Override 
  60.     public boolean onRestoreData(IntentParams intentParams) { 
  61.         data= (String) intentParams.getParam("data"); 
  62.         return true
  63.     } 
  64.     /** 
  65.      * 遷移完成 
  66.      */ 
  67.     @Override 
  68.     public void onCompleteContinuation(int i) {} 

根據(jù)上面的核心代碼示例,了解實現(xiàn)原理,接下來便可以結(jié)合實際需求完善功能了。

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

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

https://harmonyos.51cto.com

 

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

2021-11-16 09:38:10

鴻蒙HarmonyOS應(yīng)用

2018-07-17 08:14:22

分布式分布式鎖方位

2021-12-13 11:07:10

鴻蒙HarmonyOS應(yīng)用

2022-03-06 21:43:05

Citus架構(gòu)PostgreSQL

2019-02-13 13:41:07

MemCache分布式HashMap

2019-09-26 15:43:52

Hadoop集群防火墻

2021-05-28 09:52:00

鴻蒙HarmonyOS應(yīng)用

2018-12-14 10:06:22

緩存分布式系統(tǒng)

2021-01-21 09:45:36

鴻蒙HarmonyOS分布式

2021-12-10 15:06:56

鴻蒙HarmonyOS應(yīng)用

2021-12-02 10:11:44

鴻蒙HarmonyOS應(yīng)用

2019-10-10 09:16:34

Zookeeper架構(gòu)分布式

2023-01-09 11:23:03

系統(tǒng)

2023-05-29 14:07:00

Zuul網(wǎng)關(guān)系統(tǒng)

2017-09-01 05:35:58

分布式計算存儲

2019-06-19 15:40:06

分布式鎖RedisJava

2021-07-22 10:20:21

鴻蒙HarmonyOS應(yīng)用

2019-07-04 15:13:16

分布式緩存Redis

2019-12-26 08:59:20

Redis主從架構(gòu)

2021-12-28 17:03:29

數(shù)據(jù)質(zhì)量分布式
點贊
收藏

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