如何使用另一个片段中的YouTubePlayerFragment加载YouTubePlayer? (机器人)

我想使用API​​中的YouTubePlayerFragment加载一个片段中的YoutubePlayer

我的片段的.xml文件是:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- VIDEO CONTENT -->



    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="@dimen/layout_horizontal_margin"
        android:paddingRight="@dimen/layout_horizontal_margin"
        android:paddingTop="@dimen/layout_vertical_margin"
        android:paddingBottom="@dimen/layout_vertical_margin" >


        <fragment
            android:name="com.google.android.youtube.player.YouTubePlayerFragment"
            android:id="@+id/youtubeplayer_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        </LinearLayout>


    <!-- LYRIC CONTENT -->

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/af_background"
            android:layout_centerInParent="true" />



        <!-- TEXT STRUCTURE -->

        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/lyric_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true" >



            <LinearLayout
                android:id="@+id/start_layout"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_horizontal"
                android:layout_centerInParent="true">

                <TextView
                    android:id="@+id/text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/lyric_margin_top"

                    android:gravity="center"
                    android:text="@string/textTitle2"
                    android:textSize="@dimen/lyric_size"
                    android:textIsSelectable="false"/>

            </LinearLayout>

        </LinearLayout>


        <!-- FRONT BACKGROUND -->


        <ImageView
            android:id="@+id/high_part_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/af_background_up_side"
            android:scaleType="fitStart"/>

        <ImageView
            android:id="@+id/low_part_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:src="@drawable/af_background_down_side"
            android:scaleType="fitEnd"/>

    </RelativeLayout>


</LinearLayout>

你可以看到,我已经添加

现在加载这个.xml的片段是:

public class MyFragment extends Fragment{


    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The content this fragment is presenting.
     */
    private Items.ItemList  mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public MyFragment() {

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

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the content specified by the fragment
            // arguments. In a real-world scenario,use a Loader
            // to load content from a content provider.
            mItem = Items.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(android.R.layout.titles_fragment,container,false);



        // Show the  content as text in a TextView.
        if (mItem != null) {
            ((TextView) rootView.findViewById(android.R.id.textTitle2)).setMovementMethod(new ScrollingMovementMethod());

        }
        return rootView;
    }
}

最后,我有一个ActivityFragment实现“YouTubePlayer.OnInitializedListener”,从中我得到该片段.

public class ItemDetailActivity extends FragmentActivity implements YouTubePlayer.OnInitializedListener{

    private TextView mTextView;
    private ImageView hImageView;
    private ImageView lImageView;

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    public static final String VIDEO_ID = "BJVlU7d-4x0";

    private YouTubePlayer youTubePlayer;
    private YouTubePlayerFragment youTubePlayerFragment;

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

        if (savedInstanceState == null) {
            // Create the detail fragment and add it to the activity
            // using a fragment transaction.
            fragmentTransaction(getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));


        }
    }

    @Override
    public boolean onoptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:

                NavUtils.navigateUpTo(this,new Intent(this,ItemListActivity.class));
                return true;
        }
        return super.onoptionsItemSelected(item);
    }


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


            Toast.makeText(this,"YouTubePlayer.onInitializationFailure(): " + result.toString(),Toast.LENGTH_LONG).show();

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored) {

        Toast.makeText(getApplicationContext(),"YouTubePlayer.onInitializationSuccess()",Toast.LENGTH_LONG).show();



        if (!wasRestored) {
            player.cueVideo(VIDEO_ID);
        }

    }

    private void fragmentTransaction(String id) {

        Bundle arguments = new Bundle();
        Fragment fragment;

        String content = Items.ITEM_MAP.get(id).content;


        if(content.equalsIgnoreCase("Title 1")) {
            // Fragment transaction
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID,id);
            fragment = new ItemDetailFragment();
            fragment.setArguments(arguments);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_detail_container,fragment)
                    .commit();
        }


        if(content.equalsIgnoreCase("Title 2")) {
            // Fragment transaction
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID,id);
            fragment = new MyFragment);
            fragment.setArguments(arguments);
            FragmentManager fm = getSupportFragmentManager();
            //youTubePlayerFragment = fragment.getChildFragmentManager().findFragmentById(R.id.youtubeplayer_fragment);
            //youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager().findFragmentById(R.id.youtubeplayer_fragment);

            //Fragment ytbfragment = fragment.getChildFragmentManager().findFragmentById(R.id.youtubeplayer_fragment);
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.item_detail_container,fragment);
            //ft.add(R.id.youtubeplayer_fragment,youTubePlayerFragment);

            ft.commit();

            //youTubePlayerFragment.initialize(API_KEY,this);
        }


        if(content.equalsIgnoreCase("Title 3")) {
            // Fragment transaction
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID,id);
            fragment = new ItemDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_detail_container,fragment)
                    .commit();
        }
    }
}

