当前位置:网站首页>Summer challenge brings you to play harmoniyos multi terminal piano performance
Summer challenge brings you to play harmoniyos multi terminal piano performance
2022-07-04 23:57:00 【51CTO】
This article is participating in the starlight project 3.0— Summer Challenge
Preface
If you want to play sweet music, why don't you have a piano , Code to realize your playing wish , Soft power program, little brother, take you hand-in-hand to code and make a piano , Pop up the song you want with your mobile phone , It is not a problem for multiple mobile phones to play at the same time .
Project introduction
The project mainly adopts HarmonyOS Cross end migration ,Fractio Wait to realize piano 88 The keys are divided into seven areas, which flow to different devices to play the corresponding audio . The traditional solid piano has three sound zones , Divided into nine groups , As shown in the figure below :
The project is in the equipment A The piano keys in a group of small characters in the midrange area are initially displayed on the , Click the flow button to pop up the three tone area , Seven ranges for users to choose , After the user confirms the selected range , On the premise of meeting the constraints and limitations of circulation characteristics , That is, on the device B Show the selected range on , And the device A,B It can be operated independently , They don't influence each other .
After entering the project , Show a group of small characters in the piano midrange , As shown in the figure below :
Seven white buttons and five black buttons , A group of corresponding audio corresponding to the small characters in the middle tone area , Multiple keys can trigger audio playback at the same time .
1. Flow button
Click the flow button , A pop-up box for selecting the range will pop up , The options have a total of three ranges , They are bass areas 、 Midrange 、 treble .
Select OK , The flow equipment selection box will pop up , Click the corresponding device name , When selecting the range , The selected corresponding range flows to the device B, As shown in the figure below :
equipment B Show ,A The selected device corresponds to the range , The flow button changes to flow .
If in the equipment B Click the transferred button , The exit flow pop-up box will pop up , As shown in the figure below :
If cancel is selected , The pop-up box disappears , No change of interface , Touch and click the area outside the pop-up box , The pop-up box will also disappear .
If you choose OK , equipment B Exit circulation .
2. Range selection button
Click the range selection button , There are three zones in total for the meeting options , They are bass areas 、 Midrange 、 treble , The second level option of bass area is big character two 、 A group of ; Big block ; The second level option of the midrange area is small words 、 A group of small characters 、 Two groups of small characters ; The secondary options in the high range are three groups of small characters , Small four 、 Five groups , The default is midrange , A group of small characters , As shown in the figure below :
Select OK , Select the corresponding range , The current range interface of the device will become the selected range , For example, choose small character four , Five ranges , At the same time, click the range selection button again , The default selection is changed to small four 、 Five groups , Corresponding to the current selection , As shown in the figure below :
3. Piano key press trigger effect
①. White button E Trigger effect , As shown in the figure below :
②. Black button d1m Trigger effect , As shown in the figure below :
③. Multi finger key trigger effect , As shown in the figure below :
Logic implementation
One : Development steps of circulation related functions :
1. Create... In the project MainAbility To realize IAbilityContinuation Interface , Besides , Still need to be in MainAbility Of onStart() in , call requestPermissionsFromUser() Method to apply for permission .
2. In the corresponding config.json Declare the permission of cross end migration access :
ohos.permission.DISTRIBUTED_DATASYNC, stay config.json The configuration in :
3. stay MainAbilitySlice Realize the page of piano keys in , The code logic is MainAbilitySlice To realize , The code example is as follows :
4. Bind the click event to the flow key , Click the flow button to pop up the range selection box , After determining the selected range , Pop up the device selection box : The code example is as follows :
5. According to the equipment list, all qualified equipment can be displayed in the equipment pop-up window , For user selection , The equipment list adaptation code is as follows :
6. According to the selected equipment B Of Id, The selected range can be displayed on the device , And use according to conditions Fraction Replace equipment A A group of ranges of upper small characters , It can also operate piano keys , The sample code is as follows :
7.FA Cross end migration of also involves the transfer of state data , Need to achieve IAbilityContinuation Interface , In order to realize the management ability of specific events in the migration process , The code example is as follows :
Two : Development steps of functions related to audio playback ability
This project realizes the equipment A,B At the same time, it has the ability to play audio , Audio playback is as a separate serviceAbility, Use HarmonyOS IDL Realize the communication and data transmission between different devices , The code example is as follows :
AudioServiceAbility When the project starts , Load piano key audio resources , And keep the system running in the background , Prevent the system kill, And according to the range selected by the user , And touch different keys to SoundPlayer Play the audio , The code example is as follows :
1. stay MainAbilitySlice in OnStart() Start the local audio service , Avoid audio proxy interfaces Proxy It's empty , The code example is as follows :
3、 ... and : Function development steps related to range selection ability
1. Click the range selection button , You can pop up the pop-up box for range selection , The same as the flow button , Range selection pop-up box , The user is choosing the corresponding range , The current device can be switched to the selected range , And the corresponding audio can be played , stay MainAbilitySlice Of OnStart() Method. The example code is as follows :
2. According to the result selected by the user , Replace the range on the device , The code example is as follows :
Reference resources
1.HarmonyOS Circulation characteristics ( Cross end migration ) May refer to : https://developer.harmonyos.com/cn/docs/documentation/doc-guides/hop-cross-device-migration-guidelines-0000001146058939
2.HarmonyOS IDL The interface specification can be referred to :
https://developer.harmonyos.com/cn/docs/documentation/doc-references/idl-overview-0000001050762835
3. Project address , For reference : https://gitee.com/swan-link/simple-piano
Summary and analysis
1. Before circulation , Circulation constraints must be met , All devices need to be in the same WiFi, And log in with the same Huawei account ;
2. After circulation , equipment B The range selection function on is the same as that of the device A Range selection function , equipment A And equipment B Audio playback does not conflict with each other ;
3. at present Nova 9 When the mobile phone runs this project , There are problems at the bottom , It can't be solved for the time being , No problem with other mobile phones ;
4.HarmonyOS SoundPlayer Disadvantages of native short tone playing ,SoundPlayer Play a short tone when playing , All audio resources need to be loaded in advance , namely createSound(Context context, int resourceId) The method is to combine audio resources according to the application context ID Load audio data to generate short tone resources , The method is asynchronous , There are many piano keys in this project , Yes 88 Key resources , It takes a long time to complete the generation of all short tone resources , The project is here , The solution is as follows :
All key audio resources in the project , It is divided into seven ranges , At the same time, all resources are divided into seven SoundPlayer Generate short tone resources , It can effectively reduce the time consumption .
5. This project triggers piano key tone , Is to set the touch event on the whole layout page , Flexible access to device screen size , Divide different key areas , Enable users to operate , It can realize the touch effect of the corresponding key , And the corresponding piano key audio playback , The sample code is as follows :
The above is used HarmonyOS Cross end migration ,Fractio And other technologies to realize the interactive process of mobile piano , Through the project , We can quickly understand the data “ Multi terminal cooperation ” and “ Cross end migration ”, It is convenient to quickly realize seamless switching in other projects .
For more original content, please pay attention to softcom power OpenHarmony college
Want to know more about open source , Please visit :
边栏推荐
- 股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
- 如何将自己的代码作品快速存证,已更好的保护自己劳动成果
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- Mysql database backup and recovery -- mysqldump command
- 雅思考试流程、需要具体注意些什么、怎么复习?
- QT personal learning summary
- MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
- Acrel-EMS综合能效平台在校园建设的意义
- The pit of sizeof operator in C language
- 打新债开户注册安全吗?有没有风险的?靠谱吗?
猜你喜欢
[JS] - [sort related] - Notes
用快解析内网穿透实现零成本自建网站
C language to quickly solve the reverse linked list
Skills in analyzing the trend chart of London Silver
城市轨道交通站应急照明疏散指示系统设计
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
It's too convenient. You can complete the code release and approval by nailing it!
基于三维gis平台的消防系统运用
Font design symbol combination multifunctional wechat applet source code
随机推荐
Summary of week 22-07-02
高配笔记本使用CAD搬砖时卡死解决记录
C语言中sizeof操作符的坑
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Examples of time (calculation) total tools: start time and end time of this year, etc
Application of multi loop instrument in base station "switching to direct"
The caching feature of docker image and dockerfile
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
[JS] - [dynamic planning] - Notes
同事的接口文档我每次看着就头大,毛病多多。。。
快解析内网穿透帮助企业快速实现协同办公
华泰证券低佣金的开户链接安全吗?
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
微服务(Microservice)那点事儿
Fast analysis -- easy to use intranet security software
Actual combat simulation │ JWT login authentication
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
uniapp 除了数字,其他输入无效