Search code examples
androidyoutube-apiactionbarsherlockmultiple-inheritance

Inheriting ActionBarSherlock and Android YouTubePlayer


I've developed an app for playing youtube videos using ActionBarSherlock.

Now that the YouTubePlayer api for android is available (here), I want to integrate this into my app to improve playback and controls.

I've run into an issue, in that I need to use multiple inheritance for my activity to both extend SherlockActivity and also YouTubeBaseActivity.

I checked out this article to try to understand multiple inheritance in Java, but frankly it's over my head.

If I attempt to do something like this I get the issue that I can't instantiate SherlockActivity.

Anyone have some concrete example of how to extend both classes? Has anyone had to extend both SherlockActivity and some other class, and how did you accomplish?


Solution

  • I had the same problems - I wanted to add YouTube player to my app, but albo I don't wanted to delete Sherlock from it (based on support library). And what is bad, I wasnt able to use any of the playbers, because I got errors (inflating fragment, YouTubePlayerView cant start without special Activity and so on).

    What worked: I used SherlockFragmentActivity, FragmentManager (getSupportFragmentManager()) and YouTubePlayerSupportFragment. Instead of adding it to XML, I created everything from code. My layout looks like this:

    <RelativeLayout 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"
    tools:context=".MainActivity" >
    
    <LinearLayout
        android:id="@+id/fragmentz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    

    and Java code:

    package com.example.youtubetesting;
    
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    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.YouTubePlayerSupportFragment;
    import com.google.android.youtube.player.YouTubePlayer.Provider;
    import android.os.Bundle;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    
    public class MainActivity extends SherlockFragmentActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
    
        YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
        fragmentTransaction.add(R.id.fragmentz, fragment);
        fragmentTransaction.commit();
    
        fragment.initialize("Your API KEY HERE",
                new OnInitializedListener() {
    
                    @Override
                    public void onInitializationSuccess(Provider arg0,
                            YouTubePlayer arg1, boolean arg2) {
                        if (!arg2) {
                            arg1.loadVideo("wKJ9KzGQq0w");
                        }
                    }
    
                    @Override
                    public void onInitializationFailure(Provider arg0,
                            YouTubeInitializationResult arg1) {
                    }
    
                });
    }
    
    }
    

    I dont know why Android was returning errors when I was inflating views in normal way, but this works perfectly.