Android Search Play Youtube Videos API

Android Search Play Youtube Videos API – In this tutorial i will show how to create Android Search and Play Youtube Videos using YouTube Player Data v3 API. Most of developers used YouTube player API for their personal android application or company based projects. YouTube Player Data V3 API used for search and get the accurate results of the videos and play from YouTube android application or your virtual mobile device.

Apart from this article in your free time you can able to earn money online without any investments. Nowadays most of peoples are using those ways for getting money online from home based jobs. Make money online earning is very easy to do. The works are very easy like watching videos, playing games, view & click ads, Earn Money $3 per Day.

Here we saw the full explanation of YouTube API and how it’s working on Android and Play the Videos from YouTube Server using the official YouTube player Data API. Some of students request to publish using YouTube API to search and play videos from YouTube player that’s why here i have published.

Android Youtube Videos API

Let’s start, First create new project and choose Empty Activity after creating the project just open the default class of MainActivity.java class file and add the following below code. The full documentation is available on Google official website. After read the guideline article you get some idea related to the projects.

MainActivity.java

package com.test.vetbossel.searchyoutube;


import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import android.os.Handler;
import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    
    private EditText searchInput;
    private YoutubeAdapter youtubeAdapter;
    private RecyclerView mRecyclerView;
    private ProgressDialog mProgressDialog;
    private Handler handler;
    
    private List<VideoItem> searchResults;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressDialog = new ProgressDialog(this);
        searchInput = (EditText)findViewById(R.id.search_input);
        mRecyclerView = (RecyclerView) findViewById(R.id.videos_recycler_view);
        
        mProgressDialog.setTitle("Searching...");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        
        mRecyclerView.setHasFixedSize(true);
       
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        handler = new Handler();
        
        searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            
            //onEditorAction method called when user clicks ok button or any custom
            //button set on the bottom right of keyboard
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    
                    mProgressDialog.setMessage("Finding videos for "+v.getText().toString());
                    
                    mProgressDialog.show();
                    
                    searchOnYoutube(v.getText().toString());
                    
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);

                    return false;
                }
                return true;
            }
        });

    }
    
    private void searchOnYoutube(final String keywords){
        
        new Thread(){

            //implementing run method
            public void run(){
                YoutubeConnector yc = new YoutubeConnector(MainActivity.this);
                searchResults = yc.search(keywords);
                handler.post(new Runnable(){

                    //implementing run method of Runnable
                    public void run(){
                        fillYoutubeVideos();
                        mProgressDialog.dismiss();
                    }
                });
            }
        //starting the thread
        }.start();
    }
    
    private void fillYoutubeVideos(){
        
        youtubeAdapter = new YoutubeAdapter(getApplicationContext(),searchResults);
        mRecyclerView.setAdapter(youtubeAdapter);
        youtubeAdapter.notifyDataSetChanged();
    }
}

After creating MainActivity.java class file, create another java class files and give some name for that java file. In this project additionally we need four java classes. The second java class name like PlayerActivity.java file, in this class used for search and get the youtube videos in your android application using YouTube Player API.

PlayerActivity.java

package com.test.vetbossel.searchyoutube;

import android.os.Bundle;
import android.widget.TextView;
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.YouTubePlayerView;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;

public class PlayerActivity extends YouTubeBaseActivity implements OnInitializedListener {

    private YouTubePlayerView playerView;
    
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_player);
        
        playerView = (YouTubePlayerView)findViewById(R.id.player_view);
        playerView.initialize(YoutubeConnector.KEY, this);
        TextView video_title = (TextView)findViewById(R.id.player_title);
        TextView video_desc = (TextView)findViewById(R.id.player_description);
        TextView video_id = (TextView)findViewById(R.id.player_id);
        video_title.setText(getIntent().getStringExtra("VIDEO_TITLE"));
        video_id.setText("Video ID : "+(getIntent().getStringExtra("VIDEO_ID")));
        video_desc.setText(getIntent().getStringExtra("VIDEO_DESC"));
    }
    
    @Override
    public void onInitializationFailure(Provider provider,
                                        YouTubeInitializationResult result) {
        Toast.makeText(this, getString(R.string.failed), Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                        boolean restored) {
        
        if(!restored){
            player.cueVideo(getIntent().getStringExtra("VIDEO_ID"));
        }
    }
}

Hereafter creating three more classes. Here spaces are not enough to explain all java classes, so finally i give the full source code of Android Search & play YouTube Videos using YouTube player API you can download.

Activity Layouts

Now time to integrate Java class files with XML layout files to make perfect android search and get youtube videos from youtube website (application) using youtube data API. Open under the path of res => layout => activity_main.xml file and add the following code below,

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText
        android:id="@+id/search_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1" />

   
    <android.support.v7.widget.RecyclerView
        android:id="@+id/videos_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="5dp"
        />

</LinearLayout>

Download Source Code

Click below to download the full source code of Android Search and Play YouTube Videos using YouTube Player Data API.

Android Search Play YouTube Videos Data API

Leave a Reply