Apr 10, 2013

Create intent to specified video or playlist in Youtube App

Before start coding using YouTube Android Player API, read the post to prepare your development tools.

com.google.android.youtube.player.YouTubeIntents of the YouTube Android Player API provides static methods that create intents which navigate to specific activities within the main YouTube application.


Example:

Create intent to specified video in Youtube App


package com.example.androidplayvideointent;

import com.google.android.youtube.player.YouTubeIntents;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
 
 private static final String VIDEO_ID = "fhWaJi1Hsfo";
 
 Button startYoutube;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  startYoutube = (Button)findViewById(R.id.startyoutube);
  startYoutube.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intentStartYoutube = 
      YouTubeIntents.createPlayVideoIntent(getApplicationContext(), VIDEO_ID);
    startActivity(intentStartYoutube);
   }});
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/startyoutube"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Youtube App" />

</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads