Tuesday 2 April 2013

Play Video in android

public class VideoPlay extends Activity {
    private VideoView videoView=null;
    private MediaController mc=null;
    private ProgressDialog mProgressDialog=null;
    private String strFileName  = null;
    private String strFilePath  = null;
    private int CurrentPosition = -1;
    private boolean isPaused = false;
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.videoview);
                getWindow().setFormat(PixelFormat.TRANSLUCENT);
                if(getIntent().getAction().equals(Constants.PLAY_VIDEO.PLAY_DONLOAD_VEDIO)){
                    strFileName = getIntent().getStringExtra(Constants.PLAY_VIDEO.DOWNLOAD_VIDIO_KEY);
                    strFilePath = Constants.PLAY_VIDEO.VIDEO_PATH + strFileName;
                }
                else{
                    strFilePath = getIntent().getStringExtra(Constants.PLAY_VIDEO.LIST_VIDEO_KEY);
                }
        }
       
        private void InitData(){
            mProgressDialog = ProgressDialog.show(this,"",Constants.PLAY_VIDEO.STREMING_VIDEO,true);
            mProgressDialog.setCancelable(true);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setOnDismissListener(new OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    if(videoView != null){
                        if(!videoView.isPlaying() && !isPaused){
                            CancelVideo();
                        }
                    }
                }
            });
           
            if(videoView == null){
                videoView = (VideoView) findViewById(R.id.surface_view);
                mc = new MediaController(this);
                mc.setAnchorView(videoView);
                mc.setMediaPlayer(videoView);
                Uri video = Uri.parse(strFilePath);
                System.out.println("path=="+strFilePath);
                videoView.setMediaController(mc);
                videoView.setVideoURI(video);
                videoView.requestFocus();
               
                videoView.setOnPreparedListener(new OnPreparedListener() {
                    public void onPrepared(MediaPlayer mp) {
                        if(mProgressDialog!=null){
                            if(mProgressDialog.isShowing()){
                                mProgressDialog.dismiss();
                            }
                        }
                        if (videoView != null && !videoView.isPlaying()){
                            videoView.start();
                        }
                       
                        if (videoView != null && CurrentPosition != -1){
                            videoView.seekTo(CurrentPosition);
                        }
                        Log.i("log_tag", "onPrepared");
                    }
                });
               
                videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                   
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        if(mProgressDialog!=null){
                            if(mProgressDialog.isShowing()){
                                mProgressDialog.dismiss();
                            }
                        }
                        Log.e("log_tag", "What: "+ what + " , Extra: " + extra);
                        return false;
                    }
                });
               
                Log.d("Before Video Finish", "i m in before video finish");
                videoView.setOnCompletionListener(new OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        CancelVideo();
                    }
                });
            }
            else{
                if (videoView != null && CurrentPosition != -1){
                    videoView.seekTo(CurrentPosition);
                }
            }
           
        }
       
        @Override
        protected void onResume() {
            super.onResume();
            isPaused = false;
            InitData();
        }
       
        @Override
        protected void onPause() {
            super.onPause();
            isPaused = true;
            if(mProgressDialog!=null){
                if(mProgressDialog.isShowing()){
                    mProgressDialog.dismiss();
                }
            }
            if (videoView != null){
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                CurrentPosition = videoView.getCurrentPosition();
            }
        }
       
        private void CancelVideo(){
            if (videoView != null){
                videoView.stopPlayback();
                videoView=null;
            }
            finish();
        }

}



Constans
========
public static final class PLAY_VIDEO{
        public static final String VIDEO_PATH = Environment.getExternalStorageDirectory() + File.separator + "ElterSein"+File.separator+"video_";
        public static final String LIST_VIDEO_KEY     = "list_video_key";
        public static final String PLAY_LIST_VEDIO = "play_list_vedio";
        public static final String DOWNLOAD_VIDIO_KEY = "download_vedio_key";
        public static final String PLAY_DONLOAD_VEDIO = "play_donload_vedio";
        public static final String STREMING_VIDEO = "Streming Video";
    }

No comments:

Post a Comment