Android : Playing a Video using YouTube Player API in Tab Activity - Android

on Tuesday, September 23, 2014


I'm using youtube player api to play a video link in my application. I got it to work. However when I put the YouTube video activity into a tab activity it doesn't work. YouTube Player Api does not support being in a tab host. Am I right? If so, does anyone know how I would get around this whilst still having the activity in a tabbed activity.


Here is my youTube Player activity



public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "AIzaSyA5z1mePQPtHof4uCUHBMbObNJ9had_6wo";
static private final String VIDEO = "ROfJkJqqUYY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
player.loadVideo(VIDEO);
}
}


Here is the layout file for the youtube activity:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@drawable/background_i5"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dip"
android:text="Hello YouTube API" />

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dip" />
</LinearLayout>

</LinearLayout>


Here is the tab host activity



public class TabbyActivity extends TabActivity {
/** Called when the activity is first created. */
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.tabhost2);
Resources res = getResources(); // Resource object to get Drawables
tabHost = getTabHost();
TabHost.TabSpec spec;


tabHost.getTabWidget().setBackgroundResource(R.drawable.tab_bar);
tabHost.setBackgroundResource(R.drawable.tab_bar);

tabHost.addTab(tabHost
.newTabSpec("Watch")
.setIndicator("",
getResources().getDrawable(R.drawable.read_selector))
.setContent(new Intent(this, ReadCourse.class)));

tabHost.addTab(tabHost
.newTabSpec("Watch")
.setIndicator("",
getResources().getDrawable(R.drawable.watch_selector))
.setContent(new Intent(this, MainActivity.class)));

tabHost.addTab(tabHost
.newTabSpec("Listen")
.setIndicator("",
getResources().getDrawable(R.drawable.listen_selector))
.setContent(new Intent(this, ListenCourse.class)));


tabHost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(null);
tabHost.getTabWidget().getChildTabViewAt(1).setBackgroundDrawable(null);
tabHost.getTabWidget().getChildTabViewAt(2).setBackgroundDrawable(null);


tabHost.setCurrentTab(0);

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

public void onTabChanged(String tabId) {


}
});

}

}


Here is the layout file for the tab host:



<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">



<LinearLayout
android:id="@+id/linear_layout_0"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>


</TabHost>


Any help is much appreciated thanks.


0 comments:

Post a Comment