Dec 24, 2012

YouTube Android Player API Tutorial


YouTube Android API is experimental
"The YouTube Android Player API is an experimental API version, which means it is still in development, though we do not expect major interface changes. Until the experimental label is removed, the Deprecation Policy for YouTube APIs won't apply to this version as discussed in the API Terms of Service."

Note:
  • Users need to run version 4.2.16 of the mobile YouTube app (or higher) to use the API.
  • Change API Key before compiling & running source code.
Let's make simple demo using YouTube Android API. Demo contains one EditText and YouTubePlayerView. Just Provide YouTube video id and press "Go" button to start video.



Step 1 : Download YouTube Android API :  download

Step 2 : Create Android Project named "YouTubeAPIDemo" using eclipse.

Step 3 : Register this demo to get API key

https://developers.google.com/youtube/android/player/register

Step 3 : Unzip YouTube Android API.
             Go to folder -> libs
             Copy "YouTubeAndroidPlayerApi.jar"
             Paste in Android Project : YouTubeAPIDemo->libs
             Right click on YouTubeAndroidPlayerApi.jar -> Build Path -> Add to build path.

 Step 4 : Add YouTube Video Player View in activity_main.xml, player view display in bold.
<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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="#C0C0C0"
        android:text="Enter Only Video ID in below EditText example : https://www.youtube.com/watch?v=fhWaJi1Hsfo"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText
        android:id="@+id/eturl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:hint="Enter YouTube URL and Press Enter"
        android:text="fhWaJi1Hsfo"
        android:singleLine="true"
        android:imeOptions="actionGo" >
    </EditText>

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubeplayer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/eturl"
        >
    </com.google.android.youtube.player.YouTubePlayerView>
    
</RelativeLayout>

Step 5 : Let's modify main activity code

package com.kpbird.youtubeapidemo;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

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.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class MainActivity extends YouTubeBaseActivity 
 implements YouTubePlayer.OnInitializedListener,OnEditorActionListener {

 private YouTubePlayerView ytpv;
 private YouTubePlayer ytp;
 private EditText et;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
  ytpv.initialize("YOUR API KEY", this);
  
  et = (EditText) findViewById(R.id.eturl);
  et.setOnEditorActionListener(this);
 }

 @Override
 public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
  Toast.makeText(this, "Initialization Fail", Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasrestored) {
  ytp = player;
  Toast.makeText(this, "Initialization  Success", Toast.LENGTH_LONG).show();
 }

 @Override
 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
  if(actionId == EditorInfo.IME_ACTION_GO ){
   if(ytp !=null){
    ytp.loadVideo(et.getText().toString());
   }
  }
  return false;
 }
}
  • Extends class with YouTubeBaseActivity
  • Implements YouTubePlayer.OnInitializedListener 
  • Call initialize method with API key and class object
  • YouTubePlayer.loadVideo(<video id>)
You have to use YouTubeBaseActivity instand of Activity and implement interface named "YouTubePlayer.OnInitializedListener" to check initialization status and to get YouTubePlayer control.

Download Demo : Click
[ File->Download to download entire zip file]

Reference
  1. https://developers.google.com/youtube/android/player/
  2. https://developers.google.com/youtube/android/player/register
  3. https://developers.google.com/youtube/android/player/register


continue reading YouTube Android Player API Tutorial

Dec 4, 2012

Android Annotation

What is Annotation ?
Annotation in Programming Language ?
History of Annotation in Java ?
Use of Annotation ?
Can I use Java’s Annotation in Android ?
Is there any Annotation framework available for Android ?


What is Annotation ?
“An annotation is a note that is made while reading any form of text.” Standard definition of Annotation.

Annotation in Programming Language ?
“Initially annotation was mainly use to extend documents and comments, Annotation was ignored in compile and execution process, Today annotation is greatly improved in language like Java, C#,etc. Many functionality and frameworks are build upon annotation. Annotations offer a way to associate metadata with program elements i.e. classes, interfaces, and methods. They can be thought of as additional modifiers without changing the generated code for those elements.” - Annotation in Programming Language

History of Annotation in Java ?

In Java annotation was introduction as JSR-175 in 2002 as named Metadata Facilities of Java Programming language, It was approved in 2004 by JCP. Till this date annotation was ignored by compiler and used to build javadoc, now it is not possible to ignore annotation so that JSR-269 was submitted in 2005 to make provision to process annotation at compile time. JSR-269 was  approved in 2006 after that Annotation was official part of Java language and shipped with JDK 1.5

Use of Annotation ?

  • Generate API Document - Annotation can be use in documentation.
  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime, few frameworks are based on Annotation, Servlet 3.0 for configuration.

Can I use Java’s Annotation in Android ?
Only few basic annotation like @Override can be used in newer version.

Is there any Annotation framework available for Android ?
Yes, Android Annotation [ http://androidannotations.org/ ] is good framework.

Android Annotation

It is good annotation library which allow you to manage and maintain all Android specific objects with annotation.

Referring UI Element Example

// without annotation

Button btn = (Button) findViewById(R.id.mybutton);
// with annotation
@ViewId
Button btn;


Features

  • Dependency injection: inject views, extras, system services, resources, ...
  • Simplified threading model: annotate your methods so that they execute on the UI thread or on a background thread.
  • Event binding: annotate methods to handle events on views, no more ugly anonymous listener classes!
  • REST client: create a client interface, AndroidAnnotations generates the implementation.
  • AndroidAnnotations provide those good things and even more for less than 50kb, without any runtime perf impact!

To Learn AndroidAnnotation Visit following links


continue reading Android Annotation

Nov 15, 2012

Android API Differences between 16 and 17 : Part-4


List of Added Classes,methods and fields in Android API 17 (S to Z)

sendBroadcastAsUser
sendOrderedBroadcastAsUser
sendStickyBroadcastAsUser
sendStickyOrderedBroadcastAsUser
setCompoundDrawablesRelativeWithIntrinsicBounds
setLabeledBy
setLabelFor
setLayoutDirection
setOnDismissListener
setTextLocale
widgetCategory
 in  android.R.attr 
continue reading Android API Differences between 16 and 17 : Part-4

Android API Differences between 16 and 17 : Part-3


List of Added Classes,methods and fields in Android API 17 (A to R)

apply
createBitmap
createConfigurationContext
createDisplayContext
dispatchGenericMotionEvent
extendVerificationTimeout
FLAG_SINGLE_USER
flags
getCreatorPackage
getCreatorUid
getCreatorUserHandle
getLabelFor
getLayoutDirection
getTextLocale
glGetActiveAttrib
glGetActiveUniform
initialKeyguardLayout
 in  android.R.attr 
isDestroyed
removeStickyBroadcastAsUser

continue reading Android API Differences between 16 and 17 : Part-3