日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
DistributedVideoPlayer分布式視頻播放器(一)

DistributedVideoPlayer 分布式視頻播放器(一)

作者:Buty9147 2021-10-19 14:27:07

開(kāi)發(fā)

前端

分布式 本示例是在官方Video Play Ability 模板基礎(chǔ)上做了擴(kuò)展開(kāi)發(fā),官方模板提供基本的視頻播放功能,并允許您在手機(jī)和電視之間傳輸視頻.

成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)涼州,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

介紹

本示例是在官方Video Play Ability 模板基礎(chǔ)上做了擴(kuò)展開(kāi)發(fā),官方模板提供基本的視頻播放功能,并允許您在手機(jī)和電視之間傳輸視頻.

應(yīng)用分為手機(jī)端(entry)和TV端(entrytv),以及一個(gè)依賴模塊(commonlib).

在示例的基礎(chǔ)之上,手機(jī)端增加了視頻播放列表功能,以及播放詳情頁(yè)和評(píng)論功能;手機(jī)端播放的視頻可以流轉(zhuǎn)到TV端,并實(shí)現(xiàn)遠(yuǎn)端遙控的功能。

內(nèi)容比較多,會(huì)分兩期給大家講解,本期文章主要講解的內(nèi)容是手機(jī)端部分:

1.實(shí)現(xiàn)一個(gè)視頻播放器 2.實(shí)現(xiàn)一個(gè)播放列表 3.實(shí)現(xiàn)一個(gè)評(píng)論功能.

[本文正在參與優(yōu)質(zhì)創(chuàng)作者激勵(lì)]

效果展示

搭建環(huán)境

安裝DevEco Studio,詳情請(qǐng)參考DevEco Studio下載。

設(shè)置DevEco Studio開(kāi)發(fā)環(huán)境,DevEco Studio開(kāi)發(fā)環(huán)境需要依賴于網(wǎng)絡(luò)環(huán)境,需要連接上網(wǎng)絡(luò)才能確保工具的正常使用,可以根據(jù)如下兩種情況來(lái)配置開(kāi)發(fā)環(huán)境:

如果可以直接訪問(wèn)Internet,只需進(jìn)行下載HarmonyOS SDK操作。

如果網(wǎng)絡(luò)不能直接訪問(wèn)Internet,需要通過(guò)代理服務(wù)器才可以訪問(wèn),請(qǐng)參考配置開(kāi)發(fā)環(huán)境。

下載源碼后,使用DevEco 打開(kāi)項(xiàng)目。

代碼結(jié)構(gòu)

Java代碼

  
 
 
 
  1. │  config.json
  2. ├─java
  3. │  └─com
  4. │      └─buty
  5. │          └─distributedvideoplayer
  6. │              │  MainAbility.java               
  7. │              │  MyApplication.java
  8. │              │
  9. │              ├─ability
  10. │              │      DevicesSelectAbility.java   
  11. │              │      MainAbilitySlice.java           #視頻播放列表頁(yè)
  12. │              │      SyncControlServiceAbility.java  
  13. │              │      VideoPlayAbility.java           #視頻播放Ability
  14. │              │      VideoPlayAbilitySlice.java      #視頻播放詳情和評(píng)論頁(yè)
  15. │              │
  16. │              ├─components
  17. │              │      EpisodesSelectionDialog.java    
  18. │              │      RemoteController.java
  19. │              │      VideoPlayerPlaybackButton.java  #播放按鈕組件
  20. │              │      VideoPlayerSlider.java          #播放時(shí)間進(jìn)度條
  21. │              │
  22. │              ├─constant
  23. │              │      Constants.java                  #常量
  24. │              │      ResolutionEnum.java             #分辨率枚舉
  25. │              │      RouteRegister.java              #自定義路由
  26. │              │
  27. │              ├─data
  28. │              │      VideoInfo.java                  #視頻基礎(chǔ)信息
  29. │              │      VideoInfoService.java           #視頻信息服務(wù),用于模擬數(shù)據(jù)
  30. │              │      Videos.java                     #視頻列表
  31. │              │ 
  32. │              ├─model
  33. │              │      CommentModel.java               #評(píng)論模型
  34. │              │      DeviceModel.java                
  35. │              │      ResolutionModel.java            #解析度模型
  36. │              │      VideoModel.java                 #視頻模型
  37. │              │
  38. │              ├─provider
  39. │              │      CommentItemProvider.java        #評(píng)論數(shù)據(jù)提供程序
  40. │              │      DeviceItemProvider.java      
  41. │              │      ResolutionItemProvider.java     #解析度數(shù)據(jù)提供程序
  42. │              │      VideoItemProvider.java          #視頻數(shù)據(jù)提供程序
  43. │              │
  44. │              └─utils
  45. │                      AppUtil.java                   #工具類
  46. │                      DateUtils.java

