0

由于Android YouTube Player API需要 Activity 来扩展YouTubeBaseActivity ,因此除了全屏播放器或使用compatlib的 YouTube Intent 之外,很难支持任何内容。

是否可以使用嵌入式YouTubePlayerViewYouTubePlayerFragment但仍向后兼容 FroYo(即使用 compatlib 或 ActionBarSherlock)?

4

2 回答 2

1

如果您查看示例应用程序,您会发现它使用了兼容性库。

于 2013-01-13T03:51:43.950 回答
1

无需扩展YouTubeBaseActivity。相反,您可以使用 YouTubePlayerSupportFragment 来实现相同的目的。通过这样做,您可以使用 compatlibs。

这是示例:

public class HomeScreen extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {


private  YouTubePlayer mYouTubePlayer;
private final String VIDEO_CODE="repalce with video code";
private final String API_KEY="replace with your api key ";
private  YouTubePlayerSupportFragment mYouTubePlayerSupportFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    youTubePlayerSetup();
}

private void youTubePlayerSetup(){

    mYouTubePlayerSupportFragment =
            (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);
    mYouTubePlayerSupportFragment.initialize(API_KEY, this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
    if (!b) {
        mYouTubePlayer = youTubePlayer;
        mYouTubePlayer.loadVideo(VIDEO_CODE);
}}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}}

并在您的 {layout}.xml 中将 YoutubePlayerView 替换为片段。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.HomeScreen">
    <fragment
        android:id="@+id/youtube_player_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
于 2018-11-07T10:18:26.170 回答