package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.MotionEvent;
import android.media.MediaPlayer;
/**
* @author: ZhangFL
*/
public class Piano extends Activity {
private ImageButton imageButton_white1;
private ImageButton imageButton_white2;
private ImageButton imageButton_white3;
private ImageButton imageButton_white4;
private ImageButton imageButton_white5;
private ImageButton imageButton_white6;
private ImageButton imageButton_white7;
private ImageButton imageButton_white8;
private ImageButton imageButton_black1;
private ImageButton imageButton_black2;
private ImageButton imageButton_black3;
private ImageButton imageButton_black4;
private ImageButton imageButton_black5;
private MediaPlayer mediaPlayer01;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
mediaPlayer01 = new MediaPlayer();
// mediaPlayer01 = MediaPlayer.create(Piano.this, R.raw.white1);
imageButton_white1 = (ImageButton) findViewById(R.id.white1);
imageButton_white2 = (ImageButton) findViewById(R.id.white2);
imageButton_white3 = (ImageButton) findViewById(R.id.white3);
imageButton_white4 = (ImageButton) findViewById(R.id.white4);
imageButton_white5 = (ImageButton) findViewById(R.id.white5);
imageButton_white6 = (ImageButton) findViewById(R.id.white6);
imageButton_white7 = (ImageButton) findViewById(R.id.white7);
imageButton_white8 = (ImageButton) findViewById(R.id.white8);
imageButton_black1 = (ImageButton) findViewById(R.id.black1);
imageButton_black2 = (ImageButton) findViewById(R.id.black2);
imageButton_black3 = (ImageButton) findViewById(R.id.black3);
imageButton_black4 = (ImageButton) findViewById(R.id.black4);
imageButton_black5 = (ImageButton) findViewById(R.id.black5);
imageButton_white1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white1);
imageButton_white1.setImageResource(R.drawable.whiteback1);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white1.setImageResource(R.drawable.white1);
}
return false;
}
});
imageButton_white2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white2);
imageButton_white2.setImageResource(R.drawable.whiteback2);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white2.setImageResource(R.drawable.white2);
}
return false;
}
});
//
imageButton_white3.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white3);
imageButton_white3.setImageResource(R.drawable.whiteback3);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white3.setImageResource(R.drawable.white3);
}
return false;
}
});
imageButton_white4.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white4);
imageButton_white4.setImageResource(R.drawable.whiteback4);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white4.setImageResource(R.drawable.white4);
}
return false;
}
});
imageButton_white5.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white5);
imageButton_white5.setImageResource(R.drawable.whiteback5);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white5.setImageResource(R.drawable.white5);
}
return false;
}
});
imageButton_white6.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white6);
imageButton_white6.setImageResource(R.drawable.whiteback6);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white6.setImageResource(R.drawable.white6);
}
return false;
}
});
imageButton_white7.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white7);
imageButton_white7.setImageResource(R.drawable.whiteback7);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white7.setImageResource(R.drawable.white7);
}
return false;
}
});
imageButton_white8.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.white8);
imageButton_white8.setImageResource(R.drawable.whiteback8);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_white8.setImageResource(R.drawable.white8);
}
return false;
}
});
imageButton_black1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.black1);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_black1.setImageResource(R.drawable.black1);
}
return false;
}
});
imageButton_black2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.black2);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_black2.setImageResource(R.drawable.black2);
}
return false;
}
});
imageButton_black3.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.black3);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_black3.setImageResource(R.drawable.black3);
}
return false;
}
});
imageButton_black4.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.black4);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_black4.setImageResource(R.drawable.black4);
}
return false;
}
});
imageButton_black5.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
play(R.raw.black5);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton_black5.setImageResource(R.drawable.black5);
}
return false;
}
});
//
//
mediaPlayer01.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mediaPlayer01.release();
mediaPlayer01 = null;
Toast.makeText(Piano.this, "资源释放了!", Toast.LENGTH_SHORT).show();
}
});
mediaPlayer01.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer arg0, int i, int i1) {
try {
mediaPlayer01.release();
Toast.makeText(Piano.this, "发生错误了!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
}
//-------------------------------------------------------------------------------------
private void play(int resource) {
try {
mediaPlayer01.release();
mediaPlayer01 = MediaPlayer.create(Piano.this, resource);
mediaPlayer01.start();
} catch (Exception e) {
Toast.makeText(Piano.this, "发生错误了:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer01 != null) {
mediaPlayer01.release();
mediaPlayer01 = null;
}
}
}
分享到:
相关推荐
Android简易版七键钢琴
简单的 模拟Android钢琴的程序源码,
android小钢琴程序,转载的
1、钢琴软件的app软件 2、实现各种琴谱的选择,根据用户按键发出声音,录制弹奏的乐曲,音频播放,视频播放等功能。 3、实现C/S架构,采用json解析数据 4、美观的界面,温和的主题颜色 5、适合于毕业生以及开发工作...
字母弹钢琴小游戏Android项目应用源码
android 现实 简易钢琴
我在第一篇博客《android小程序-电子钢琴-滑动连续响应》中实现了一个简单地7键钢琴,这几天把它又完善了一下,增加了多点触控,按键也增加了一个低音区和一个高
NULL 博文链接:https://davice-li.iteye.com/blog/760736
Android程序研发源码Android 小钢琴源码.rar
资源:【Android-钢琴模拟实现】
主要为大家详细介绍了Android实现简易版弹钢琴效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
比较简单的Android小程序,具体说明,请参考博客:https://blog.csdn.net/m0_46153949/article/details/106865169
搭建Android开发环境和构建第一个Android程序(Android studio版)
SimpleDanmu:实现一个简单Android弹幕库
android平台上的一个简单的计算机程序,运用intent实现activity的跳转,实现计算功能
根据前人的基础上改编的,和网页flash钢琴差不多的,按键做得很不好看,只是前期的作品,希望大家不要纠结于此~
小钢琴源码是基于Android的小钢琴软件,大家可以看看,希望给大家带来些帮助。
一个android上的sqlite简易使用程序,包含插入,查询,列表
android开发基础应用程序——利用Android Studio开发一个简易计算器
自己做的android项目,dip布局,仿钢琴.下了就知道好了