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