You Tube Integration
Now I am going to explain, how to integrate you tube player in android.
1. Create a new project in the Eclipse IDE from File ⇒ Android Application Project and fill all required details. I named my main activity as MainActivity.java
In MainActivity.java
package com.androidtutorialpoint;
1. Create a new project in the Eclipse IDE from File ⇒ Android Application Project and fill all required details. I named my main activity as MainActivity.java
In MainActivity.java
package com.androidtutorialpoint;
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.YouTubePlayerView; import com.google.android.youtube.player.YouTubePlayer.Provider; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { static private final String DEVELOPER_KEY = "API KEY"; static private final String VIDEO = "XpB-uFUfyLQ"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtubevideo); 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); } }Add your API Key in the above required line.
2. In order to create an View, we need xml layout files.
Open your activity_main.xml
In activity_main.xml
<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:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dip" android:text="You tube" /> <com.google.android.youtube.player.YouTubePlayerView android:id="@+id/youtubevideo" android:layout_width="440dp" android:layout_height="wrap_content" android:layout_margin="12dip" android:layout_weight="0.64" /> </LinearLayout>
Finally add internet permission in manifest.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>The output screen will be like this.
Please check your output in real device.
Download source code here
0 comments: