我已经实施了播放器,现在出现了问题。当视频正在播放并且应用关闭并恢复时,视频屏幕会冻结。我甚至看到了来自Google的ExoPlayer演示活动以便更好地理解,但我无法通过它在我的应用程序中实现它。我在这里附加了Player Activity,对于完整的代码,我正在共享GitHub存储库,用于完整的文件集。
RecipeStepDetailFragment.java
package com.example.android.recipe.ui;
import android.content.Context;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.recipe.R;
import com.example.android.recipe.pojo.Recipe;
import com.example.android.recipe.pojo.Step;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import java.util.ArrayList;
import java.util.List;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveVideoTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.squareup.picasso.Picasso;
import static com.example.android.recipe.ui.RecipeActivity.SELECTED_INDEX;
import static com.example.android.recipe.ui.RecipeActivity.SELECTED_RECIPES;
import static com.example.android.recipe.ui.RecipeActivity.SELECTED_STEPS;
public class RecipeStepDetailFragment extends Fragment {
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private BandwidthMeter bandwidthMeter;
private ArrayList<Step> steps = new ArrayList<>();
private int selectedIndex;
private Handler mainHandler;
ArrayList<Recipe> recipe;
String recipeName;
public RecipeStepDetailFragment() { }
private ListItemClickListener itemClickListener;
public interface ListItemClickListener {
void onListItemClick(List<Step> allSteps,int Index,String recipeName);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView;
mainHandler = new Handler();
bandwidthMeter = new DefaultBandwidthMeter();
itemClickListener =(RecipeDetailActivity)getActivity();
recipe = new ArrayList<>();
if(savedInstanceState != null) {
steps = savedInstanceState.getParcelableArrayList(SELECTED_STEPS);
selectedIndex = savedInstanceState.getInt(SELECTED_INDEX);
recipeName = savedInstanceState.getString("Title");
}
else {
steps =getArguments().getParcelableArrayList(SELECTED_STEPS);
if (steps!=null) {
steps =getArguments().getParcelableArrayList(SELECTED_STEPS);
selectedIndex=getArguments().getInt(SELECTED_INDEX);
recipeName=getArguments().getString("Title");
}
else {
recipe =getArguments().getParcelableArrayList(SELECTED_RECIPES);
steps=(ArrayList<Step>)recipe.get(0).getSteps();
selectedIndex=0;
}
}
View rootView = inflater.inflate(R.layout.recipe_step_detail_fragment_body_part, container, false);
textView = (TextView) rootView.findViewById(R.id.recipe_step_detail_text);
textView.setText(steps.get(selectedIndex).getDescription());
textView.setVisibility(View.VISIBLE);
simpleExoPlayerView = (SimpleExoPlayerView) rootView.findViewById(R.id.playerView);
simpleExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
String videoURL = steps.get(selectedIndex).getVideoURL();
if (rootView.findViewWithTag("sw600dp-port-recipe_step_detail")!=null) {
recipeName=((RecipeDetailActivity) getActivity()).recipeName;
((RecipeDetailActivity) getActivity()).getSupportActionBar().setTitle(recipeName);
}
String imageUrl=steps.get(selectedIndex).getThumbnailURL();
if (imageUrl!="") {
Uri builtUri = Uri.parse(imageUrl).buildUpon().build();
ImageView thumbImage = (ImageView) rootView.findViewById(R.id.thumbImage);
Picasso.with(getContext()).load(builtUri).into(thumbImage);
}
if (!videoURL.isEmpty()) {
initializePlayer(Uri.parse(steps.get(selectedIndex).getVideoURL()));
if (rootView.findViewWithTag("sw600dp-land-recipe_step_detail")!=null) {
getActivity().findViewById(R.id.fragment_container2).setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
simpleExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH);
}
else if (isInLandscapeMode(getContext())){
textView.setVisibility(View.GONE);
}
}
else {
player=null;
simpleExoPlayerView.setForeground(ContextCompat.getDrawable(getContext(), R.drawable.ic_visibility_off_white_36dp));
simpleExoPlayerView.setLayoutParams(new LinearLayout.LayoutParams(300, 300));
}
Button mPrevStep = (Button) rootView.findViewById(R.id.previousStep);
Button mNextstep = (Button) rootView.findViewById(R.id.nextStep);
mPrevStep.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (steps.get(selectedIndex).getId() > 0) {
if (player!=null){
player.stop();
}
itemClickListener.onListItemClick(steps,steps.get(selectedIndex).getId() - 1,recipeName);
}
else {
Toast.makeText(getActivity(),"You already are in the First step of the recipe", Toast.LENGTH_SHORT).show();
}
}});
mNextstep.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int lastIndex = steps.size()-1;
if (steps.get(selectedIndex).getId() < steps.get(lastIndex).getId()) {
if (player!=null){
player.stop();
}
itemClickListener.onListItemClick(steps,steps.get(selectedIndex).getId() + 1,recipeName);
}
else {
Toast.makeText(getContext(),"You already are in the Last step of the recipe", Toast.LENGTH_SHORT).show();
}
}});
return rootView;
}
private void initializePlayer(Uri mediaUri) {
if (player == null) {
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
DefaultTrackSelector trackSelector = new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
simpleExoPlayerView.setPlayer(player);
String userAgent = Util.getUserAgent(getContext(), "Baking App");
MediaSource mediaSource = new ExtractorMediaSource(mediaUri, new DefaultDataSourceFactory(getContext(), userAgent), new DefaultExtractorsFactory(), null, null);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}
}
@Override
public void onSaveInstanceState(Bundle currentState) {
super.onSaveInstanceState(currentState);
currentState.putParcelableArrayList(SELECTED_STEPS,steps);
currentState.putInt(SELECTED_INDEX,selectedIndex);
currentState.putString("Title",recipeName);
}
public boolean isInLandscapeMode( Context context ) {
return (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}
@Override
public void onDetach() {
super.onDetach();
if (player!=null) {
player.stop();
player.release();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (player!=null) {
player.stop();
player.release();
player=null;
}
}
@Override
public void onStop() {
super.onStop();
if (player!=null) {
player.stop();
player.release();
}
}
@Override
public void onPause() {
super.onPause();
if (player!=null) {
player.stop();
player.release();
}
}
}
完整的项目存储库:https://github.com/mtp2697/Udacity-AndroidDeveloperNanodegree-BakingApp
帮助恢复onPause()和onResume()
的视频播放器状态谢谢, Praveen Thirumurugan。
答案 0 :(得分:11)
您可以在暂停时存储玩家位置:
position = player.getCurrentPosition(); //then, save it on the bundle.
然后当你恢复它时,如果它在那里,你可以这样做:
if (position != C.TIME_UNSET) player.seekTo(position);
在prepare()
方法{。}}之前。
好的,我克隆了这个项目,并让它发挥作用。我改变的主要是:
我添加了之前说过的内容,然后:
initializePlayer()
我制作了videoUri global
position = C.TIME_UNSET;
if (savedInstanceState != null) {
//...your code...
position = savedInstanceState.getLong(SELECTED_POSITION, C.TIME_UNSET);
}
添加onResume:
videoUri = Uri.parse(steps.get(selectedIndex).getVideoURL());
更新暂停:
@Override
public void onResume() {
super.onResume();
if (videoUri != null)
initializePlayer(videoUri);
}
和onSaveInstanceState:
@Override
public void onPause() {
super.onPause();
if (player != null) {
position = player.getCurrentPosition();
player.stop();
player.release();
player = null;
}
}
上次,我删除了currentState.putLong(SELECTED_POSITION, position);
onDetach
onDestroyView
。
显然这是&#34;只是为了让它工作&#34;,你将需要更多地工作。
答案 1 :(得分:3)
我知道这是一个旧线程,但这是我的解决方法
protected void onPause() {
player.setPlayWhenReady(false);
super.onPause();
}
protected void onResume() {
player.setPlayWhenReady(true);
super.onResume();
}
这将在活动暂停时暂停视频,并在活动恢复后继续播放视频。