| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| entry/ | 22-Oct-2025 | - | 1,900 | 1,555 | ||
| hvigor/ | 22-Oct-2025 | - | 9 | 8 | ||
| screenshots/device/ | 22-Oct-2025 | - | ||||
| signature/ | 22-Oct-2025 | - | 28 | 28 | ||
| .gitignore | D | 22-Oct-2025 | 100 | 7 | 7 | |
| README.md | D | 22-Oct-2025 | 1.9 KiB | 41 | 20 | |
| README_zh.md | D | 22-Oct-2025 | 6.9 KiB | 116 | 79 | |
| build-profile.json5 | D | 22-Oct-2025 | 1.1 KiB | 44 | 42 | |
| hvigorfile.js | D | 22-Oct-2025 | 174 | 2 | 1 | |
| hvigorw | D | 22-Oct-2025 | 1.4 KiB | 49 | 28 | |
| hvigorw.bat | D | 22-Oct-2025 | 1.4 KiB | 58 | 43 | |
| oh-package.json5 | D | 22-Oct-2025 | 221 | 12 | 11 | |
| ohsoTest.md | D | 22-Oct-2025 | 2.8 KiB | 23 | 21 |
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<img src="screenshots/device/music2_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 30- This sample requires DevEco Studio 3.0 Beta4 (Build Version: 3.0.0.992, built on July 14, 2022) to compile and run. 31 32- If the app is set as a preset app, "error: install sign info inconsistent" will be displayed during the app installation. If this occurs, run the following commands to install the app and restart the device after the installation: 33 34 **hdc shell mount -o rw,remount /** 35 36 **hdc file send ./entry-default-signed.hap /system/app/com.ohos.distributedmusicplayer/Music_Demo.hap** 37 38 **hdc shell reboot** 39 40After the device is restarted, the app is installed. No other operation is required. 41
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.window](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-window.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|  | 25 26使用说明 27 281.**音乐播放**,点击**播放**、**暂停**、上**一曲**、下**一曲**按钮可以对音乐进行操作。 29 302.**跨设备迁移播放**,组网条件下,点击**流转**按钮,选择设备,拉起对端设备上的音乐,本端退出。 31 32 33#### 相关概念 34 35音频播放:媒体子系统包含了音视频相关媒体业务,通过AudioPlayer实现音频播放的能力。 36 37数据流转:分布式数据管理为应用程序提供不同设备间数据库的分布式协同能力。通过调用分布式数据各个接口,应用程序可将数据保存到分布式数据库中,并可对分布式数据库中的数据进行增/删/改/查等各项操作。 38 39### 工程目录 40``` 41entry/src/main/ets/ 42|---pages/index 43| |---index.css // 首页样式 44| |---index.hml // 首页结构 45| |---index.js // 首页逻辑 46|---model 47| |---PlayerModel.js // 播放器模块 48| |---KvStoreModel.js // kvstore对象操作类 49| |---RemoteDeviceModel.js // 远程设备操作类 50|---common/media // 存放播放器图片资源 51``` 52 53### 具体实现 54在分布式音乐播放器中,分布式设备管理包含了分布式设备搜索、分布式设备列表弹窗、远端设备拉起三部分。 55首先在分布式组网内搜索设备,然后把设备展示到分布式设备列表弹窗中,最后根据用户的选择拉起远端设备。 56#### 分布式设备搜索 57通过SUBSCRIBE_ID搜索分布式组网内的远端设备,详见registerDeviceListCallback_(callback) {}模块[源码参考](entry/src/main/js/MainAbility/model/RemoteDeviceModel.js )。 58#### 分布式设备列表弹窗 59使用continueAbilityDialog弹出分布式设备列表弹窗,参考首页。 60#### 远端设备拉起 61通过startAbility(deviceId)方法拉起远端设备的包,[源码参考](entry/src/main/js/MainAbility/pages/index/index.js )。 62#### 分布式数据管理 63(1) 管理分布式数据库 64创建一个KVManager对象实例,用于管理分布式数据库对象。通过distributedData.createKVManager(config),并通过指定Options和storeId,创建并获取KVStore数据库,并通过Promise方式返回,此方法为异步方法,例如this.kvManager.getKVStore(STORE_ID, options).then((store) => {}) 65(2) 订阅分布式数据变化 66通过订阅分布式数据库所有(本地及远端)数据变化实现数据协同[源码参考](entry/src/main/js/MainAbility/model/KvStoreModel.js )。 67 68#### 跨设备播放操作 69(1)分布式设备管理器绑定应用包 70deviceManager.createDeviceManager('ohos.samples.distributedmusicplayer') [源码参考](entry/src/main/js/MainAbility/model/RemoteDeviceModel.js )。 71(2) 初始化播放器 72构造函数中通过'@ohos.multimedia.media'组件对播放器进行实例化,并调用播放器初始化函数,通过播放器的on函数,监听error、finish、timeUpdate 73(3) 同步当前播放数据 74播放器通过onRadioChange(),将当前播放的资源、时间、以及播放状态同步给选中的设备。 75(4) 接收当前播放数据 76播放通过onNewRequest(),调用this.restoreFromWant(), featureAbility获取want参数,kvstore组件获取播放列表,playerModel组件重新加载播放器状态和资源。 77 78 79### 相关权限 80 81允许不同设备间的数据交换:[ohos.permission.DISTRIBUTED_DATASYNC](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-all.md#ohospermissiondistributed_datasync) 82 83允许系统应用获取分布式设备的认证组网能力:[ohos.permission.ACCESS_SERVICE_DM](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/AccessToken/permissions-for-system-apps.md#ohospermissionaccess_service_dm) 84 85### 依赖 86 87不涉及 88 89### 约束与限制 90 911.本示例仅支持标准系统上运行。 92 932.本示例需要使用DevEco Studio 3.0 Beta4 (Build Version: 3.0.0.992, built on July 14, 2022)才可编译运行。 94 953.如果安装本示例报错为error:install sign info inconsistent,则有可能本应用被设置为系统预置应用,已安装在系统中,此时需使用命令进行替换安装,并在替换安装后对设备进行重启操作,具体命令如下: 96 97hdc shell mount -o rw,remount / 98 99hdc file send ./entry-default-signed.hap /system/app/com.ohos.distributedmusicplayer/Music_Demo.hap 100 101hdc shell reboot 102 103等设备重启后即可完成应用的替换安装,无需其他操作。 104 1054.本示例需要使用@ohos.distributedDeviceManager系统权限的系统接口。使用Full SDK时需要手动从镜像站点获取,并在DevEco Studio中替换,具体操作可参考[替换指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/full-sdk-switch-guide.md)。 106 107### 下载 108 109如需单独下载本工程,执行如下命令: 110``` 111git init 112git config core.sparsecheckout true 113echo code/SuperFeature/DistributedAppDev/JsDistributedMusicPlayer/ > .git/info/sparse-checkout 114git remote add origin https://gitee.com/openharmony/applications_app_samples.git 115git pull origin master 116```