我想在其中一个片段(YouTube Android Player API)中播放YouTube视频。实现视频片段的类如下所示:
public class VideoFragment extends YouTubePlayerSupportFragment {
private YouTubePlayer activePlayer;
public static VideoFragment newInstance(String url) {
VideoFragment playerYouTubeFrag = new VideoFragment();
Bundle bundle = new Bundle();
bundle.putString("url", url);
playerYouTubeFrag.setArguments(bundle);
playerYouTubeFrag.init();
return playerYouTubeFrag;
}
private void init() {
initialize(DeveloperKey.DEVELOPER_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
activePlayer = player;
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
activePlayer.loadVideo(getArguments().getString("url"), 0);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
@Override
public void onPause() {
activePlayer.pause();
}
}
然后我替换另一个片段中的FrameLayout
:
public class InfoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// ... some code ...
VideoFragment videoFragment = VideoFragment.newInstance("video_id");
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.videoLayout, videoFragment).commit();
}
}
它可以工作,但在观看视频屏幕时会向上滚动,视频会暂停。
所以,我的问题是:
感谢您的回答。