在安卓系统中停止的地方恢复视频



我想恢复上次关闭的视频。我试过了,但问题是当我关闭视频时,last_position被正确保存。但当我再次打开该视频时,last_position为0,这意味着视频从一开始就打开。如何获取onPause((方法中存在的last_position的值并在onResume((中使用?

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, PlayerActivity.class);
intent.putExtra("position", position);
activity.startActivity(intent);
}
}  
public class  PlayerActivity extends AppCompatActivity{
SimpleExoPlayer player;
long last_position;
PlayerView playerView;
int position = 0;
ArrayList<Files> myFiles = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getIntent().getIntExtra("position", 0);
String path = myFiles.get(position).getPath();
Uri uri = Uri.parse(path);
player= new SimpleExoPlayer.Builder(this).build();
DataSource.Factory factory = new DefaultDataSourceFactory
(this, Util.getUserAgent(this, "My App"));
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ProgressiveMediaSource.Factory(factory, extractorsFactory).createMediaSource(uri);
playerView.setPlayer(player);
playerView.setKeepScreenOn(true);
player.prepare(mediaSource);
if (last_Position > 0){
simpleExoPlayer.seekTo(last_Position);
}
player.setPlayWhenReady(true);
@Override
protected void onPause() {
super.onPause();
player.setPlayWhenReady(false);
player.getPlaybackState();
if (player != null){
State.last_Position = (int) player.getCurrentPosition();
State.click_Position = position;
}
}

@Override
protected void onResume() {
super.onResume();
if (position != State.click_Position){                
State.last_Position = 0;
}
player.seekTo(State.last_Position);
player.getPlaybackState();
player.setPlayWhenReady(true);
}

abstract static class State {
public static int last_Position = 0;
public static int click_Position = -1;
}
}

步骤1:创建一个静态变量

public  static int  lastPositionTime = 0;

步骤2:在onPause状态下保存此变量中的当前位置。检查一个条件

if(lastPositionTime == 0){
//save your current position
}

步骤3:在OnResume中调用seekTo方法并传递lastPositionTime

注意:您的最后一个位置变量不是静态的,所以它被回滚到默认值。请为localPostion 使用静态变量

尝试使用以下代码,它将对您有所帮助。

//create a class for manage state or sharedPref
abstract class State {
public  static int  lastPosition = 0;
public  static int  clickPosition = -1;
}
//update below line in your onPause()
if (player != null){
State.lastPosition= player.getCurrentPosition();
State.clickPosition = position;
}
//remove code from below method
@Override
protected void onResume() {
super.onResume();
player.seekTo(State.last_Position);
player.getPlaybackState();
player.setPlayWhenReady(true);
}
// on the click of video you can check if position is same as State.clickPosition then it is not new video
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(postion != State.clickPosition){
//this is new video you can reset position
State.lastPosition = 0;
}
Intent intent = new Intent(activity, PlayerActivity.class);
intent.putExtra("position", position);
activity.startActivity(intent);
}
}  

playerView.setPlayer(player);
playerView.setKeepScreenOn(true);
player.prepare(mediaSource);
//here you can pass your last position
if(State.lastPosition>0) player.seekTo(State.lastPosition);
player.setPlayWhenReady(true);

相关内容

  • 没有找到相关文章

最新更新