先看一下效果图
比自带控制器多出上面菜单,并将视频名称改到上面显示
先是布局文件 mymdiacontroller
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="34dp" android:background="#77000000"> <ImageButton android:id="@+id/mediacontroller_top_back" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentStart="true" android:background="@null" android:src="@drawable/left_white_arrow"/> <TextView android:id="@+id/mediacontroller_file_name" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginLeft="5dp" android:ellipsize="marquee" android:singleLine="true" android:text="file name"/> <TextView android:id="@+id/mediacontroller_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="17:22" android:textColor="#ffffff" android:textSize="15sp"/> </RelativeLayout> <RelativeLayout android:id="@+id/rl_med" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#77000000"> <ImageButton android:id="@+id/mediacontroller_play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:background="@drawable/mediacontroller_button" android:contentDescription="@string/mediacontroller_play_pause" android:src="@drawable/mediacontroller_pause"/> <TextView android:id="@+id/mediacontroller_time_current" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/mediacontroller_play_pause" android:text="33:33:33" /> <TextView android:id="@+id/mediacontroller_time_total" style="@style/MediaController_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="33:33:33"/> <SeekBar android:id="@+id/mediacontroller_seekbar" style="@style/MediaController_SeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/mediacontroller_time_total" android:layout_toRightOf="@id/mediacontroller_time_current" android:focusable="true" android:max="1000"/> </RelativeLayout> </RelativeLayout></LinearLayout>然后是控制器代码MyMediaController.javapublic class MyMediaController extends MediaController { PRivate GestureDetector mGestureDetector; private ImageButton img_back; private TextView textViewTime; private VideoView videoView; private Activity activity; private Context context; private int controllerWidth = 0;//设置mediaController高度为了使横屏时top显示在屏幕顶端 //返回监听 private View.OnClickListener backListener = new View.OnClickListener() { public void onClick(View v) { if(activity != null){ activity.finish(); } } }; //videoview 用于对视频进行控制的等,activity为了退出 public MyMediaController(Context context, VideoView videoView , Activity activity) { super(context); this.context = context; this.videoView = videoView; this.activity = activity; WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); controllerWidth = wm.getDefaultDisplay().getWidth(); mGestureDetector = new GestureDetector(context, new MyGestureListener()); } @Override protected View makeControllerView() { //加入布局文件,mymediacontroller布局名称 View v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(getResources().getIdentifier("mymediacontroller", "layout", getContext().getPackageName()), this); v.setMinimumHeight(controllerWidth); img_back = (ImageButton) v.findViewById(getResources().getIdentifier("mediacontroller_top_back", "id", context.getPackageName())); img_back.setOnClickListener(backListener); textViewTime = (TextView)v.findViewById(getResources().getIdentifier("mediacontroller_time", "id", context.getPackageName())); return v; } @Override public boolean dispatchKeyEvent(KeyEvent event) {// System.out.println("MYApp-MyMediaController-dispatchKeyEvent"); return true; } @Override public boolean onTouchEvent(MotionEvent event) { if (mGestureDetector.onTouchEvent(event)) return true; // 处理手势结束 switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: break; } return super.onTouchEvent(event); } private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { //当收拾结束,并且是单击结束时,控制器隐藏/显示 toggleMediaControlsVisiblity(); return super.onSingleTapConfirmed(e); } @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return super.onScroll(e1, e2, distanceX, distanceY); } //双击暂停或开始 @Override public boolean onDoubleTap(MotionEvent e) { playOrPause(); return true; } } //设置时间 public void setTime(String time){ if (textViewTime != null) textViewTime.setText(time); } //隐藏/显示 private void toggleMediaControlsVisiblity(){ if (isShowing()) { hide(); } else { show(); } } //播放与暂停 private void playOrPause(){ if (videoView != null) if (videoView.isPlaying()) { videoView.pause(); } else { videoView.start(); } }}上面代码直接复制就可以使用了.
Activity中使用示例
public class CompanyVideoActivity extends BaseActivity implements Runnable{//返回布局id @Override protected int getLayoutId() { return R.layout.activity_company_video; } private VideoView mVideoView; private MyMediaController myMediaController; private static final int TIME = 0; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case TIME: myMediaController.setTime(msg.obj.toString()); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置为全屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置视频解码监听 if (!io.vov.vitamio.LibsChecker.checkVitamioLibs(this)) return; mVideoView = (VideoView) findViewById(R.id.vv_videoplay); //设置播放地址 mVideoView.setVideoURI(Uri.parse("http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv")); //创建控制器 myMediaController = new MyMediaController(this,mVideoView,this); //设置控制器 mVideoView.setMediaController(myMediaController); //设置高画质 mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH); //设置显示时长 myMediaController.show(5000); mVideoView.requestFocus(); new Thread(this).start(); } @Override public void onConfigurationChanged(Configuration newConfig) { if (mVideoView != null){ mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0); } super.onConfigurationChanged(newConfig); } @Override protected void onDestroy() { super.onDestroy(); } @Override public void run() { // TODO Auto-generated method stub while (true) { //时间读取线程 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); String str = sdf.format(new Date()); Message msg = new Message(); msg.obj = str; msg.what = TIME; mHandler.sendMessage(msg); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } //隐藏状态栏 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && Build.VERSION.SDK_INT >= 19) { View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); } }}清单文件设置<activity android:name=".activity.CompanyVideoActivity" android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation" android:launchMode="singleTop"/>最后放一些常用方法:
public void onFinishInflate() //从XML加载完所有子视图后调用。初始化控制视图(调用initControllerView方法,设置事件、绑定控件和设置默认值)。public void setAnchorView(View view) //设置MediaController绑定到一个视图上。例如可以是一个VideoView对象,或者是你的activity的主视图。 //参数 view 可见时绑定的视图public void setMediaPlayer(MediaPlayerControl player) //设置媒体播放器。并更新播放/暂停按钮状态。public void setInstantSeeking(boolean seekWhenDragging) //设置用户拖拽SeekBar时画面是否跟着变化。(VPlayer默认完成操作后再更新画面)public void show() //显示MediaController。默认显示3秒后自动隐藏。public void show(int timeout) //显示MediaController。在timeout毫秒后自动隐藏。 //参数 timeout 超时时间,单位毫秒。为0时控制条的hide()将被调用。public void setFileName(String name) //设置视频文件名称。public void setInfoView(OutlineTextView v)//设置保存MediaController的操作信息。例如进度改变时更新v。public void setAnimationStyle(int animationStyle) //更改MediaController的动画风格。 //如果MediaController正在显示,调用此方法将在下一次显示时生效。 //参数 animationStyle 在MediaController显示或隐藏时使用的动画风格。设置-1为默认风格,0没有动画,或设置一个明确的动画资源。public boolean isShowing() //获取MediaController是否已经显示。public void hide() //隐藏MediaController。public void setOnShownListener(OnShownListener l) //注册一个回调函数,在MediaController显示后被调用。public void setOnHiddenListener(OnHiddenListener l) //注册一个回调函数,在MediaController隐藏后被调用。public boolean onTouchEvent(MotionEvent event) //调用show()并返回true。public boolean onTrackballEvent(MotionEvent ev) //调用show()并返回false。public void setEnabled(boolean enabled) //设置MediaController的可用状态。包括进度条和播放/暂停按钮。 protected View makeControllerView() //创建控制播放的布局视图。子类可重写此方法创建自定义视图。
新闻热点
疑难解答