我正在开发使用Kaltura原生android sdk播放视频的android应用程序。 android-native-kaltura-sdk 我可以播放视频。但是当我改变移动方向(纵向到风景)时,播放器不会恢复。
问题是玩家重启。
VideoDetailActivity Class
public class VideoDetailActivity extends AppCompatActivity implements DraggableListener, OnTaskFinishedListener, KPEventListener {
public boolean isPlaying;
private static final int FULL_SCREEN_FLAG = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
private PlayerFragment mPlayerFragment;
// TODO: Rename and change types of parameters
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_detail);
ButterKnife.bind(this);
if (getFragmentManager().findFragmentByTag(PlayerFragment.class.getName()) == null) {
Log.d(TAG, this + ": Existing fragment not found.");
isPlaying = false;
mPlayerFragment = new PlayerFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();//.setCustomAnimations(R.anim.slide_up, R.anim.slide_down);
// transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
transaction.add(R.id.fragment_container, mPlayerFragment, PlayerFragment.class.getName());
transaction.addToBackStack(mPlayerFragment.getClass().getName());
transaction.commit();
} else {
if (savedInstanceState != null) {
isPlaying = savedInstanceState.getBoolean("isPlaying");
}
mPlayerFragment = (PlayerFragment) getFragmentManager().findFragmentByTag(PlayerFragment.class.getName());
Log.d(TAG, this + ": Existing fragment found.");
}
}
/**
* Pause the VideoView content.
*/
private void pauseVideo() {
if (player != null) {
player.releaseAndSavePosition();
}
}
/**
* Resume the VideoView content.
*/
private void startVideo() {
if (player != null) {
player.resumePlayer();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//player.releaseAndSavePosition();
mPlayerFragment.pausePlayer();
// mPlayerFragment.killPlayer();
outState.putBoolean("isPlaying", isPlaying);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//player.resumePlayer();
//isPlaying = savedInstanceState.getBoolean("isPlaying");
/* if (isPlaying) {
mPlayerFragment.resumePlayer();
}*/
}
public void isPlaying(boolean value) {
isPlaying = value;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPlayerFragment != null) {
mPlayerFragment.killPlayer();
}
// if (player != null)
//player.resumePlayer();
}
@Override
protected void onPause() {
super.onPause();
if (!isFinishing() && mPlayerFragment != null) {
//mPlayerFragment.killPlayer();
/* mPlayerFragment = null;*/
isPlaying = true;
}
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
mPlayerFragment.pausePlayer();
} else {
super.onBackPressed();
}
}
}
PlayerFragment Class
public class PlayerFragment extends Fragment implements KPEventListener {
private boolean isResumed = false;
private View mFragmentView = null;
private PlayerViewController mPlayerView;
private boolean mFullScreen = true;
private OnFragmentInteractionListener mListener;
public PlayerFragment() {
// Required empty public constructor
}
public static PlayerFragment newInstance(String param1, String param2) {
PlayerFragment fragment = new PlayerFragment();
/* Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);*/
return fragment;
}
public void pausePlayer() {
mPlayerView.releaseAndSavePosition();
}
public void killPlayer() {
mPlayerView.removePlayer();
mPlayerView = null;
//getActivity().getFragmentManager().beginTransaction().remove(this).commit();
}
public void resumePlayer() {
mPlayerView.resumePlayer();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
/* if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}*/
// getActivity().getActionBar().hide();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
mFragmentView = inflater.inflate(R.layout.fragment_player, container, false);
return mFragmentView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mPlayerView = (PlayerViewController) mFragmentView.findViewById(R.id.player);
mPlayerView.loadPlayerIntoActivity(getActivity());
if (!((VideoDetailActivity) getActivity()).isPlaying) {
((VideoDetailActivity) getActivity()).isPlaying(true);
KPPlayerConfig config = new KPPlayerConfig("http://cdnapi.kaltura.com", "Player Cofig ID", "Kaltura Partner ID");
config.setEntryId("Video EntryID");
mPlayerView.initWithConfiguration(config);
mPlayerView.addEventListener(this);
} else {
mPlayerView.resumePlayer();
}
}
public void setFullScreen(boolean fullScreen) {
fullScreen = false;
if (mFullScreen) {
Log.v("FullScreen", "-----------Set full screen SCREEN_ORIENTATION_LANDSCAPE------------");
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
/* int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height=height;
params.setMargins(0, 0, 0, 0);*/
//set icon is full screen
mFullScreen = fullScreen;
} else {
Log.v("FullScreen", "-----------Set small screen SCREEN_ORIENTATION_PORTRAIT------------");
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
/* final FrameLayout mFrame = (FrameLayout) findViewById(R.id.videoSurfaceContainer);
// int height = displaymetrics.heightPixels;
int height = mFrame.getHeight();//get height Frame Container video
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height= height;
params.setMargins(0, 0, 0, 0);*/
//set icon is small screen
mFullScreen = !fullScreen;
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onKPlayerStateChanged(PlayerViewController playerViewController, KPlayerState state) {
switch (state) {
case READY:
Log.e("KPlayer State Changed", "Player State READY :- " + state.toString() + "###################################################################");
((VideoDetailActivity) getActivity()).isPlaying(true);
break;
case LOADED:
Log.e("KPlayer State Changed", "Player State LOADED :- " + state.toString() + "###################################################################");
break;
case PAUSED:
Log.e("KPlayer State Changed", "Player State PAUSED :- " + state.toString() + "###################################################################");
break;
case SEEKED:
Log.e("KPlayer State Changed", "Player State PAUSED :- " + state.toString() + "###################################################################");
break;
case SEEKING:
Log.e("KPlayer State Changed", "Player State PAUSED :- " + state.toString() + "###################################################################");
break;
}
}
@Override
public void onKPlayerPlayheadUpdate(PlayerViewController playerViewController, float currentTime) {
Log.e("KPlayer State Changed", "Player State PAUSED :- " +currentTime + "###################################################################");
}
@Override
public void onKPlayerFullScreenToggeled(PlayerViewController playerViewController, boolean isFullscreen) {
setFullScreen(isFullscreen);
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}