Tuesday, April 5, 2016

Play Video with MediaController @ Media API

"Jai Saraswati Maa"

Hi ... Dear All ..Today I am uploading a superb example of playing Video with Media Controller using Media API.
In this example i played a video from sdcard/Video folder.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19 
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Please check on Real Device on emulator it will not enjoyable as compare to physical device.****** Please add permissions in manifest file ************

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

A. Layout File activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.exam.ravi.videoex1.MainActivity">


    <VideoView        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/videoView"        android:layout_alignParentBottom="true"

        android:layout_alignParentStart="true"

        android:layout_marginBottom="188dp" />
</RelativeLayout>

B. package com.exam.ravi.videoex1;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
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);

        //Creating MediaController

        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);

        //specify the location of media file

        Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+
                                   "/Video/hangover.mp4");

        //Setting MediaController and URI, then starting the videoView

        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.start();
    }
}

No comments:

Post a Comment