1-2 秒后暂停 YouTube 视频 [英] Pausing youtube video after 1-2 seconds

查看:20
本文介绍了1-2 秒后暂停 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Youtube Player api 在我的应用程序中播放 youtube 视频.视频开始播放并在 1-2 秒后暂停

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

我创建了视频片段和视图组.随后我创建了一些 Youtobe videoview.

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

视频片段

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;
    }

}

创建Youtobe videoview的功能

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;
}

推荐答案

不能按照 Google 的规定在播放器上方添加按钮作为叠加层,否则播放器会停止:

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

请注意,在播放视频时,此视图的最小大小为200x110 dp.如果您缩小视图,视频将自动停止播放.此外,不允许叠加在视频播放时与其他视图一起查看.

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.

此视图不支持填充.为了达到同样的效果,换行另一个 ViewGroup 中的视图或给它留边距.

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

YouTubePlayer 也不支持填充.

Padding is also not supported on YouTubePlayer.

要将您的视图叠加到视频上,我建议您使用 ExoPlayer,它不是 android sdk 的一部分,但由 google 推荐并包含在 android 开发人员文档中:

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 允许您流式传输任何类型的视频,而不仅仅是 Youtubes 视频.

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

值得一提的是,YouTube 应用程序中使用了 Exoplayer.

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

这篇关于1-2 秒后暂停 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