0

我正在尝试制作一个小型钢琴键盘,但在媒体播放器的管理和按钮的触摸方面发现了一些问题。这是我到目前为止写的代码:

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
public class KeyboardPianoActivity extends Activity {

Button dooT,reT,miT,faT,solT,laT,siT,doodT,redT,fadT,soldT,ladT,doo1T,re1T,mi1T,fa1T,sol1T,la1T,si1T,doo1dT,re1dT,fa1dT,sol1dT,la1dT,doo2T;

MediaPlayer m1; 
MediaPlayer m2;
MediaPlayer m3;
MediaPlayer m4;
MediaPlayer m5; 
MediaPlayer m6; 
MediaPlayer m7,m8,m9,m10,m11,m12;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);

dooT = (Button) findViewById(R.id.button1);
reT = (Button) findViewById(R.id.button2);
miT = (Button) findViewById(R.id.button3);
faT = (Button) findViewById(R.id.button4);
solT = (Button) findViewById(R.id.button5);
laT = (Button) findViewById(R.id.button6);
siT = (Button) findViewById(R.id.button7);


m1 = MediaPlayer.create(getBaseContext(), R.raw.do1);
m2 = MediaPlayer.create(getBaseContext(), R.raw.re);
m3 = MediaPlayer.create(getBaseContext(), R.raw.mi);
m4 = MediaPlayer.create(getBaseContext(), R.raw.fa);
m5 = MediaPlayer.create(getBaseContext(), R.raw.sol);
m6 = MediaPlayer.create(getBaseContext(), R.raw.la);
m7 = MediaPlayer.create(getBaseContext(), R.raw.si);



dooT.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {

m1.start();
}
});

reT.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {

m2.start();
}
});

miT.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {

m3.start();
}
});

faT.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {

m4.start();
}
});

solT.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {

m5.start();
}
}); 
}
}

这是 LogCat:

E/AndroidRuntime(29177): FATAL EXCEPTION: main
E/AndroidRuntime(29177): java.lang.NullPointerException
E/AndroidRuntime(29177): at      it.bisemanuDEV.piano.KeyboardPianoActivity$1.onClick(KeyboardPianoActivity.java:71)
E/AndroidRuntime(29177):    at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(29177):    at android.view.View$PerformClick.run(View.java:8819)
E/AndroidRuntime(29177):    at android.os.Handler.handleCallback(Handler.java:603)
E/AndroidRuntime(29177):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(29177):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(29177):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(29177):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(29177):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(29177):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)    
E/AndroidRuntime(29177):    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(29177):    at dalvik.system.NativeStart.main(Native Method)

我仍然是 Android 编程的初学者,我想正确管理按钮和媒体播放器的行为,以模拟钢琴键盘的操作。我希望你的一些建议。谢谢

4

2 回答 2

0

不要getBaseContext()用来创建MediaPlayer对象,使用getApplicationContext()or this,这取决于它需要的持久性。有关更多信息,请参阅此答案

于 2012-06-27T21:21:11.060 回答
0

对于这样的应用程序,您不想使用它MediaPlayer来完成所有这些工作。 SoundPool专为需要短音效的项目而设计。运行这么多MediaPlayer实例将非常慢。

http://developer.android.com/reference/android/media/SoundPool.html

于 2012-06-27T21:47:01.413 回答