Menus

Showing posts with label android videoview. Show all posts
Showing posts with label android videoview. Show all posts

Wednesday 24 February 2021

Android code for Displaying a video with VideoView



By the help of MediaController and VideoView classes, we can play the video files in android.

 - Dsiplaying loacal videos


- Create an application that displays a video clip, which is stored locally 

in our applications “res” directory


- Create a row directory

      Paste video file to raw directory


-  Add VideoView to your UI


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.lb.videoplay;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

VideoView videoView = (VideoView) findViewById(R.id.videoView);

//Attach a media controller to video view
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);

//Specify the location of media file
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.favicon);

videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();



}
}





Android playing video from url with video view






previous tutorial we are discuss about

how to play video on videoview from local file


First set to internet permission


  your manifest must include the permissions:

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


Then and to video url


Check this code


package com.lb.videoplay;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

VideoView videoView = (VideoView) findViewById(R.id.videoView);

//Attach a media controller to video view
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);

//add your video URL
Uri uri = Uri.parse("Add URL here..");


videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();



}
}



videoView can't open this video Online video


Create file res/xml/network_security_config.xml


<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>



New in the AndroidManifest.xml file under application:



android:networkSecurityConfig="@xml/network_security_config"