片段在观看视频时滚动[YouTube API]

时间:2014-12-15 12:23:08

标签: java android android-fragments youtube-data-api android-youtube-api

我想在其中一个片段(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();
    }
}

它可以工作,但在观看视频屏幕时会向上滚动,视频会暂停。

所以,我的问题是:

  1. 为什么在观看时屏幕会滚动?
  2. 如果我没有弄错的话,视频播放应该在一个单独的线程中,因为播放器初始化会延迟主线程。在这种情况下是否应该考虑细微差别?
  3. 感谢您的回答。

0 个答案:

没有答案