DistributedVideoPlayer分布式视频播放器(一)

DistributedVideoPlayer 分布式视频播放器(一)

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

开发

前端

分布式 本示例是在官方Video Play Ability 模板基础上做了扩展开发,官方模板提供基本的视频播放功能,并允许您在手机和电视之间传输视频.

成都创新互联公司主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务凉州,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

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

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

介绍

本示例是在官方Video Play Ability 模板基础上做了扩展开发,官方模板提供基本的视频播放功能,并允许您在手机和电视之间传输视频.

应用分为手机端(entry)和TV端(entrytv),以及一个依赖模块(commonlib).

在示例的基础之上,手机端增加了视频播放列表功能,以及播放详情页和评论功能;手机端播放的视频可以流转到TV端,并实现远端遥控的功能。

内容比较多,会分两期给大家讲解,本期文章主要讲解的内容是手机端部分:

1.实现一个视频播放器 2.实现一个播放列表 3.实现一个评论功能.

[本文正在参与优质创作者激励]

效果展示

搭建环境

安装DevEco Studio,详情请参考DevEco Studio下载。

设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:

如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。

如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。

下载源码后,使用DevEco 打开项目。

代码结构

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           #视频播放列表页
  12. │              │      SyncControlServiceAbility.java  
  13. │              │      VideoPlayAbility.java           #视频播放Ability
  14. │              │      VideoPlayAbilitySlice.java      #视频播放详情和评论页
  15. │              │
  16. │              ├─components
  17. │              │      EpisodesSelectionDialog.java    
  18. │              │      RemoteController.java
  19. │              │      VideoPlayerPlaybackButton.java  #播放按钮组件
  20. │              │      VideoPlayerSlider.java          #播放时间进度条
  21. │              │
  22. │              ├─constant
  23. │              │      Constants.java                  #常量
  24. │              │      ResolutionEnum.java             #分辨率枚举
  25. │              │      RouteRegister.java              #自定义路由
  26. │              │
  27. │              ├─data
  28. │              │      VideoInfo.java                  #视频基础信息
  29. │              │      VideoInfoService.java           #视频信息服务,用于模拟数据
  30. │              │      Videos.java                     #视频列表
  31. │              │ 
  32. │              ├─model
  33. │              │      CommentModel.java               #评论模型
  34. │              │      DeviceModel.java                
  35. │              │      ResolutionModel.java            #解析度模型
  36. │              │      VideoModel.java                 #视频模型
  37. │              │
  38. │              ├─provider
  39. │              │      CommentItemProvider.java        #评论数据提供程序
  40. │              │      DeviceItemProvider.java      
  41. │              │      ResolutionItemProvider.java     #解析度数据提供程序
  42. │              │      VideoItemProvider.java          #视频数据提供程序
  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                               #单条评论布局
  31.     │  │      dialog_playlist.xml                     
  32.     │  │      dialog_resolution_list.xml
  33.     │  │      dialog_table_layout.xml
  34.     │  │      hm_sample_ability_video_box.xml                 #视频播放组件页
  35.     │  │      hm_sample_ability_video_comments.xml            #播放详情布局页
  36.     │  │      hm_sample_view_video_box_seek_bar_style1.xml    #播放进度条布局
  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                                      #模拟数据JSON文件
  59.     │
  60.     └─zh
  61.         └─element
  62.                 string.json

实现步骤

1.实现一个视频播放器

引入对commonlib的依赖后,实现一个视频播放器就很容易了.

entry的build.gradle 增加对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.页面布局 hm_sample_ability_video_comments.xml

添加一个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代码

获取视频组件对象后,只需要2步就可以了: 1.设置视频资源路径和描述; 2.设置播放器核心组件和自定义组件.几行关键代码 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.         //获取视频信息服务
  10.         videoService = new VideoInfoService(getContext());
  11.         //获取当前播放视频的路径
  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.             //设置路径和描述
  22.             player.setVideoPathAndTitle(path, videoDesc);
  23.             //视频准备完毕就播放并设置播放进度条
  24.             player.setPlayerOnPreparedListener(
  25.                     () -> {
  26.                         player.start();
  27.                         //设置播放位置
  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.             //监听播放错误
  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.         //添加核心组件,播放时间进度滑块
  69.         addCoreComponent();
  70.         //添加自定义组件
  71.         addCustomComponent();
  72.         HiLog.debug(LABEL, "initPlayer finish");
  73.     }
  74. }

2.实现一个视频播放列表功能

2.1.页面布局 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="关注"
  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代码

申请用户敏感权限授权 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.     //读写媒体权限
  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.         //申请授权
  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. ...

通过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.         //加载视频播放器页面
  8.         super.setUIContent(ResourceTable.Layout_ability_main);
  9.         initVideoContainer();
  10.     }
  11.     /**
  12.      * 模拟数据
  13.      * 初始化视频容器列表
  14.      */
  15.     private void initVideoContainer() {
  16.         HiLog.debug(LABEL, "initVideoContainer");
  17.         List videos = new ArrayList<>();
  18.         //获取视频信息服务
  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.         //容器绑定数据提供程序
  32.         VideoItemProvider provider = new VideoItemProvider(this, videos, this);
  33.         listContainer.setItemProvider(provider);
  34.     }
  35. }

 视频容器列表数据提供程序 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.     //当前播放视频分辨率索引
  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.  

    文章名称:DistributedVideoPlayer分布式视频播放器(一)
    网页链接:http://www.mswzjz.cn/qtweb/news47/300397.html

    攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能