当前位置:网站首页>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 :
边栏推荐
- Microservice
- Galera cluster of MariaDB - dual active and dual active installation settings
- 【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
- MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
- Chinese verification of JS regular expressions (turn)
- [ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- Five papers recommended for the new development of convolutional neural network in deep learning
- ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
- js如何实现数组转树
猜你喜欢
业务场景功能的继续修改
Combien de temps faut - il pour obtenir un certificat PMP?
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
[kotlin] the third day
微服务(Microservice)那点事儿
ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
QT personal learning summary
巩固表达式C# 案例简单变量运算
Jar batch management gadget
随机推荐
Skills in analyzing the trend chart of London Silver
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
The input of uniapp is invalid except for numbers
业务场景功能的继续修改
Servlet+jdbc+mysql simple web exercise
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
22-07-02周总结
Five papers recommended for the new development of convolutional neural network in deep learning
如果炒股开华泰证券的户,在网上开户安全吗?
Is the account opening link of Huatai Securities with low commission safe?
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
Netcore3.1 JSON web token Middleware
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
C语言中sizeof操作符的坑
OSEK standard ISO_ 17356 summary introduction
[path planning] RRT adds dynamic model for trajectory planning
C language to quickly solve the reverse linked list
js如何实现数组转树
js正则表达式之中文验证(转)