如何在同一活动中创建YouTubePlayer和youtube缩略图视图?

时间:2016-09-17 18:29:07

标签: android xml youtube-api

我必须创建一个示例应用程序,使用youtubeAPI在同一活动中实现视频和缩略图。在我收到以下错误时

  Caused by: android.view.InflateException: Binary XML file line #27: Binary XML file line #27: Error inflating class fragment

使用以下xml代码

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="450dp"
            android:layout_height="200dp"
            android:id="@+id/frame"
            android:layout_centerHorizontal="true">

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        </FrameLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="title"
            android:gravity="center"
            android:id="@+id/videotitle"
            android:layout_below="@+id/frame"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <fragment
            android:name="com.thiyagu.healthytip.Fragment.RecentPlaylistFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/relatedplaylist"/>

    </RelativeLayout>

使用以下活动

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
import com.thiyagu.healthytip.Config;
import com.thiyagu.healthytip.R;

public class YoutubeVideoPlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_REQUEST = 1;
    private YouTubePlayerView youTubeView;
    String uri;
    TextView videotitle;
    String title;

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

        Intent intent =getIntent();
        uri = intent.getStringExtra("uri");
        title = intent.getStringExtra("title");
        youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        videotitle = (TextView)findViewById(R.id.videotitle);
        videotitle.setText(title);
        youTubeView.initialize(Config.YOUTUBE_API_KEY, this);


    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        if (!b) {
            youTubePlayer.cueVideo(uri);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        if (youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(this, RECOVERY_REQUEST).show();
        } else {
            String error = String.format(getString(R.string.player_error), youTubeInitializationResult.toString());
            Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_REQUEST) {
            getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);

        }
    }
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return youTubeView;
    }


}

注意:      1.我正在使用YouTubeBaseActivity。      2.启动YouTubePlayer.OnInitializedListener      3.使用YouTubeThumbnailView

0 个答案:

没有答案