Inheriting ActionBarSherlock and Android YouTubePlayer

I developed an application to play YouTube videos using ActionBarSherlock.

Now that the YouTube app for the api player for Android is available ( here ), I want to integrate it into my application to improve playback and controls.

I ran into a problem because I need to use multiple inheritance for my activity to extend SherlockActivity as well as YouTubeBaseActivity .

I checked this article to try and understand multiple inheritance in Java, but frankly, this is on my head.

If I try to do something like this , I get a message stating that I cannot create an instance of SherlockActivity .

Does anyone have a concrete example of how to extend both classes? Someone had to extend both SherlockActivity and some other class, and how did you do it?

+4
source share
3 answers

I had the same problems - I wanted to add a YouTube player to my application, but albo I did not want to remove Sherlock from it (based on the support library). And what's bad, I could not use any of the players because I had errors (inflating a fragment, launching YouTubePlayerView without much action, etc.).

What worked: I used SherlockFragmentActivity, FragmentManager (getSupportFragmentManager ()) and YouTubePlayerSupportFragment. Instead of adding it to XML, I created everything from the code. My layout is as follows:

 <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 don’t know why Android was returning errors when I inflated my views in the usual way, but this works fine.

+9
source

You cannot use both YouTubeBaseActivity and SherlockActivity at the same time, at least not practical.

Instead, it's a lot easier if you just use SherlockFragmentActivity to host YouTubePlayerFragment

YouTubePlayerFragment contains YouTubePlayerView , as well as YouTubeBaseActivity , which allows you to play videos on YouTube.

If you need a tutorial on snippets on Android, you can run it here

+1
source

I copied the source code for the com.actionbarsherlock.app.SherlockActivity class and I put it in my project as my.package.SherlockYoutubeActivity , and I replaced " extends Activity " with " extends YouTubeBaseActivity ". in my activity, I inherited from this class as follows:

 public class MyVideoActivity extends SherlockYoutubeActivity implements OnInitializedListener 
+1
source

All Articles