Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

Youtube Data API Seek to Millis | Jump to specific time of youtube video

Youtube Data API Seek to Millis | Jump to a specific time of youtube video





For Youtube Data Api V3 Creation and Initialization click here

For Youtube Data Api V3 Listeners Tutorial click here

For Youtube Data Api V3 detailed methods click here


In this post, we will add the functionality of seek to millis, jump to specific time of video in youtube player with Youtube Data API.

See the previous posts to join the code, 

 In this, we will add a button and edit text, also we get the whole video length and compare it with the edit text value to not jump to an invalid value.

XML
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical"  
   android:padding="10dp"  
   tools:context=".MainActivity">  
   
   <com.google.android.youtube.player.YouTubePlayerView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:id="@+id/youtube_player"/>  
   
   <EditText  
     android:id="@+id/video_seconds"  
     android:layout_width="87dp"  
     android:layout_height="wrap_content" />  
   
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Goto"  
     android:id="@+id/goto_btn"/>  
   
 </LinearLayout>  

Java Code
 package studio.harpreet.sampleyoutubedataapi;  
   
 import androidx.appcompat.app.AppCompatActivity;  
   
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 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 java.util.ArrayList;  
   
 import static android.content.ContentValues.TAG;  
   
 public class MainActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {  
   
   YouTubePlayerView youTubePlayerView;  
   
   YouTubePlayer youTubePlayer;  
   
   EditText videoSec_et;  
   Button goto_btn;  
   
   MyPlayBackEventListener playBackEventListener;  
   MyPlayerStateChangeListener playerStateChangeListener;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     videoSec_et = findViewById(R.id.video_seconds);  
     goto_btn = findViewById(R.id.goto_btn);  
   
     youTubePlayerView = findViewById(R.id.youtube_player);  
     youTubePlayerView.initialize(getString(R.string.youtube_api_key),this);  
   
     playerStateChangeListener = new MyPlayerStateChangeListener();  
     playBackEventListener = new MyPlayBackEventListener();  
   
     goto_btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
   
         int videolength = youTubePlayer.getDurationMillis()/1000;  
   
         int seconds = Integer.parseInt(videoSec_et.getText().toString().trim());  
   
         if(seconds < videolength)  
         {  
           youTubePlayer.seekToMillis(seconds*1000);  
         }  
         else  
         {  
           showMessage("Value greater than Video Length");  
         }  
   
       }  
   
   
     });  
   
      
   }  
   
   @Override  
   public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {  
   
     this.youTubePlayer = youTubePlayer;  
   
     youTubePlayer.setPlaybackEventListener(playBackEventListener);  
     youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);  
   
     if(!b)  
     {  
       youTubePlayer.cueVideo("-_pacR6syDo",200000); //https://www.youtube.com/watch?v=-_pacR6syDo // plays at 200 sec 200*1000  
   
       //youTubePlayer.cueVideos(videos,3,0); // method used as arraylist,index of array list,milliseconds of video start // you can use less or full method  
      // youTubePlayer.loadVideos(videos,3,0); // method used as arraylist,index of array list,milliseconds of video start // you can use less or full method  
   
       // cuevideos is used to only load videos but not play  
       // load videos is used to load and play  
     }  
   
   }  
   
   @Override  
   public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {  
     if (youTubeInitializationResult.isUserRecoverableError()) {  
       youTubeInitializationResult.getErrorDialog(this, 1).show();  
     } else {  
       String error = String.format("Error initializing YouTube Player ", youTubeInitializationResult.toString());  
       Toast.makeText(this, error, Toast.LENGTH_LONG).show();  
     }  
   }  
   
   @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     if(requestCode == 1)  
     {  
       getYoutubePlayerProvider().initialize(getString(R.string.youtube_api_key),this);  
     }  
   }  
   
   private YouTubePlayer.Provider getYoutubePlayerProvider() {  
   
     return youTubePlayerView;  
   
   }  
   
   private final class MyPlayBackEventListener implements YouTubePlayer.PlaybackEventListener  
   {  
   
     @Override  
     public void onPlaying() {  
       showMessage("onPlaying");  
     }  
   
     @Override  
     public void onPaused() {  
       showMessage("OnPaused");  
     }  
   
     @Override  
     public void onStopped() {  
       showMessage("OnStopped");  
     }  
   
     @Override  
     public void onBuffering(boolean b) {  
       showMessage("onBuffering");  
     }  
   
     @Override  
     public void onSeekTo(int i) {  
       showMessage("onSeekTo");  
     }  
   }  
   
   private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener  
   {  
   
     @Override  
     public void onLoading() {  
       showMessage("onLoading");  
     }  
   
     @Override  
     public void onLoaded(String s) {  
       showMessage("onLoaded");  
     }  
   
     @Override  
     public void onAdStarted() {  
       showMessage("onAdStarted");  
     }  
   
     @Override  
     public void onVideoStarted() {  
       showMessage("onVideoStarted");  
     }  
   
     @Override  
     public void onVideoEnded() {  
       showMessage("onVideoEnded");  
   
     }  
   
     @Override  
     public void onError(YouTubePlayer.ErrorReason errorReason) {  
   
       showMessage("onError: "+errorReason);  
     }  
   }  
   
   private final class MyPlaylistEventListener implements YouTubePlayer.PlaylistEventListener // used only when we play a playlist  
   {  
   
     @Override  
     public void onPrevious() {  
   
     }  
   
     @Override  
     public void onNext() {  
   
     }  
   
     @Override  
     public void onPlaylistEnded() {  
   
     }  
   }  
   
   private void showMessage(String message)  
   {  
     Toast.makeText(this, message, Toast.LENGTH_SHORT).show();  
   }  
   
 }  


 
Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

1 comment:

  1. Rating YouTube recordings gives other YouTube clients that the recordings are popular, which might prompt a greater amount of their #1 recordings are on the YouTube site. This is one of many motivations behind why you ought open a YouTube account, yet in addition rate YouTube that you can view or leave remarks.
    https://www.buyyoutubeviewsindia.in/youtube-marketing/

    ReplyDelete