資源文件

  
 
 
 
  1. └─resources
  2.     ├─base
  3.     │  ├─element
  4.     │  │      color.json
  5.     │  │      float.json
  6.     │  │      strarray.json
  7.     │  │      string.json
  8.     │  │
  9.     │  ├─graphic
  10.     │  │      background_ability_control_bg.xml          
  11.     │  │      background_ability_control_middle.xml
  12.     │  │      background_ability_control_ok.xml
  13.     │  │      background_ability_devices.xml
  14.     │  │      background_ability_episodes.xml
  15.     │  │      background_button_click.xml
  16.     │  │      background_button_clicked.xml
  17.     │  │      background_episodes_item.xml
  18.     │  │      background_episodes_quality.xml
  19.     │  │      background_episodes_trailer.xml
  20.     │  │      background_slide_thumb.xml
  21.     │  │      background_switch_checked.xml
  22.     │  │      background_switch_empty.xml
  23.     │  │      background_switch_thumb.xml
  24.     │  │      background_switch_track.xml
  25.     │  │      list_divider.xml
  26.     │  │      shape_slider_thumb.xml
  27.     │  │
  28.     │  ├─layout
  29.     │  │      ability_main.xml                                #播放列表布局
  30.     │  │      comments_item.xml                               #單條評(píng)論布局
  31.     │  │      dialog_playlist.xml                     
  32.     │  │      dialog_resolution_list.xml
  33.     │  │      dialog_table_layout.xml
  34.     │  │      hm_sample_ability_video_box.xml                 #視頻播放組件頁(yè)
  35.     │  │      hm_sample_ability_video_comments.xml            #播放詳情布局頁(yè)
  36.     │  │      hm_sample_view_video_box_seek_bar_style1.xml    #播放進(jìn)度條布局
  37.     │  │      hm_sample_view_video_box_seek_bar_style2.xml
  38.     │  │      remote_ability_control.xml
  39.     │  │      remote_ability_episodes.xml
  40.     │  │      remote_ability_select_devices.xml
  41.     │  │      remote_ability_sound_equipment.xml
  42.     │  │      remote_device_item.xml
  43.     │  │      remote_episodes_item.xml
  44.     │  │      remote_video_quality_item.xml
  45.     │  │
  46.     │  ├─media
  47.     │  │      comments.png
  48.     │  │      great.png
  49.     │  │      icon.png
  50.     │  │      ic_anthology.png
  51.     │  │
  52.     │  └─profile
  53.     ├─en
  54.     │  └─element
  55.     │          string.json
  56.     │
  57.     ├─rawfile
  58.     │      videos.json                                      #模擬數(shù)據(jù)JSON文件
  59.     │
  60.     └─zh
  61.         └─element
  62.                 string.json

實(shí)現(xiàn)步驟

1.實(shí)現(xiàn)一個(gè)視頻播放器

引入對(duì)commonlib的依賴后,實(shí)現(xiàn)一個(gè)視頻播放器就很容易了.

entry的build.gradle 增加對(duì)commonlib的依賴

  
 
 
 
  1. dependencies {
  2.     implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
  3.     testImplementation 'junit:junit:4.13'
  4.     ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.200'
  5.     //引用commonlib 依賴
  6.     implementation project(path: ':commonlib')
  7. }

