Pausing youtube video after 1-2 seconds

跟風遠走 提交于 2019-12-23 09:01:05

问题


I am using an Youtube Player api for playing youtube videos in my application. Video start playing and pausing after 1-2 seconds

I created Video Fragment and ViewGroup. Subsequently I create some youtobe videoview.

VideoFragment

public static final class VideoFragment extends YouTubePlayerSupportFragment implements
        OnInitializedListener
{

    private YouTubePlayer player;
    private String videoId;

    public static VideoFragment newInstance()
    {
        return new VideoFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        initialize(Constants.DEVELOPER_KEY, this);
    }

    @Override
    public void onDestroy()
    {
        if (player != null)
        {
            player.release();
        }
        super.onDestroy();
    }

    public void setVideoId(String videoId)
    {
        if (videoId != null && !videoId.equals(this.videoId))
        {
            this.videoId = videoId;
            if (player != null)
            {
                player.cueVideo(videoId);
            }
        }
    }

    public void pause()
    {
        if (player != null)
        {
            player.pause();
        }
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
            boolean restored)
    {
        this.player = player;
        if (!restored && videoId != null)
        {
            player.cueVideo(videoId);
        }
    }

    @Override
    public void onInitializationFailure(Provider provider, YouTubeInitializationResult result)
    {
        this.player = null;
    }

}

Function for creating Youtobe videoview

private ViewGroup createYouTubePlayer(final VideoData data, final FrameLayout youTubePlayer)
{

    youTubePlayer.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            FragmentManager fm = mActivity.getSupportFragmentManager();

            if (v.getId() == mCurrentYouTubePlayer)
            {
                return;
            }

            VideoFragment fragment = (VideoFragment) fm.findFragmentById(mCurrentYouTubePlayer);
            if (fragment == null)
            {
                fragment = VideoFragment.newInstance();
                fragment.setVideoId(data.srcPath);

                fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
                mCurrentYouTubePlayer = v.getId();
            }
            else
            {
                fm.beginTransaction().remove(fragment).commit();

                fragment = VideoFragment.newInstance();
                fragment.setVideoId(data.srcPath);
                fm.beginTransaction().add(youTubePlayer.getId(), fragment).commit();
                mCurrentYouTubePlayer = v.getId();
            }
        }
    });



    return youTubePlayer;
}

回答1:


It is not possible to add buttons as overlays above the player as specified by Google, otherwise the player will stop:

https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubePlayerView

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay the view with other views while a video is playing.

This view does not support padding. To achieve the same effect, wrap the view in another ViewGroup or give it margins.

Padding is also not supported on YouTubePlayer.

To overlay your view on video, I will recommend you to use ExoPlayer, It is not part of the android sdk, but it's recommended by google and included in android developer documentation :

http://google.github.io/ExoPlayer/

Exoplayer allow you to stream any kind of video, not only Youtubes videos.

It's also good to mention that Exoplayer is used in youtube application.




回答2:


The problem may be because there is some object above com.google.android.youtube.player.YouTubePlayerView or the video has padding property



来源:https://stackoverflow.com/questions/23015127/pausing-youtube-video-after-1-2-seconds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!