• Home
Name Date Size #Lines LOC

..--

AppScope/06-May-2025-3734

entry/06-May-2025-2,8722,533

hvigor/06-May-2025-2019

screenshots/device/06-May-2025-

signature/06-May-2025-2928

.gitignoreD06-May-2025152 1010

README.mdD06-May-20251.5 KiB3216

README_zh.mdD06-May-20257.4 KiB12184

build-profile.json5D06-May-20251.1 KiB4442

hvigorfile.jsD06-May-2025168 21

oh-package.json5D06-May-2025861 2826

ohosTest.mdD06-May-20252.4 KiB2117

README.md

1# Distributed Music Player
2
3### Introduction
4
5In this sample, **fileIo** is used to obtain an audio file; **AudioPlayer** is used to play music, pause the playback, and play the next or previous song; **DeviceManager** is used to display the distributed device list and hop music playback across devices. The display effect is as follows:
6
7![](./screenshots/device/distributedMusicPlayer_en.png)
8
9### Concepts
10
11Audio playback: The media subsystem provides audio and video services and implements audio playback by using **AudioPlayer**.
12
13Data hop: The distributed data management module implements collaboration between databases of different devices for applications. The APIs provided by distributed data management can be used to save data to the distributed database and perform operations such as adding, deleting, modifying, and querying data in the distributed database.
14
15### Required Permissions
16
17ohos.permission.DISTRIBUTED_DATASYNC
18
19### Usage
20
211. Play music. Touch the buttons on the music player to play music, pause the playback, and play the next or previous music clip.
22
232. Play music across devices. On the Super Device formed by multiple networked devices, touch the **Hop** button and select a device to play the music on the peer device.
24
25### Constraints
26
27- This sample can only be run on standard-system devices.
28
29- This sample is based on the stage model, which is supported from API version 9.
30
31- This sample requires DevEco Studio 3.0 Beta4 (Build Version: 3.0.0.992, built on July 14, 2022) to compile and run.
32

README_zh.md