1.1.頁(yè)面布局 hm_sample_ability_video_comments.xml

添加一個(gè)VideoPlayerView組件

  
 
 
 
  1.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  2.     ohos:id="$+id:root_layout"
  3.     ohos:height="match_parent"
  4.     ohos:width="match_parent"
  5.     ohos:background_element="#FFFFFFFF"
  6.     ohos:orientation="vertical">
  7.     
  8.         ohos:id="$+id:video_view"
  9.         ohos:height="250vp"
  10.         ohos:width="match_parent"/>
  11. ...

1.2.Java代碼

獲取視頻組件對(duì)象后,只需要2步就可以了: 1.設(shè)置視頻資源路徑和描述; 2.設(shè)置播放器核心組件和自定義組件.幾行關(guān)鍵代碼 VideoPlayAbilitySlice.java

  
 
 
 
  1. /**
  2.  * 初始化播放器
  3.  */
  4. private void initPlayer() {
  5.     HiLog.debug(LABEL, "initPlayer");
  6.     //自定義視頻播放視圖組件
  7.     player = (VideoPlayerView) findComponentById(ResourceTable.Id_video_view);
  8.     if (player != null) {
  9.         //獲取視頻信息服務(wù)
  10.         videoService = new VideoInfoService(getContext());
  11.         //獲取當(dāng)前播放視頻的路徑
  12.         String path = videoService
  13.                 .getVideoInfoByIndex(currentPlayingIndex)
  14.                 .getResolutions()
  15.                 .get(currentPlayingResolutionIndex)
  16.                 .getUrl();
  17.         //視頻描述
  18.         String videoDesc = videoService.getVideoInfoByIndex(currentPlayingIndex).getVideoDesc();
  19.         HiLog.debug(LABEL, "videoDesc = " + videoDesc + " path = " + path);
  20.         if (path != null) {
  21.             //設(shè)置路徑和描述
  22.             player.setVideoPathAndTitle(path, videoDesc);
  23.             //視頻準(zhǔn)備完畢就播放并設(shè)置播放進(jìn)度條
  24.             player.setPlayerOnPreparedListener(
  25.                     () -> {
  26.                         player.start();
  27.                         //設(shè)置播放位置
  28.                         player.seekTo(currentPlayingPosition);
  29.                     });
  30.             //雙擊播放或暫停
  31.             player.setDoubleClickedListener(
  32.                     component -> {
  33.                         //是否在控制TV端
  34.                         if (remoteController != null && remoteController.isShown()) {
  35.                             return;
  36.                         }
  37.                         HiLog.debug(LABEL, "VideoPlayView double-click event");
  38.                         if (player.isPlaying()) {
  39.                             player.pause();
  40.                         } else {
  41.                             player.start();
  42.                         }
  43.                     });
  44.             //監(jiān)聽(tīng)播放錯(cuò)誤
  45.             player.setErrorListener(
  46.                     (errorType, errorCode) -> {
  47.                         ToastDialog toast = new ToastDialog(getContext());
  48.                         switch (errorType) {
  49.                             case HmPlayerAdapter.ERROR_LOADING_RESOURCE:
  50.                                 toast.setText(
  51.                                         AppUtil.getStringResource(
  52.                                                 getContext(), ResourceTable.String_media_file_loading_error));
  53.                                 break;
  54.                             case HmPlayerAdapter.ERROR_INVALID_OPERATION:
  55.                                 toast.setText(
  56.                                         AppUtil.getStringResource(
  57.                                                 getContext(), ResourceTable.String_invalid_operation));
  58.                                 break;
  59.                             default:
  60.                                 toast.setText(
  61.                                         AppUtil.getStringResource(
  62.                                                 getContext(), ResourceTable.String_undefined_error_type));
  63.                                 break;
  64.                         }
  65.                         getUITaskDispatcher().asyncDispatch(toast::show);
  66.                     });
  67.         }
  68.         //添加核心組件,播放時(shí)間進(jìn)度滑塊
  69.         addCoreComponent();
  70.         //添加自定義組件
  71.         addCustomComponent();
  72.         HiLog.debug(LABEL, "initPlayer finish");
  73.     }
  74. }

