Struggling with Youtube Player Support Fragment
Asked Answered
O

2

11

Im trying to use the Youtube Player Support Fragment in a fragment but the app always crash (NullPointerException) and I have not been able to find any similar post to fix it.

I have import import android.support.v4.app.Fragment so that should not be the problem.

This is how my fragment class looks like:

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;
    }

}

This is my totally and simple "Youtube" layout:

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

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

And this is the error that the log shows:

    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)

ANY help or hint would be deeply grateful. I have already wasted around 4 hours without luck.

Osteopath answered 30/11, 2013 at 15:42 Comment(0)
D
28

I ran into this problem before and I believe the issue stemmed from trying to inflate the YouTubePlayerSupportFragment layout. I solved my issue by creating a fragment like this:

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();
    }
}

And then call an instance of the fragment like this:

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

Where video_container in my case was an empty frame layout.

Decimate answered 3/12, 2013 at 22:38 Comment(11)
how about if i want to play 2 or more videos at the same time?Victim
@Cristiana214 Perhaps instantiate a new fragment and pass it a new video_id. Then call the fragment manager to replace a different container, passing in your newly created fragment.Decimate
@Decimate i can't run the code you posted above the calling instance "getSupportFragmentManager()" is in redVictim
@Decimate please help me with this #22190302Victim
I have the same problem. I've implemented your solution and there are no exceptions, yet I got black screen where the youtube fragment i.e. the video play doesn't start.Kelsi
@Kelsi Are you getting sound at least? Make sure the YouTube application is updated on your device.Decimate
I've solved the issue and edited your answer but it was deleted. In the solution you've provided, init() has to be called in OnCreate /*just onCreate*/ in order to get video play.Kelsi
One thing you forgot, which is essential to make it work (at least for me it was) is RIGHT beneath the line: "playerYouTubeFrag.setArguments(bundle);", you need to add: "playerYouTubeFrag.init();". This initializes the init() class, which starts the video.Stable
Also, since I'm sure people will run into the same issue I did, when you pass in a URL for the video, you are NOT passing in the youtube URL (IE: "youtube.com/watch?v=z7PYqhABiSo&feature=youtu.be") You are passing in only the video ID (IE: "z7PYqhABiSo"). Since it is Google's own documentation, it knows how to play it just fine with the id.Stable
@Decimate Why does onYouTubeVideoPaused have @ Override annotation? There's no such method in YouTubePlayerSupportFragment.Blip
@Decimate Why are you setting player style to PlayerStyle.DEFAULT, when that's already being used, since it's the default style?Blip
S
6

CzarMatt nailed it. Just to add to his answer though, don't forget you need to call the init() method. To do so follow CzarMatt's code and add one line:

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;
}

Also, since I'm sure people will run into the same issue I did, even though CzarMatt mentioned it above, just to reiterate, when you pass in a URL for the video, you are NOT passing in the youtube URL

That is, with: "youtube.com/watch?v=z7PYqhABiSo&feature=youtu.be"

You are passing in only the video ID

I.e. use: "z7PYqhABiSo"

Since it is Google's own documentation, it knows how to play it just fine with the id.

Stable answered 15/1, 2015 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.