1# 分布式音乐播放
2
3### 介绍
4
5本示例使用fileIo获取指定音频文件,并通过AudioPlayer完成了音乐的播放完成了基本的音乐播放、暂停、上一曲、下一曲功能;并使用DeviceManager完成了分布式设备列表的显示和分布式能力完成了音乐播放状态的跨设备分享。
6
7本示例用到了与用户进行交互的Ability的能力接口[@ohos.ability.featureAbility](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-ability-kit/js-apis-ability-featureAbility.md)
8
9文件存储管理能力接口[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md)
10
11屏幕属性接口[@ohos.display](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-display.md)
12
13媒体查询接口[@ohos.mediaquery](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-mediaquery.md)14
15分布式数据管理接口[@ohos.data.distributedData](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkdata/js-apis-distributed-data.md)
16
17音视频相关媒体业务能力接口[@ohos.multimedia.media](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-media-kit/js-apis-media.md)
18
19分布式设备管理能力接口(设备管理),实现设备之间的kvStore对象的数据传输交互[@ohos.distributedDeviceManager](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md)
20
21### 效果预览
22| 首页                              |
23|--------------------------------------|
24|![](./screenshots/device/distributedMusicPlayer.png)|
25
26使用说明
27
281.**音乐播放**,点击**播放**、**暂停**、上**一曲**、下**一曲**按钮可以对音乐进行操作。
29
302.**跨设备分享**,组网并且双端均已授权条件下,点击**分享**按钮,选择设备,拉起对端设备上的音乐,并将本端的播放状态同步到对端上。
31
323.**跨设备停止分享**,分享成功前提条件下,点击**停止分享**按钮,将对端设备拉起的音乐应用停止退出。
33
34#### 相关概念
35
36音频播放:媒体子系统包含了音视频相关媒体业务,通过AudioPlayer实现音频播放的能力。
37
38数据分享:分布式数据管理为应用程序提供不同设备间数据库的分布式协同能力。通过调用分布式数据各个接口,应用程序可将数据保存到分布式数据库中,并可对分布式数据库中的数据进行增/删/改/查等各项操作。
39
40### 工程目录
41```
42entry/src/main/ets/
43|---pages
44|   |---index.ets                           // 首页
45|---model
46|   |---PlayerModel.ts                      // 播放器模块
47|   |---KvStoreModel.ts                     // kvstore对象操作类
48|   |---RemoteDeviceModel.ts                // 远程设备操作类
49|---common
50|   |---MusicSharedDefinition.ts            // 定义音乐播放器状态
51|   |---DeviceDialog.ets                    // 分布式设备列表弹窗
52
53```
54
55### 具体实现
56在分布式音乐播放器中,分布式设备管理包含了分布式设备搜索、分布式设备列表弹窗、远端设备拉起三部分。
57首先在分布式组网内搜索设备,然后把设备展示到分布式设备列表弹窗中,最后根据用户的选择拉起远端设备。
58#### 分布式设备搜索
59通过SUBSCRIBE_ID搜索分布式组网内的远端设备,详见registerDeviceListCallback(callback) {}模块[源码参考](entry/src/main/ets/model/RemoteDeviceModel.ts )。
60#### 分布式设备列表弹窗
61使用@CustomDialog弹出分布式设备列表弹窗,参考首页。[源码参考](entry/src/main/ets/common/DeviceDialog.ets )。
62#### 远端设备拉起
63通过startAbility(deviceId)方法拉起远端设备的包,[源码参考](entry/src/main/ets/pages/Index.ets )。
64#### 分布式数据管理
65(1) 管理分布式数据库
66创建一个KVManager对象实例,用于管理分布式数据库对象。通过distributedData.createKVManager(config),并通过指定Options和storeId,创建并获取KVStore数据库,并通过Promise方式返回,此方法为异步方法,例如this.kvManager.getKVStore(STORE_ID, options).then((store) => {})
67(2) 订阅分布式数据变化
68通过订阅分布式数据库所有(本地及远端)数据变化实现数据协同[源码参考](entry/src/main/ets/pages/Index.ets )。
69
70#### 跨设备播放操作
71(1)分布式设备管理器绑定应用包
72deviceManager.createDeviceManager('ohos.samples.distributedmusicplayer') [源码参考](entry/src/main/ets/model/RemoteDeviceModel.ts )。
73(2) 初始化播放器
74构造函数中通过'@ohos.multimedia.media'组件对播放器进行实例化,并调用播放器初始化函数,通过播放器的on函数,监听error、finish、timeUpdate
75(3) 同步当前播放数据
76播放器通过调用selectedIndexChange(),将当前播放的资源、时间、以及播放状态同步给选中的设备。
77(4) 接收当前播放数据
78播放器通过在aboutToAppear()时调用this.restoreFromWant(), KvStoreModel组件获取播放列表,playerModel组件重新加载播放器状态和资源。
79
80
81### 相关权限
82
83允许不同设备间的数据交换:[ohos.permission.DISTRIBUTED_DATASYNC](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissiondistributed_datasync)
84
85允许音乐服务后台拉起音乐页面:[ohos.permission.START_ABILITIES_FROM_BACKGROUND](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionstart_abilities_from_background)
86
87允许应用进行调用,无论Ability是否可见: [ohos.permission.START_INVISIBLE_ABILITY](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionstart_invisible_ability)
88
89### 依赖
90
91不涉及
92
93### 约束与限制
94
951.本示例仅支持标准系统上运行。
96
972.本示例完整功能必须双端授予允许使用分布式协同能力,否则在只有发起端授权条件下,发起端会弹框提示错误。
98
993.本示例为stage模型,已适配API version 14版本SDK,SDK版本号(API Version 14 Release),镜像版本号(5.0.2Release)。
100
1014.本示例需要使用DevEco Studio 版本号(5.0.2Release)才可编译运行。
102
1035.本示例需要使用@ohos.distributedDeviceManager系统权限的系统接口。使用Full SDK时需要手动从镜像站点获取,并在DevEco Studio中替换,具体操作可参考[替换指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/full-sdk-switch-guide.md)104
1056.本示例中使用到特殊安装,需要将本示例加入到白名单中再进行安装。详细内容如下
106{
107    "bundleName": "ohos.samples.distributedmusicplayer",
108    "app_signature" : [],
109    "allowAppUsePrivilegeExtension": true
110}
111
112### 下载
113
114如需单独下载本工程,执行如下命令:
115```
116git init
117git config core.sparsecheckout true
118echo code/SuperFeature/DistributedAppDev/ArkTSDistributedMusicPlayer/ > .git/info/sparse-checkout
119git remote add origin https://gitee.com/openharmony/applications_app_samples.git
120git pull origin master
121```