2.實(shí)現(xiàn)一個(gè)視頻播放列表功能

2.1.頁(yè)面布局 ability_main.xml

使用了DirectionalLayout布局組件,還有ScrollView和ListContainer組件

  
 
 
 
  1.     xmlns:ohos="http://schemas.huawei.com/res/ohos"
  2.     ohos:id="$+id:ability_main_root"
  3.     ohos:height="match_parent"
  4.     ohos:width="match_parent"
  5.     ohos:orientation="vertical">
  6.     
  7.         ohos:id="$+id:ability_main_titlebar"
  8.         ohos:height="match_content"
  9.         ohos:width="match_parent"
  10.         ohos:background_element="#B0B0B0"
  11.         ohos:orientation="horizontal"
  12.         ohos:padding="10vp">
  13.         
  14.             ohos:height="match_parent"
  15.             ohos:width="match_content"
  16.             ohos:weight="1">
  17.             
  18.                 ohos:id="$+id:tag_favorite"
  19.                 ohos:height="match_content"
  20.                 ohos:width="match_parent"
  21.                 ohos:text="關(guān)注"
  22.                 ohos:text_size="$float:normal_text_size_15"
  23.                 />
  24.         
  25.         
  26.             ohos:height="match_content"
  27.             ohos:width="match_content"
  28.             ohos:weight="1">
  29.             
  30.                 ohos:id="$+id:tag_support"
  31.                 ohos:height="match_content"
  32.                 ohos:width="match_content"
  33.                 ohos:text="推薦"
  34.                 ohos:text_size="$float:normal_text_size_15"
  35.                 />
  36.         
  37.         
  38.             ohos:height="match_content"
  39.             ohos:width="match_content"
  40.             ohos:weight="1">
  41.             
  42.                 ohos:id="$+id:tag_movie"
  43.                 ohos:height="match_content"
  44.                 ohos:width="match_content"
  45.                 ohos:element_end="$id:favorite"
  46.                 ohos:text="電影"
  47.                 ohos:text_color="#1C6AE9"
  48.                 ohos:text_size="$float:normal_text_size_15"
  49.                 ohos:text_weight="600"/>
  50.             
  51.                 ohos:id="$+id:device_item_divider"
  52.                 ohos:height="2vp"
  53.                 ohos:width="30vp"
  54.                 ohos:background_element="$graphic:list_divider"/>
  55.         
  56.         
  57.             ohos:height="match_content"
  58.             ohos:width="match_content"
  59.             ohos:weight="1">
  60.             
  61.                 ohos:id="$+id:tag_live"
  62.                 ohos:height="match_content"
  63.                 ohos:width="match_parent"
  64.                 ohos:text="直播"
  65.                 ohos:text_size="$float:normal_text_size_15"
  66.                 />
  67.         
  68.         
  69.             ohos:height="match_content"
  70.             ohos:width="match_content"
  71.             ohos:weight="1">
  72.             
  73.                 ohos:id="$+id:tag_tv"
  74.                 ohos:height="match_content"
  75.                 ohos:width="match_parent"
  76.                 ohos:text="電視"
  77.                 ohos:text_size="$float:normal_text_size_15"
  78.                 />
  79.         
  80.     
  81.     
  82.         ohos:height="match_parent"
  83.         ohos:width="match_parent"
  84.         ohos:id="$+id:video_list_scroll" >
  85.         
  86.             ohos:id="$+id:videos_container"
  87.             ohos:height="match_parent"
  88.             ohos:width="match_parent">
  89.         
  90.     

2.2.Java代碼

