如何在scrollview中使用youtube playerview?

时间:2016-05-17 09:36:43

标签: android android-layout youtube-api

当我在Scrollview中使用youtube playerview时。我得到以下错误:

05-17 16:39:16.899 28246-28246/com.example.mgic.projectmovie E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.mgic.projectmovie, PID: 28246
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mgic.projectmovie/com.example.mgic.projectmovie.Video}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
        at android.app.ActivityThread.access$800(ActivityThread.java:138)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5045)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at com.example.mgic.projectmovie.Video.onCreate(Video.java:64)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2188)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2273)
        at android.app.ActivityThread.access$800(ActivityThread.java:138)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5045)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
        at dalvik.system.NativeStart.main(Native Method) 

以下是我的布局:

  <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/_80sdp"
            android:layout_marginBottom="@dimen/_55sdp"
            android:layout_marginRight="@dimen/_30sdp"
            android:layout_marginLeft="@dimen/_30sdp"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <com.google.android.youtube.player.YouTubePlayerView
                    android:id="@+id/youtube_player"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </LinearLayout>
        </ScrollView>

2 个答案:

答案 0 :(得分:2)

如果在其上绘制任何其他布局,Youtube播放器视图不会播放视频。因此,如果您在滚动视图中使用它,它确保它不会播放并抛出重叠错误日志。在这种情况下要考虑的方法是在滚动视图中显示视频的缩略图,点击缩略图将用户导航到仅包含播放器视图并在那里播放视频的新独立布局。

答案 1 :(得分:0)

我能够让YouTubePlayerFragmentNestedScrollView内工作。简短的回答是我有一个根级布局(浮动容器)跟踪ImageViewNestedScrollView(视频缩略图)的位置。当我想加载播放器时,片段管理器将它添加到根级别布局。播放器将继续播放,除非它在屏幕外滚动,并检测到叠加。

一些代码:

...
<android.support.v4.widget.NestedScrollView
    android:id="@+id/topic_modules_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/topic_modules"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"/>
</android.support.v4.widget.NestedScrollView>

<FrameLayout
    android:id="@+id/topic_youtube_floating_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:background="@android:color/transparent"
    android:clickable="false"
    android:focusable="false" />
...

然后添加滚动更改逻辑

        final DetailYouTubeVideoFragment ytf = CustomYouTubeVideoFragment.newInstance(object, null);

        mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                final View fragmentView = ytf.getView();
                if (fragmentView != null) {
                    final int sY = v.getScrollY(),
                            top = fragmentView.getTop() + v.getPaddingBottom(),
                            newTop = top - sY;

                    mYoutubeFloatingLayout.setTop(newTop);
                    mYoutubeFloatingLayout.setBottom(newTop + fragmentView.getHeight());
                }
            }
        });

        mYoutubeFloatingLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                // prevent repositioning when player state changes
                if (top != oldTop) {
                    mYoutubeFloatingLayout.setTop(oldTop);
                    mYoutubeFloatingLayout.setBottom(oldBottom);
                }
            }
        });

我的DetailYouTubeVideoFragment的字段为YouTubePlayerFragment。在我的片段中,播放器片段被插入到浮动容器中......

getFragmentManager()
            .beginTransaction()
            .replace(R.id.topic_youtube_floating_fragment_container, mYouTubePlayerFragment)
            .commit();

虽然这适用于我的应用程序,但它可能不适用于所有情况。祝你好运!