Android plays video comprehensively

1 Overview

As a code Porter developed in C language, it is not easy to write an Android program. It took two days to read n posts. It felt that each post was incomplete, such as how to pass parameters before each function and how to display the results. So I wrote this blog, hoping it would be useful for Android beginners. The development environment is Android Studio, and the version is Android Studio 3.4 Canary 2. We won't go into details about Android Studio installation and SDK configuration here.

2 engineering realization

2.1. XML file

Create a new project video, modify the / RES / layout / activity main.xml file, and add a VideoView control in it. The complete activity main.xml file and screenshot are as follows,

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="0dp"
        android:layout_height="226dp"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="22dp"
        android:layout_marginRight="22dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

2.2. Java file

Modify the / java/com.example.video/Mainactivity.java file. The complete Mainactivity.java file and screenshot are as follows

package com.example.video;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        play_mp4();
    }

    private void play_mp4() {
        String videoUrl1="/sdcard/Yu_Pictures/Bottle gourd silk/Grassland beauty.mp4";
        Uri uri = Uri.parse(videoUrl1);
        videoView = this.findViewById(R.id.videoView);
        videoView.setMediaController(new MediaController(this));
        videoView.setVideoURI(uri);
        videoView.start();
    }
}

2.3 Android manifest.xml file

Modify the Android manifest.xml file and add the following 2 permissions

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The complete Android manifest.xm file and screenshot are as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.video">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>

3 function test

The test is as follows:

Tags: Android xml Java encoding

Posted on Thu, 28 Nov 2019 09:49:18 -0800 by brad_fears