Electronic piano source code for Android Development

Source: Internet
Author: User

This example mainly uses the knowledge of Android gesture and mediaplayer.
there is no problem with the gesture part. We can use the Event code defined in motionevent to determine the status of the current gesture (press or release)

 imagebutton_white1.setontouchlistener ( New  View. ontouchlistener () {
Public Boolean ontouch (view, motionevent) {
If (motionevent. getaction () = motionevent. action_down) {
play (R. raw. white1); /// Create the resource file to be played
imagebutton_white1.setimageresource (R. drawable. whiteback1);
}< br> If (motionevent. getaction () = motionevent. action_up) {
imagebutton_white1.setimageresource (R. drawable. white1);
}< br> return false ;< BR >}< BR >});

Mediaplayer has the following problems:
Each time I press the keyboard, I need to call the corresponding notes.
Mediaplayer01 = mediaplayer. Create (piano. This, resource );
As the number of buttons increases, the number of create operations increases.
As long as the number of create operations reaches 32, the keyboard will not ring again. At this time, you only need to change the screen direction.ProgramTo continue running.
My understanding is that the resources after create are not released.
But inCodeAdded

 
Mediaplayer01.setoncompletionlistener (NewMediaplayer. oncompletionlistener (){
Public VoidOncompletion (mediaplayer arg0 ){
Mediaplayer01.release ();
Toast. maketext (piano.This,"Resource released!", Toast. length_short). Show ();
}
});

This listening code. Release the resource file after it is played. However, this part does not work during running (because the toast part is not executed ).
I do not know why.

PS: Find a solution today
Do not use the mediaplayer listening event to release the mediaplayer resource. Directly release the file once during play, and then create the resource file.
However, this approach is slightly unreasonable.
Why is the resource file created when an event is triggered not listened to normally. If anyone knows the reason, please help me solve it.

The source file is here
 Mypiano.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.