挣扎于 Youtube 播放器支持片段

Posted

技术标签:

【中文标题】挣扎于 Youtube 播放器支持片段【英文标题】:Struggling with Youtube Player Support Fragment 【发布时间】:2013-12-16 14:46:09 【问题描述】:

我试图在片段中使用 Youtube 播放器支持片段,但应用程序总是崩溃 (NullPointerException),我无法找到任何类似的帖子来修复它。

我有 import import android.support.v4.app.Fragment 所以这应该不是问题。

这就是我的片段类的样子:

package com.example.activitydetector;
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.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayerView;
import systemManager.SystemManager;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GuidelinesFragment extends YouTubePlayerSupportFragment 

    SystemManager sm;
    YouTubePlayerView youTubeView; 
    String URL_VIDEO = "CaA-k1l0xa4";
    String KEY_DEVELOPER = "AIzaSyBIIs0u0NXhsZguv8nCNvSzUmflTt7K1Ek";

    public GuidelinesFragment() 
        super();
        // TODO Auto-generated constructor stub
    

    public void onCreate(Bundle savedInstanceState) 
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


    

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

        View view = inflater.inflate(R.layout.youtube, container, false);


        YouTubePlayerSupportFragment youTubePlayerSupportFragment = (YouTubePlayerSupportFragment) getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);

        youTubePlayerSupportFragment.initialize(KEY_DEVELOPER, new OnInitializedListener() 

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

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

            
        );

        return view;
    


这是我完全简单的“Youtube”布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context=".MainActivity" >

<fragment
    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
    android:id="@+id/youtubeplayerfragment"
    android:layout_
    android:layout_/>

这是日志显示的错误:

    11-30 16:33:56.419: W/dalvikvm(19375): threadid=1: thread exiting with uncaught exception (group=0x40f14258)
11-30 16:33:56.423: E/AndroidRuntime(19375): FATAL EXCEPTION: main
11-30 16:33:56.423: E/AndroidRuntime(19375): java.lang.NullPointerException
11-30 16:33:56.423: E/AndroidRuntime(19375):    at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.os.Handler.handleCallback(Handler.java:605)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.os.Looper.loop(Looper.java:137)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at android.app.ActivityThread.main(ActivityThread.java:4645)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-30 16:33:56.423: E/AndroidRuntime(19375):    at dalvik.system.NativeStart.main(Native Method)

任何帮助或提示将不胜感激。没有运气,我已经浪费了大约 4 个小时。

【问题讨论】:

【参考方案1】:

我之前遇到过这个问题,我相信这个问题源于试图夸大YouTubePlayerSupportFragment 布局。我通过创建这样的片段解决了我的问题:

public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment 
    private String currentVideoID = "video_id";
    private YouTubePlayer activePlayer;

    public static PlayerYouTubeFrag newInstance(String url) 
        PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();

        Bundle bundle = new Bundle();
        bundle.putString("url", url);

        playerYouTubeFrag.setArguments(bundle);

        return playerYouTubeFrag;
    

    private void init() 
        initialize(DeveloperKey.DEVELOPER_KEY, new OnInitializedListener() 

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1)  
            

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) 
                activePlayer = player;
                activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                if (!wasRestored) 
                    activePlayer.loadVideo(getArguments().getString("url"), 0);

                
            
        );
    

    @Override
    public void onYouTubeVideoPaused() 
        activePlayer.pause();
    

然后像这样调用片段的一个实例:

PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance("video_id");
getSupportFragmentManager().beginTransaction().replace(R.id.video_container, myFragment).commit();

在我的例子中,video_container 是一个空框架布局。

【讨论】:

@Cristiana214 也许实例化一个新片段并传递一个新的 video_id。然后调用片段管理器来替换不同的容器,传入你新创建的片段。 @CzarMatt 我无法运行您在调用实例上方发布的代码“getSupportFragmentManager()”为红色 您忘记了一件事情,这对于使其工作至关重要(至少对我来说是这样)就在下面一行:“playerYouTubeFrag.setArguments(bundle);”,您需要添加:“播放器YouTubeFrag.init();"。这会初始化启动视频的 init() 类。 另外,由于我确信人们会遇到与我相同的问题,因此当您传入视频的 URL 时,您并没有传入 youtube URL(即:“youtube.com/watch?v=z7PYqhABiSo&feature=youtu.be" ) 您只传入了视频 ID(即:“z7PYqhABiSo”)。由于它是 Google 自己的文档,因此它知道如何使用该 ID 播放它。 @CzarMatt 为什么 onYouTubeVideoPaused 有 @Override 注释? YouTubePlayerSupportFragment 中没有这种方法。【参考方案2】:

CzarMatt 成功了。不过,为了补充他的答案,不要忘记您需要调用 init() 方法。为此,请遵循 CzarMatt 的代码并添加一行:

public static PlayerYouTubeFrag newInstance(String url)     
    PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();

    Bundle bundle = new Bundle();
    bundle.putString("url", url);

    playerYouTubeFrag.setArguments(bundle);

    playerYouTubeFrag.init(); //This line right here

    return playerYouTubeFrag;

此外,由于我确信人们会遇到与我相同的问题,即使 CzarMatt 上面提到过,只是重申一下,当您传递视频的 URL 时,您并没有传递 youtube URL

即用:"youtube.com/watch?v=z7PYqhABiSo&amp;feature=youtu.be"

您只传递了视频 ID

即使用:"z7PYqhABiSo"

由于是谷歌自己的文档,它知道如何用 id 玩它就好了。

【讨论】:

以上是关于挣扎于 Youtube 播放器支持片段的主要内容,如果未能解决你的问题,请参考以下文章

video.js 视频播放器是不是支持 youtube 视频?

Android youtube API 在一个主要活动中播放两个或多个 Youtube 播放器

youtube播放器api是否适用于谷歌驱动器嵌入式视频?

控制 youtube 电影播放器

IE11 JavaScript(错误:SCRIPT445)“对象不支持此操作”

Android YouTube Player API Fragment无法手动处理触摸事件