directory

  • Final rendering
  • preface
  • Layout file
  • The use of the VideoView
  • Switching between horizontal and vertical screens
  • File selection
  • Gestures to adjust volume
  • The last

Final rendering


preface

Here with VideoView to write a player, you can horizontal and vertical screen, you can select files, you can pause, you can fast forward backward, you can drag the progress bar, you can touch the screen to adjust the volume. Let’s see how it works!


Layout file

Wrapping VideoView around a RelativeLayout is important, the general Settings will be distorted. Of course, you also need to rewrite onConfigurationChanged, as shown below.

<? xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.so.mymedia.ui.activity.MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout
        android:id="@+id/rl_vv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:minHeight="200dp">

        <VideoView
            android:id="@+id/vv_video"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</RelativeLayout>
Copy the code

The use of the VideoView

VideoView is very simple to use, set up MediaController, and then set URI or Path, and then start. The main points here are some implementations of the functionality used. You can check the official documentation.


Switching between horizontal and vertical screens

The first step is to go to the configuration file. In the activity under the label add android: configChanges = “the rid_device_info_keyboard | orientation | screenSize”. That way, you don’t have to call onStop when you switch screens. We added the toggle portrait button to the Toolbar and overwrote onConfigurationChanged.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (mVvVideo == null) {
        return;
    }
    if(this) getResources (). GetConfiguration (). The orientation = = Configuration. ORIENTATION_LANDSCAPE) {/ / landscape getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().getDecorView().invalidate();float height = new ScreenUtil(this).getAppWidth();
        float width = new ScreenUtil(this).getAppHeight();
        mRlVv.getLayoutParams().height = (int) width;
        mRlVv.getLayoutParams().width = (int) height;
    } else{/ / vertical screen final WindowManager. LayoutParams attrs = getWindow (). The getAttributes (); attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setAttributes(attrs); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);float width = new ScreenUtil(this).getAppWidth();
        floatheight = DisplayUtil.dp2px(this, 200.f); mRlVv.getLayoutParams().height = (int) height; mRlVv.getLayoutParams().width = (int) width; }}Copy the code

There are several UITL inside, all of which are common packages, so I won’t go into details. This allows you to switch between horizontal and vertical screens.


File selection

Check out my previous article on file pickers. The next step is to return the selected file path. This is the normal use of intEnts. No more.


Gestures to adjust volume

Add a touch listener and implement it with gestures. The next step is to increase or decrease the volume depending on the direction you swipe up and down. The code to adjust the volume is also fairly common.

@Override
public boolean onTouch(View v, MotionEvent event) {
    return mGestureDetector.onTouchEvent(event);
}
Copy the code
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    float v = e2.getY() - e1.getY();
    if (Math.abs(v) > 10) {
        setVoiceVolume(v);
    }
    return true;
}
Copy the code
private void setVoiceVolume(floatvalue) { int currentVolume = mAM.getStreamVolume(AudioManager.STREAM_MUSIC); int maxVolume = mAM.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int flag = value > 0 ? 1:1; CurrentVolume += flag * 0.15 * maxVolume; mAM.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0); }Copy the code

The last

If you want to do more DIY, you can consider using SurfaceView, but VideoView will suffice most of the time. Like to remember to like or follow me, have comments or suggestions in the comments section.