我在 android 内部私有存储中存储了一些视频文件,并使用 android 加载了这些视频VideoView
。然后我尝试用 . 打开视频的最后一个位置onResume()
。
我得到了视频的最后一个位置videoView.getCurrentPosition()
(它是一个 int 值)。
我曾经videoView.seekTo(stopPosition);
访问最后一个位置。并且看不到良好的输出,并且视频因显示错误“无法播放此视频”而崩溃。
我需要使用onResume()
方法从上次查看的位置加载视频。
我使用 23 作为 compileSdkVersion。
示例代码:
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();
}
}
任何人都可以为这个问题提供一个好的解决方案吗?或者任何其他方式来做这个定位?