I have stored some video files in android internal private storage and I
loaded those videos using android VideoView
. And then I tried to open last position of the video with onResume()
.
I got the last position of the video using videoView.getCurrentPosition()
(it comes as a int value).
I used videoView.seekTo(stopPosition);
to access last position.
And Couldn't see good out put and the video getting crash with showing the error "can't play this video".
I need to load the video from last viewed position with onResume()
method.
I am using 23 as the compileSdkVersion.
Sample code:
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call
toolbar.setVisibility(View.GONE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
videoViewAction = (LinearLayout) findViewById(R.id.videoview_action);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size
mRecyclerView.setAdapter(mAdapter); // Setting the adapter to RecyclerView
mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager
mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
videoView = (VideoView) findViewById(R.id.videoview);
videoView.setMediaController(new MediaController(this));
videoView.setMediaController(null);
final MediaController mediaController = new MediaController(this);
String path;
try {
path = getIntent().getExtras().get("url").toString();
videoView.setVideoPath(path);
videoView.start();
} catch (Exception e) {
int p = 1;
}
// rawer object Assigned to the view
videoViewAction.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (flag == true) {
toolbar.setVisibility(View.GONE);
videoView.setMediaController(mediaController);
mediaController.hide();
flag = false;
} else if (flag == false) {
toolbar.setVisibility(View.VISIBLE);
videoView.setMediaController(mediaController);
mediaController.show();
flag = true;
}
return true;
default:
return true;
}
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer vmp) {
onBackPressed();
}
});
/* VideoView.setMediaController(new MediaController(this));*/
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
if(stopPosition == 0)
{
videoView.start();
}
else {
videoView.seekTo(stopPosition);
videoView.start();
}
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_video, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.action_chapters:
Drawer.openDrawer(Gravity.RIGHT);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onResume() {
videoView.resume();
super.onResume();
videoView.requestFocus();
videoView.seekTo(stopPosition);
videoView.start();
}
@Override
protected void onPause() {
//super.onPause();
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
// videoView.pause();
videoView.suspend();
super.onPause();
}
}
Can anyone please give a good solution for this issue? Or Any other way to do that positioning?