Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to manage media player into a piano keyboard in Android ?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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(); } }); } } </code></pre> <p>This is the LogCat:</p> <pre><code>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) </code></pre> <p>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</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload