Videoview, mediaplayer not resuming video on onResume

I an new to Android/Java and have been struggling to play video from the left over position before putting the app in background. I have read several posts outlining overriding onPause and onResume but unfortunately I cant seem to get any working. Right now all the videos play just fine except that if I put the app in BG and open it back again the video starts from beginning rather than resuming from where it was left. This is my code, please help.

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;


public class VideoPlayback extends Activity {
    private static final String TAG = Dashboard.class.getSimpleName();
    ProgressBar progressBar = null;
    VideoView vidView;
    int pos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_playback);
        Intent intent = getIntent();
        String message = intent.getStringExtra(Dashboard.EXTRA_MESSAGE);
        String link = "http://" + message;
        vidView = (VideoView) findViewById(R.id.myVideo);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        Uri vidUri = Uri.parse(link);
        Log.d(TAG, link);
        vidView.setVideoURI(vidUri);
        try {
            MediaController vidControl = new MediaController(this);
            vidControl.setAnchorView(vidView);
            vidView.setMediaController(vidControl);


        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        vidView.requestFocus();
        vidView.start();
        progressBar.setVisibility(vidView.VISIBLE);

        vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1, int arg2) {
                        // TODO Auto-generated method stub
                        Log.d(TAG, "Changed");
                        progressBar.setVisibility(vidView.GONE);
                        mp.start();
                    }
                });

            }

        });

    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(String.valueOf(event.getKeyCode())=="4"){
            finish();
            Log.v("key pressed", String.valueOf(event.getKeyCode()));
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG, "PAUSE");
        vidView.pause();
        pos=vidView.getCurrentPosition();
        Log.d("POS",String.valueOf(pos));
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Resume");
        Log.d("R_POS", String.valueOf(pos));
        vidView.seekTo(pos);
        //vidView.resume();
    }


}

Answers


Ok , so finally I got this going, one of the issues was that the video position upon pause was getting lost when app was going in background so I used onSaveInstanceState to retain the value and secondly the video I was streaming was .mp4 but was not seekable. Once I started using H.264 encoding everything fell in to place. Here is my code:

import android.os.Bundle;    

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;
import android.os.PowerManager;


    public class VideoPlayback extends Activity {
        private static final String TAG = Dashboard.class.getSimpleName();
        //protected PowerManager.WakeLock mWakeLock;

        ProgressBar progressBar = null;
        VideoView vidView;
        int pos;

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            pos = vidView.getCurrentPosition();
            vidView.pause();
            outState.putInt("position", pos);
            Log.d("SAVED-POS", String.valueOf(pos));
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if ( savedInstanceState != null ){
                pos = savedInstanceState.getInt("pos");
            }
            setContentView(R.layout.activity_video_playback);
           getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            Intent intent = getIntent();
            String message = intent.getStringExtra(Dashboard.EXTRA_MESSAGE);
            String link = "http://" + message;
            vidView = (VideoView) findViewById(R.id.myVideo);
            progressBar = (ProgressBar) findViewById(R.id.progressbar);
            Uri vidUri = Uri.parse(link);
            Log.d(TAG, link);
            vidView.setVideoURI(vidUri);
            try {
                MediaController vidControl = new MediaController(this);
                vidControl.setAnchorView(vidView);
                vidView.setMediaController(vidControl);


            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            vidView.requestFocus();
            vidView.start();
            progressBar.setVisibility(vidView.VISIBLE);

            vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();

                    mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mp, int arg1, int arg2) {
                            // TODO Auto-generated method stub
                            Log.d(TAG, "Changed");
                            progressBar.setVisibility(vidView.GONE);
                            mp.start();
                        }
                    });

                }

            });

        }
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if(String.valueOf(event.getKeyCode())=="4"){
                finish();
                Log.v("key pressed", String.valueOf(event.getKeyCode()));
            }
            return super.dispatchKeyEvent(event);
        }

        @Override
        public void onPause(){
            super.onPause();
            Log.d(TAG, "PAUSE");
            vidView.pause();
            pos=vidView.getCurrentPosition();
            Log.d("POS", String.valueOf(pos));
        }

        @Override
        public void onResume() {
            super.onResume();
            Log.d(TAG, "onResume called");
            vidView.seekTo(pos);
            Log.d("RESUME-POS", String.valueOf(pos));
            vidView.start(); 
        }

    }

Need Your Help

Can't Access Class Variable From WebSocket Server

python variables websocket

So I am trying to get the amount from progressive in the Progressive class. However, I have tried so many times to access it from the WebSocket class, but can't. What's wrong here?

How to resize a windows forms application in designer mode? (C# in Visual Studio 2013)

c# windows windows-8 visual-studio-2013 windows-forms-designer

I'm still new to Microsoft Visual Studio and C#, but to make a windows forms application a certain size in the designer mode in Visual Studio 2013, you just drag the ends, and it automatically make...