申請(qǐng)用戶敏感權(quán)限授權(quán) MainAbility.java

  
 
 
 
  1. /**
  2.  * Entry to the main interface of the program
  3.  */
  4. public class MainAbility extends Ability {
  5.     private static final int REQUEST_CODE = 1;
  6.     //讀寫(xiě)媒體權(quán)限
  7.     private final String[] permissionLists
  8.             = new String[]{"ohos.permission.READ_MEDIA", "ohos.permission.WRITE_MEDIA"};
  9.     @Override
  10.     public void onStart(Intent intent) {
  11.         super.onStart(intent);
  12.         super.setUIContent(ResourceTable.Layout_ability_main);
  13.         super.setMainRoute(MainAbilitySlice.class.getName());
  14.         //申請(qǐng)授權(quán)
  15.         verifyPermissions();
  16.     }
  17.     private void verifyPermissions() {
  18.         for (String permissionList : permissionLists) {
  19.             int result = verifySelfPermission(permissionList);
  20.             if (result != IBundleManager.PERMISSION_GRANTED) {
  21.                 requestPermissionsFromUser(permissionLists, REQUEST_CODE);
  22.             }
  23.         }
  24.     }
  25. ...

通過(guò)VideoInfoService讀取videos.json文件初始化視頻容器列表MainAbilitySlice.java

  
 
 
 
  1. public class MainAbilitySlice extends AbilitySlice {
  2.     public static final HiLogLabel LABEL = new HiLogLabel(0, 0, "=>MainAbilitySlice");
  3.     private VideoInfoService videoService;
  4.     @Override
  5.     protected void onStart(Intent intent) {
  6.         super.onStart(intent);
  7.         //加載視頻播放器頁(yè)面
  8.         super.setUIContent(ResourceTable.Layout_ability_main);
  9.         initVideoContainer();
  10.     }
  11.     /**
  12.      * 模擬數(shù)據(jù)
  13.      * 初始化視頻容器列表
  14.      */
  15.     private void initVideoContainer() {
  16.         HiLog.debug(LABEL, "initVideoContainer");
  17.         List videos = new ArrayList<>();
  18.         //獲取視頻信息服務(wù)
  19.         videoService = new VideoInfoService(getContext());
  20.         for (int i = 0; i < 7; i++) {
  21.             VideoModel video = new VideoModel();
  22.             video.setComments(new Random().nextInt(1000));
  23.             video.setFavorites(new Random().nextInt(1000));
  24.             video.setGreats(new Random().nextInt(10000));
  25.             VideoInfo videoInfo = videoService.getVideoInfoByIndex(i);
  26.             videoInfo.setIndex(i);
  27.             video.setVideoInfo(videoInfo);
  28.             videos.add(video);
  29.         }
  30.         ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_videos_container);
  31.         //容器綁定數(shù)據(jù)提供程序
  32.         VideoItemProvider provider = new VideoItemProvider(this, videos, this);
  33.         listContainer.setItemProvider(provider);
  34.     }
  35. }

 視頻容器列表數(shù)據(jù)提供程序 VideoItemProvider.java

  
 
 
 
  1. public class VideoItemProvider extends BaseItemProvider {
  2.     private static final HiLogLabel LABEL = new HiLogLabel(0, 0, "=>VideoItemProvider");
  3.     private final Context context;
  4.     private final List list;
  5.     private AbilitySlice abilitySlice;
  6.     //當(dāng)前播放視頻分辨率索引
  7.     private int currentPlayingResolutionIndex = 0;
  8.     /**
  9.      * Initialization
  10.      */
  11.     public VideoItemProvider(Context context, List list, AbilitySlice abilitySlice) {
  12.         HiLog.debug(LABEL, "VideoItemProvider");
  13.         this.context = context;
  14.         this.list = list;
  15.         this.abilitySlice = abilitySlice;
  16.     }
  17.     public Context getContext() {
  18.         return context;
  19.     }
  20.     @Override
  21.     public int getCount() {
  22.         return list == null ? 0 : list.size();
  23.     }
  24.     @Override
  25.     public Object getItem(int position) {
  26.         if (list != null && position >= 0 && position < list.size()) {
  27.             return list.get(position);
  28.         }
  29.         return new VideoModel();
  30.     }
  31.  
    標(biāo)題名稱:DistributedVideoPlayer分布式視頻播放器(一)
    鏈接URL:http://m.5511xx.com/article/cdoopeh.html