VideoView手势(刷卡功能)的Android [英] VideoView with Gesture(Swipe function) in android
本文介绍了VideoView手势(刷卡功能)的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想结合默认的 VideoView 和的姿态监听器
我要实现同样的VideoView如果用户刷卡左,那么歌曲向后播放或向右滑动则乐曲的播放着。
打开默认的媒体通过以下code球员:
意向意图=新的意图(android.intent.action.MUSIC_PLAYER);
startActivity(意向);
再如何添加手势听者??
解决方案我明白了如何结合VideoView和GestureListener
:
布局文件的 activity_main.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
< VideoView
机器人:ID =@ + ID / videoView
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< / LinearLayout中>
MainActivity.java 文件:
公共类MainActivity扩展活动实现SimpleGestureListener {
VideoView videoView;
MainActivity活动;
的MediaController的MediaController;
AudioManager audioManager;
GestureDetection检测器;
INT currentPosition;
INT currentVolume;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
audioManager =(AudioManager)getSystemService(AUDIO_SERVICE);
videoView =(VideoView)findViewById(R.id.videoView);
探测器=新GestureDetection(这一点,这一点);
意向意图=新的意图(Intent.ACTION_GET_CONTENT,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
intent.setType(音频/ *);
startActivityForResult(意向,1);
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent我){
//调用SimpleGestureFilter类的onTouchEvent
this.detector.onTouchEvent(箱);
返回super.dispatchTouchEvent(箱);
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
// TODO自动生成方法存根
super.onActivityResult(要求code,因此code,数据);
字符串文件路径= NULL;
如果(要求code == 1){
开关(结果code){
案例RESULT_OK:
乌里selectedAudio = data.getData();
的String [] filePathColumn = {MediaStore.Audio.Media.DATA};
光标光标= getContentResolver()查询(selectedAudio,
filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
文件路径= cursor.getString(参数:columnIndex);
cursor.close();
打破;
默认:
完();
}
如果(文件路径!= NULL){
的MediaController =新的MediaController(本);
mediaController.setAnchorView(videoView);
开放的我们的uri = Uri.parse(文件路径);
videoView.setMediaController(的MediaController);
videoView.setVideoURI(URI);
videoView.requestFocus();
videoView.start();
} 其他
完();
}
}
@覆盖
公共无效onSwipe(INT方向){
// TODO自动生成方法存根
字符串str =;
开关(方向){
案例GestureDetection.SWIPE_LEFT:
currentPosition = videoView.getCurrentPosition();
currentPosition = videoView.getCurrentPosition() - 10000;
videoView.seekTo(currentPosition);
海峡=刷卡左;
打破;
案例GestureDetection.SWIPE_RIGHT:
currentPosition = videoView.getCurrentPosition();
currentPosition = videoView.getCurrentPosition()+ 10000;
videoView.seekTo(currentPosition);
海峡=向右滑动;
打破;
案例GestureDetection.SWIPE_DOWN:
currentVolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
currentVolume - 1,0);
海峡=向下滑动
打破;
案例GestureDetection.SWIPE_UP:
currentVolume = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
currentVolume + 1,0);
海峡=向上滑动;
打破;
}
Toast.makeText(这一点,STR,Toast.LENGTH_SHORT).show();
}
}
GestureDetection.java 类:
公共类GestureDetection扩展SimpleOnGestureListener {
公共最后静态INT SWIPE_UP = 1;
公共最后静态INT SWIPE_DOWN = 2;
公共最后静态INT SWIPE_LEFT = 3;
公共最后静态INT SWIPE_RIGHT = 4;
公共最后静态INT MODE_SOLID = 1;
公共最后静态INT MODE_DYNAMIC = 2;
私人最终静态INT ACTION_FAKE = -13; //只是一个不可能的数字
私人诠释swipe_Min_Distance = 50;
私人诠释swipe_Max_Distance = 350;
私人诠释swipe_Min_Velocity = 100;
私人诠释模式= MODE_DYNAMIC;
私人布尔运行=真;
私人布尔tapIndicator = FALSE;
私人活动范围内;
私人GestureDetector检测器;
私人SimpleGestureListener监听;
公共GestureDetection(活动方面,SimpleGestureListener SGL){
this.context =背景;
this.detector =新GestureDetector(背景下,这一点);
this.listener = SGL;
}
公共无效的onTouchEvent(MotionEvent事件){
如果(!this.running)
返回;
布尔结果= this.detector.onTouchEvent(事件);
如果(this.mode == MODE_SOLID)
event.setAction(MotionEvent.ACTION_CANCEL);
否则,如果(this.mode == MODE_DYNAMIC){
如果(event.getAction()== ACTION_FAKE)
event.setAction(MotionEvent.ACTION_UP);
否则,如果(结果)
event.setAction(MotionEvent.ACTION_CANCEL);
否则,如果(this.tapIndicator){
event.setAction(MotionEvent.ACTION_DOWN);
this.tapIndicator = FALSE;
}
}
//其他人只是什么也不做,它是透明的
}
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
浮动velocityY){
最终浮动xDistance = Math.abs(e1.getX() - e2.getX());
最终浮动yDistance = Math.abs(e1.getY() - e2.getY());
如果(xDistance> this.swipe_Max_Distance
|| yDistance> this.swipe_Max_Distance)
返回false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
布尔结果= FALSE;
如果(velocityX> this.swipe_Min_Velocity
&功放;&安培; xDistance> this.swipe_Min_Distance){
如果(e1.getX()> e2.getX())//从右到左
this.listener.onSwipe(SWIPE_RIGHT);
其他
this.listener.onSwipe(SWIPE_LEFT);
结果=真;
}否则如果(velocityY> this.swipe_Min_Velocity
&功放;&安培; yDistance> this.swipe_Min_Distance){
如果(e1.getY()> e2.getY())//自下而上
this.listener.onSwipe(SWIPE_UP);
其他
this.listener.onSwipe(SWIPE_DOWN);
结果=真;
}
返回结果;
}
@覆盖
公共布尔onSingleTapConfirmed(MotionEvent ARG){
如果(this.mode == MODE_DYNAMIC){
arg.setAction(ACTION_FAKE);
this.context.dispatchTouchEvent(ARG);
}
返回false;
}
静态接口SimpleGestureListener {
无效onSwipe(INT方向);
}
}
I want to combine default VideoView and gesture listener,
I want to implement likewise on VideoView if user swipe left then song plays backward or swipe right then song plays forward.
open default media player via following code :
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
then how to add gesture listener ..??
解决方案
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);
}
}
这篇关于VideoView手势(刷卡功能)的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文