Play Video from SDCard

Friends today we are going to learn how to play video from sd card with step by step.before starting it we learn about Video View and Medea Controller.


VideoView: This class is basically used to for the display of Video File. VideoView can also be used to load images for the various sources, having information about their measurements computation from the video so that images can be used in any kind of layout manager. Here are below important methods of this class:

void start(): This method is used to start the playback of video file.
void pause(): This method is used to pause the current playback.
boolean canPause(): This method will tell whether video view is able to pause.
boolean canSeekForward(): This method will return true value if video view is able to seek forward.
int getDuration(): This method is used to get the total duration of video view.
boolean isPlaying(): This method will return true value if the current video view is in play state.
void resume(): This method is used to play the resumed file.


MediaController: MediaController class is used to provide the controls for the video playback. It is used with the VideoView most of. MediaController contains the PAUSE, PLAY, FORWARD, REWIND, SEEKBAR which will sync with state of MediaPlayer.

Here are the below methods of this class which are mostly used:

void hide(): This method is use to hide the controls from the screen.
Boolean onTouchEvent(MotionEvent evnt): This method is used to handle the touch screen events.
void setEnabled(boolean value): This method is used to set the Enable state of this view.
void show(int timeOut): This method is used to set the time to show the controller on the screen of current activity.
void show(): This method is used to show the controller on the screeen of current activity.

Full Source code Example

public class video extends Activity{
 
    VideoView video_view;
    String ex_name;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eccryption);
        video_view = (VideoView) findViewById(R.id.videoView1);
 
        ex_name = getIntent().getExtras().getString("video_name");
 
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(video_view);
        video_view.setMediaController(new MediaController(this));
        handler.sendEmptyMessage(1);
 
    }
 
    Handler handler = new Handler(){
 
        public void handleMessage(Message msg){
 
            int pos=msg.what;
            if (pos == 1){
 
                video_view.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
                video_view.requestFocus();
                video_view.start();
 
                Log.d("Before Video Finish", "i m in before video finish");
                video_view.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        finish();
                    }
                });
            }
        }
    };
 

         




How to Load URL and PDF file in WebView

Hello Friends today we are going to learn about WebView and how to load URL and PDF file in android WebView.first we talk about what is webview in android.


Q. What is WebView?
Ans : WebView is a view that display web pages inside android application and if we want to display HTML Pages like As UI we can use WebView.

Example :- 
1.use in XML

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
/>
  
2. use after create an object

WebView browser = (WebView) findViewById(R.id.webview);
  OR
WebView browser = new wEBvIEW(this); 





Full code of  WebView


package com.artofandroid.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WevViewPdf extends AppCompatActivity {
    WebView mWebview;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWebview  = new WebView(this);
        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
        final Activity activity = this;

        mWebview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
        mWebview .loadUrl("http://docs.google.com/");
        setContentView(mWebview );
    }
}



Ho to Open PFD File  In Webview

Ans :- basically android not provide to open pdf directly bot we can open PDF by following below:-

·         Use a third party library (unfortunately, most open source pdf libs are GPL)
·         Open a pdf viewer app via an Intent
·         Use Google docs in a webview

if you want to open pdf webview you can use Google docs URl like below example

String myPdfUrl = "http://example.com/awesome.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;
Log.i(TAG, "Opening PDF: " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);