Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide start and end parameters for a youtube video in youtube Android player?

I am using an Youtube Player api for playing youtube videos in my application. I want to start the video from suppose 36sec and wants to end this video at some 65sec.

example: https://www.youtube.com/v/BmOpD46eZoA?start=36&end=65

I am using youtubeAndroidPlayer Api. I didnt find any methods for Youtubeplayer to set the start and end values.could anyone suggest how can I set the parameters.

And I also want the controls to be hide using the "controls" field in youtube api,but I didnt find any methods to hide this.

my xml:

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:autoLink="web" />

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubeplayerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

My Activity:

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{

 public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
 public static final String VIDEO_ID = "xyoajjlPt_o";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);


    }

 @Override
 public void onInitializationFailure(Provider provider,
   YouTubeInitializationResult result) {
  Toast.makeText(getApplicationContext(), 
    "onInitializationFailure()", 
    Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,
   boolean wasRestored) {
  if (!wasRestored) {
        player.cueVideo(VIDEO_ID);

      }
 }
like image 785
user1891910 Avatar asked Dec 13 '13 11:12

user1891910


1 Answers

Well, you can make use of Handler to track the current running time of the video. When the time in milliseconds has reached the point where you want to stop the video, simply call the player.pause() method.

Here's the complete code of the activity:

public class MyActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{

    public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
    public static final String VIDEO_ID = "xyoajjlPt_o";
    private static YouTubePlayer player;

    TextView text;

    //this is the end time in milliseconds (65th second)
    public int endTime = 65000; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        text = (TextView) findViewById(R.id.text);
        YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);
    }

    @Override
    public void onInitializationFailure(Provider provider,
            YouTubeInitializationResult result) {
        Toast.makeText(getApplicationContext(), 
                "onInitializationFailure()", 
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {

        MyActivity.player = player; //necessary to access inside Runnable 

        //start the video at 36th second
        player.loadVideo(VIDEO_ID, 36000);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //For every 1 second, check the current time and endTime
                if(MyActivity.player.getCurrentTimeMillis() <= endTime) { 
                    text.setText("Video Playing at " + MyActivity.player.getCurrentTimeMillis());
                    handler.postDelayed(this, 1000);
                } else {
                    handler.removeCallbacks(this); //no longer required
                    text.setText(" Reached " + endTime);
                    MyActivity.player.pause(); //and Pause the video
                }
            }
        }, 1000);
    }
}

I'm not sure if there's any other way. Hope it solved your problem.

P.S. A duplicate question was asked here and I've updated my answer there.

like image 174
Joel Fernandes Avatar answered Oct 12 '22 09:10

Joel Fernandes