gpt4 book ai didi

android - 1-2 秒后暂停 youtube 视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:47:09 26 4
gpt4 key购买 nike

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

我创建了 Video Fragment 和 ViewGroup。随后我创建了一些 youtobe videoview。

视频 fragment

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的函数

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 的规定在播放器上方添加按钮,否则播放器将停止:

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.

YouTubePlayer 也不支持填充。

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

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

Exoplayer 允许您流式传输任何类型的视频,而不仅仅是 Youtube 视频。

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

关于android - 1-2 秒后暂停 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com