web-dev-qa-db-ja.com

YouTubeをフラグメントに統合する

私のアクティビティは別のアクティビティを拡張し、各アイテムが新しいフラグメントを開くナビゲーションドロワーで構成されています.YouTubeをフラグメントの1つに統合したいです。以前はYouTubePlayerViewを使用してYouTubeをアクティビティに統合しましたが、ここではフラグメントで必要です。 netとYouTubePlayerFragmentを見つけて、youtubeをfragmentに統合します。しかし、チュートリアルで検索すると、YouTubePlayerFragmentを使用してもYouTubeBaseActivityを拡張していることがわかりました。これらは例です。 http://Android-coding.blogspot.in/2013/04/example-to-use- youtubeplayerfragment-of.htmlhttp://Android-er.blogspot.in/2013/06/example-to-use-youtubeplayerfragment-of.html

私のクラスがプロジェクトで必要なYouTubeBaseActivityではなくFragmentを拡張するようにYouTubePlayerFragmentを使用する方法を理解できませんでした。このフラグメント内でYoutubeビデオを再生します。クラスの様子を簡単なレイアウトで示しています。

public class LandingActivity extends BaseGActivity {
.
.
.
.


public void selectDrawerItem(int position) {

        Bundle args = new Bundle();

        switch (position) {
            case 0:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
                GApplication.getInstance().stopGPlayer();
                break;
            case 1:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_PHOTO);
                GApplication.getInstance().stopGPlayer();
                break;
//
            case 2:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_AUDIO);
                GApplication.getInstance().stopGPlayer();
                break;

            case 3:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_VIDEO);
                GApplication.getInstance().stopGPlayer();
                break;
            case 4:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_MEME);
                GApplication.getInstance().stopGPlayer();
                break;
            case 5:
                currentFragment = new YoutubeFragment();
            default:
                currentFragment = new HomeFragment_();
                args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
                GApplication.getInstance().stopGPlayer();
                break;
        }

        currentFragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
                .commit();

        mDrawerList.setItemChecked(position, true);
        setTitle(dataList.get(position).getItemName());
        mDrawerLayout.closeDrawers();
    }


    public class YoutubeFragment extends Fragment implements YouTubePlayer.OnInitializedListener{
        private FragmentActivity myContext;

        private YouTubePlayer YPlayer;
        private static final String YoutubeDeveloperKey = "xyz";
        private static final int RECOVERY_DIALOG_REQUEST = 1;
        @Override
        public void onAttach(Activity activity) {

            if (activity instanceof FragmentActivity) {
                myContext = (FragmentActivity) activity;
            }

            super.onAttach(activity);
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);

            YouTubePlayerView youTubeView = (YouTubePlayerView) rootView.findViewById(R.id.youtube_view);
            youTubeView.initialize(YoutubeDeveloperKey, (YouTubePlayer.OnInitializedListener) myContext);
            return rootView;
        }
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider,
                                            YouTubeInitializationResult errorReason) {
            if (errorReason.isUserRecoverableError()) {
                errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
            } else {
                String errorMessage = String.format(
                        "There was an error initializing the YouTubePlayer",
                        errorReason.toString());
                Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == RECOVERY_DIALOG_REQUEST)
            {

                getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
            }
        }



        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                            YouTubePlayer player, boolean wasRestored) {
            if (!wasRestored) {
                YPlayer = player;
                YPlayer.setFullscreen(true);
                YPlayer.loadVideo("2zNSgSzhBfM");
                YPlayer.play();
            }
        }

    }

YouTubeFragment.Java

public class YoutubeFragment extends Fragment implements
        YouTubePlayer.OnInitializedListener {
    private FragmentActivity myContext;

private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;

@Override
public void onAttach(Activity activity) {

    if (activity instanceof FragmentActivity) {
        myContext = (FragmentActivity) activity;
    }

    super.onAttach(activity);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);

    YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

    youTubePlayerFragment.initialize("DEVELOPER_KEY", new YouTubePlayer.OnInitializedListener() {


    });
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
    return rootView;

}
    @Override
    public void onInitializationSuccess (YouTubePlayer.Provider provider, YouTubePlayer
    youTubePlayer,boolean b){
        if (!b) {
            YPlayer = youTubePlayer;
            YPlayer.setFullscreen(true);
            YPlayer.loadVideo("2zNSgSzhBfM");
            YPlayer.play();
        }
    }

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

    }
}

レイアウト

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">
    <fragment
        Android:name="com.google.Android.youtube.player.YouTubePlayerSupportFragment"
        Android:id="@+id/youtube_fragment"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"/>
</LinearLayout>

エラー-

Error:(64, 101) error: <anonymous com.pc.gi.ui.fragment.YoutubeFragment$1> is not abstract and does not override abstract method onInitializationFailure(Provider,YouTubeInitializationResult) in OnInitializedListener
26

最初に通常どおりアクティビティを拡張します

 class YourActivity extends Activity...

レイアウトファイルに以下の行を入れて

<fragment
  Android:name="com.google.Android.youtube.player.YouTubePlayerSupportFragment"
  Android:id="@+id/youtube_fragment"
  Android:layout_width="match_parent"
  Android:layout_height="wrap_content"/>

次に、アクティビティで、フラグメントのonCreareViewメソッドの以下の行を使用してインスタンスを作成できます。

YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.youtube_fragment);

または、xmlでFrameLayoutを宣言し、以下の行を使用してYouTubeSupportFragmentを開始できます

<?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:id="@+id/youtube_fragment"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_centerHorizontal="true"
        Android:visibility="visible" />

</RelativeLayout>

OnCreateViewのコード

 YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

    youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {

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

            if (!wasRestored) {
                YPlayer = player;
                YPlayer.setFullscreen(true);
                YPlayer.loadVideo("2zNSgSzhBfM");
                YPlayer.play();
            }

        }

        @Override
        public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
            // TODO Auto-generated method stub

        }
    });
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();

ここで重要なことは、YouTubePlayerSupportFragmentの代わりにYouTubePlayerFragmentを使用することです。

お役に立てれば。

ここにあなたのフラグメントがあります

import Android.app.Activity;
import Android.os.Bundle;
import Android.support.v4.app.Fragment;
import Android.support.v4.app.FragmentActivity;
import Android.support.v4.app.FragmentTransaction;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;

import com.google.Android.youtube.player.YouTubeInitializationResult;
import com.google.Android.youtube.player.YouTubePlayer;
import com.google.Android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.Android.youtube.player.YouTubePlayer.Provider;
import com.google.Android.youtube.player.YouTubePlayerSupportFragment;
import com.ismart.omanapp.R;

public class YoutubeFragment extends Fragment {
    private FragmentActivity myContext;

    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "xyz";
    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @Override
    public void onAttach(Activity activity) {

        if (activity instanceof FragmentActivity) {
            myContext = (FragmentActivity) activity;
        }

        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);

        YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();

        youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {

            @Override
            public void onInitializationSuccess(Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
                if (!b) {
                    YPlayer = youTubePlayer;
                    YPlayer.setFullscreen(true);
                    YPlayer.loadVideo("2zNSgSzhBfM");
                    YPlayer.play();
                }
            }

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
                // TODO Auto-generated method stub

            }
        });

    }
}
71
Sunny