I want to combine default VideoView and gesture listener,
I want to implement likewise on VideoView if user swipe left then song plays
i got that how to combine VideoView and GestureListener :
layout file activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java file :
public class MainActivity extends Activity implements SimpleGestureListener {
VideoView videoView;
MainActivity activity;
MediaController mediaController;
AudioManager audioManager;
GestureDetection detector;
int currentPosition;
int currentVolume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
videoView = (VideoView) findViewById(R.id.videoView);
detector = new GestureDetection(this, this);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
intent.setType("audio/*");
startActivityForResult(intent, 1);
}
@Override
public boolean dispatchTouchEvent(MotionEvent me) {
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
String filePath = null;
if (requestCode == 1) {
switch (resultCode) {
case RESULT_OK:
Uri selectedAudio = data.getData();
String[] filePathColumn = { MediaStore.Audio.Media.DATA };
Cursor cursor = getContentResolver().query(selectedAudio,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
break;
default:
finish();
}
if (filePath != null) {
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse(filePath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
} else
finish();
}
}
@Override
public void onSwipe(int direction) {
// TODO Auto-generated method stub
String str = "";
switch (direction) {
case GestureDetection.SWIPE_LEFT:
currentPosition = videoView.getCurrentPosition();
currentPosition = videoView.getCurrentPosition() - 10000;
videoView.seekTo(currentPosition);
str = "Swipe Left";
break;
case GestureDetection.SWIPE_RIGHT:
currentPosition = videoView.getCurrentPosition();
currentPosition = videoView.getCurrentPosition() + 10000;
videoView.seekTo(currentPosition);
str = "Swipe Right";
break;
case GestureDetection.SWIPE_DOWN:
currentVolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
currentVolume - 1, 0);
str = "Swipe Down";
break;
case GestureDetection.SWIPE_UP:
currentVolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
currentVolume + 1, 0);
str = "Swipe Up";
break;
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
GestureDetection.java class :
public class GestureDetection extends SimpleOnGestureListener {
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int MODE_SOLID = 1;
public final static int MODE_DYNAMIC = 2;
private final static int ACTION_FAKE = -13; // just an unlikely number
private int swipe_Min_Distance = 50;
private int swipe_Max_Distance = 350;
private int swipe_Min_Velocity = 100;
private int mode = MODE_DYNAMIC;
private boolean running = true;
private boolean tapIndicator = false;
private Activity context;
private GestureDetector detector;
private SimpleGestureListener listener;
public GestureDetection(Activity context, SimpleGestureListener sgl) {
this.context = context;
this.detector = new GestureDetector(context, this);
this.listener = sgl;
}
public void onTouchEvent(MotionEvent event) {
if (!this.running)
return;
boolean result = this.detector.onTouchEvent(event);
if (this.mode == MODE_SOLID)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.mode == MODE_DYNAMIC) {
if (event.getAction() == ACTION_FAKE)
event.setAction(MotionEvent.ACTION_UP);
else if (result)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.tapIndicator) {
event.setAction(MotionEvent.ACTION_DOWN);
this.tapIndicator = false;
}
}
// else just do nothing, it's Transparent
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final float xDistance = Math.abs(e1.getX() - e2.getX());
final float yDistance = Math.abs(e1.getY() - e2.getY());
if (xDistance > this.swipe_Max_Distance
|| yDistance > this.swipe_Max_Distance)
return false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
boolean result = false;
if (velocityX > this.swipe_Min_Velocity
&& xDistance > this.swipe_Min_Distance) {
if (e1.getX() > e2.getX()) // right to left
this.listener.onSwipe(SWIPE_RIGHT);
else
this.listener.onSwipe(SWIPE_LEFT);
result = true;
} else if (velocityY > this.swipe_Min_Velocity
&& yDistance > this.swipe_Min_Distance) {
if (e1.getY() > e2.getY()) // bottom to up
this.listener.onSwipe(SWIPE_UP);
else
this.listener.onSwipe(SWIPE_DOWN);
result = true;
}
return result;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent arg) {
if (this.mode == MODE_DYNAMIC) {
arg.setAction(ACTION_FAKE);
this.context.dispatchTouchEvent(arg);
}
return false;
}
static interface SimpleGestureListener {
void onSwipe(int direction);
}
}