Try this:
layoutfile.xml
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/teacherName"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#fff"
android:padding="5dp" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/date"
android:layout_centerHorizontal="true"
android:layout_marginTop="115dp"
android:visibility="gone"
/>
<TextView
android:id="@+id/progressBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:maxLines="3"
android:visibility="gone"
android:text="Video Loading..." />
TutorialVideoView.java
public class TutorialVideoView extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{
private VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
ProgressBar progressBar1;
TextView progressBarText;
String v_title,v_date,v_id,v_url,v_teacher;
public static final String API_KEY = "Your API key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorialvideo_view);
progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
progressBarText=(TextView)findViewById(R.id.progressBarText);
v_id="Your YouTube Video ID";
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.setVisibility(View.VISIBLE);
youTubePlayerView.initialize(API_KEY, this);
}
@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(v_id);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
}
@Override
public void onVideoStarted() {
}
};
}
Hope this may help you!