标题2中,我得到的片段和我想要使用不同的方法获取YouTube播放器的内部片段,但它不起作用.
总是崩溃加载.xml试图让玩家形成当我尝试获得de YoutubeFragment.

如果我只加载原始片段,应用程序可以很好地获取xml文件.

有什么问题?

解决方法

我已经在这个问题上工作了几天,我终于找到了一个解决方案.我会在这里发送代码好吗?
@Override
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
    View fragmentYoutubeView = inflater.inflate(R.layout.fragment_youtube,false);
    mYoutubePlayerFragment = new YouTubePlayerSupportFragment();
    mYoutubePlayerFragment.initialize(youtubeKey,this); 
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_youtube_player,mYoutubePlayerFragment);
    fragmentTransaction.commit();

    mYoutubeVideoTitle = (TextView)fragmentYoutubeView.findViewById(R.id.fragment_youtube_title);
    mYoutubeVideoDescription = (TextView)fragmentYoutubeView.findViewById(R.id.fragment_youtube_description);

    mYoutubeVideoTitle.setText(getArguments().getString(Resources.KEY_VIDEO_TITLE));
    mYoutubeVideoDescription.setText(getArguments().getString(Resources.KEY_VIDEO_DESC));

    VideoFragment.setTextToShare(getArguments().getString(Resources.KEY_VIDEO_URL));

    return fragmentYoutubeView;
}

在这代码中,我通过一个Intent的YouTube视频ID(url),标题和描述,并将它们放在textViews中.要播放youtubePlayerFragment,我使用一个frameLayout(fragment_youtube_player),就像这样:

<FrameLayout
    android:id="@+id/fragment_youtube_player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

我重量为1,因为包含Frame和textViews的LinearLayout的权重为3,强制视图占据我告诉他们的空间.

问题的工具包在这里

mYoutubePlayerFragment = new YouTubePlayerSupportFragment();
    mYoutubePlayerFragment.initialize(youtubeKey,mYoutubePlayerFragment);
    fragmentTransaction.commit();

代码以编程方式替换了一个youtubePlayerSupportFragment的FrameLayout.所有这一切都在一个Android片段中,它的工作原理.

编辑1:

我看到了Fragment如何管理Youtube的API的成功和失败.澄清我的片段标题看起来像这样:

public class FragmentYoutubePlayerGenerator extends Fragment
implements YouTubePlayer.OnInitializedListener{

而这两种实现方法相当简单:

@Override
public void onInitializationSuccess(Provider provider,boolean wasRestored) {  
    if(!wasRestored){
        player.cueVideo(mVideoInfo.getId());
    }
}

@Override
public void onInitializationFailure(Provider provider,YouTubeInitializationResult result) {   
    if (result.isUserRecoverableError()) {
        result.getErrorDialog(this.getActivity(),1).show(); 
    } else {
        Toast.makeText(this.getActivity(),Toast.LENGTH_LONG).show(); 
    }
}

我离开开发人员的选择离开或不离开result.toString()在错误处理,一些用户不会喜欢看到他们不了解的很多数字在他们的屏幕上,但也很好,因为他们可以快照并将其发送给您来调试错误.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android岛屿数量算...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andro...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android数据压缩的方法是什么”文章能帮助大家解决疑惑...
这篇“Android怎么使用Intent传大数据”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...
本文小编为大家详细介绍“Android事件冲突怎么解决悬浮窗拖拽问题”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android事件冲突怎么解决悬浮窗拖拽问题”文...
这篇文章主要介绍了Android拼接如何实现动态对象的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android拼接如何实现动态对象文...
今天小编给大家分享一下Android全面屏适配怎么实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下...
本篇内容介绍了“Android怎么开发Input系统触摸事件分发”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何...
今天小编给大家分享一下AndroidRoom怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下...
本文小编为大家详细介绍“AndroidRoom使用方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“AndroidRoom使用方法有哪些”文章能帮助大家...
这篇文章主要介绍“Android中的OpenGL怎么配置使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android中的Open...
这篇文章主要介绍了Android如何自定义自动识别涂鸦工具类的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android如何自定义自动...
今天小编给大家分享一下Android如何自定义有限制区域的图例角度自识别涂鸦工具类的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以...
今天小编给大家分享一下ReactNative错误采集原理在Android中如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章...
这篇文章主要讲解了“Android崩溃日志收集和保存代码分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“A...
这篇“Android面向单Activity开发实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大...
本篇内容介绍了“Android应用启动白屏处理的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何...