0

I'm trying to make a small piano keyboard but am finding some problems in the management of the media player and the touch of button. This is the code I've written so far:

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();
}
}); 
}
}

This is the 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)

I'm still a beginner with programming in Android, and I would like to manage properly the behavior of the button, and the media player, to simulate the operation of a piano keyboard. I hope some of your advice. Thanks

DaChavoh
  • 100
  • 1
  • 16
whiteTIGER
  • 391
  • 1
  • 6
  • 19

2 Answers2

0

Don't use getBaseContext() to create the MediaPlayer objects, use getApplicationContext() or this, depending on how persistent it needs to be. See this answer for more information.

Community
  • 1
  • 1
Geobits
  • 22,218
  • 6
  • 59
  • 103
0

For an application like this, you do not want to use MediaPlayer to do all that work. SoundPool was designed for projects like this that need short sound effects. Running this many MediaPlayerinstances will be very slow.

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

Jack Satriano
  • 1,999
  • 1
  • 13
  • 17