youtubePlayerFragmnet - 无法创建片段来播放YouTube视频

时间:2014-09-29 20:21:53

标签: android android-fragments youtube

我需要在Fragment即FragmentVideo之一中播放youtube视频。 FragmentVideo是使用FragmentStatePagerAdapter创建的一个金额4。

当我滚动到一个页面时,FragmentVideo应该显示链接视频链接的视频形式。 我真的在想如何完成这件事。大多数示例都是创建一个Activity,但我需要在一个页面中,相应的片段应处理视频播放。 同样基于片段状态(暂停,恢复,onsavestateinstance)视频播放应该停止,恢复等。

请有人帮帮我。 我已经拥有所有网络访问权限。

    package com.test.myTestingapp;
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 android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragmentVideo extends Fragment implements YouTubePlayer.OnInitializedListener {
    public static final String ARG_SECTION_NUMBER = "section_number";
    public static final String TAG ="LOGTAG";
    public View rootView;

    public String videolink;
    public int loc;

    public static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    public static final String VIDEO_ID = "o7VVHhK9zf0";


    public static FragmentVideo newInstance(String param1, String param2) {
        FragmentVideo fragment = new FragmentVideo();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public FragmentVideo() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_video, container, false);

        videolink = getArguments().getString("VIDEOLINK");

        if(videolink == null) {

            return rootView;

        }
        Log.i(TAG, "FragmentVideo: received videolink: " + videolink);
        YouTubePlayerSupportFragment ypf = new YouTubePlayerSupportFragment();
        ypf.initialize(API_KEY, this);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.youtubeplayerfragment, ypf);
        fragmentTransaction.commit();
        return rootView;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

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

    }

    @Override
    public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
            boolean arg2) {
        // TODO Auto-generated method stub
        if(!arg2)
            arg1.cueVideo(videolink);
    }
}

LogCat输出:

    09-29 18:57:10.065: E/InputEventReceiver(1615): Exception dispatching input event.
09-29 18:57:10.065: D/AndroidRuntime(1615): Shutting down VM
09-29 18:57:10.069: W/dalvikvm(1615): threadid=1: thread exiting with uncaught exception (group=0xa4bb0648)
09-29 18:57:10.081: E/AndroidRuntime(1615): FATAL EXCEPTION: main
09-29 18:57:10.081: E/AndroidRuntime(1615): java.lang.NoClassDefFoundError: com.test.myTestingapp.FragmentVideo
09-29 18:57:10.081: E/AndroidRuntime(1615):     at com.test.myTestingapp.MainActivity$SectionsPagerAdapter.getItem(MainActivity.java:278)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1761)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:579)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:908)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2819)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at com.test.myTestingapp.MainActivity$1.onPageSelected(MainActivity.java:89)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:567)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2015)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.View.dispatchTouchEvent(View.java:7384)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2205)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1940)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1954)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
09-29 18:57:10.081: E/AndroidRuntime(1615):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)

我需要FragmentVideo播放youtube视频播放/暂停等。 我怎么能实现这个???

0 个答案:

没有答案