i'm trying to implement OnLongClickListener on MediaPlayer, but i can't figure out how can i manage on Release button to stop music.
This is the code which i have now.
@Override
public boolean onLongClick(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.play) {
mp1.setLooping(true);
mp1.start(); }
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() == R.id.play) {
if (mp1 != null) {
mp1.setLooping(false);
mp1.pause();
mp1 = null;
}
} }
return false; }
but it gives me error The method onLongClick(View, MotionEvent) of type MainActivity must override or implement a supertype method i managed the music to play on long click but on release false or true it didn't stopped the music... how can i manage it ...
i made it to work with onTouch method, it works one time, but on the second click i get nullpointerexception
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (v.getId() == R.id.play) {
mp1.setLooping(true);
mp1.start();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (v.getId() == R.id.play) {
if (mp1 != null) {
mp1.setLooping(false);
mp1.pause();
mp1 = null;
}
}
}
return false;
} }
here is the error
FATAL EXCEPTION: main java.lang.NullPointerException at app.technozed.dogwhistle.MainActivity.onTouch(MainActivity.java:68) at android.view.View.dispatchTouchEvent(View.java:3762) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1731) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1120) at android.app.Activity.dispatchTouchEvent(Activity.java:2086) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1715) at android.view.ViewRoot.handleMessage(ViewRoot.java:1787) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4633) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)
i think it's because on Pause i set mp1=null; isn't it?
yes it is, i just deleted mp1=null; and it works fine.