How to play YouTube video in my Android application?
This is the btn click event
btnvideo.setOnClickListener(new OnClickListener() {public void onClick(View v) { startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=Hxy8BZGQ5Jo"))); Log.i("Video", "Video Playing...."); }});
this type it opened in another page with the youtube where u can show your video
Steps
Create a new Activity, for your player(fullscreen) screen with menu options. Run the mediaplayer and UI in different threads.
For playing media - In general to play audio/video there is mediaplayer api in android. FILE_PATH is the path of file - may be url(youtube) stream or local file path
MediaPlayer mp = new MediaPlayer(); mp.setDataSource(FILE_PATH); mp.prepare(); mp.start();
Also check: Android YouTube app Play Video Intenthave already discussed this in detail.
Use YouTube Android Player API.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.andreaskonstantakos.vfy.MainActivity"><com.google.android.youtube.player.YouTubePlayerView android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" android:layout_centerHorizontal="true" android:id="@+id/youtube_player" android:layout_alignParentTop="true" /><Buttonandroid:text="Button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="195dp"android:visibility="visible"android:id="@+id/button" /></RelativeLayout>
MainActivity.java:
package com.example.andreaskonstantakos.vfy;import android.os.Bundle;import android.view.View;import android.widget.Button;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;public class MainActivity extends YouTubeBaseActivity {YouTubePlayerView youTubePlayerView;Button button;YouTubePlayer.OnInitializedListener onInitializedListener;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player); button = (Button) findViewById(R.id.button); onInitializedListener = new YouTubePlayer.OnInitializedListener(){ @Override public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) { youTubePlayer.loadVideo("Hce74cEAAaE"); youTubePlayer.play(); } @Override public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) { } }; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {youTubePlayerView.initialize(PlayerConfig.API_KEY,onInitializedListener); } });}}
and the PlayerConfig.java class:
package com.example.andreaskonstantakos.vfy;/** * Created by Andreas Konstantakos on 13/4/2017. */public class PlayerConfig {PlayerConfig(){}public static final String API_KEY = "xxxxx";}
Replace the "Hce74cEAAaE" with your video ID from https://www.youtube.com/watch?v=Hce74cEAAaE.Get your API_KEY from Console.developers.google.com and also replace it on the PlayerConfig.API_KEY.For any further information you can follow the following tutorial step by step:https://www.youtube.com/watch?v=3LiubyYpEUk