appukrb appukrb - 17 days ago 9
Android Question

How to stop youtube video or set buffer limit in youtube-api in android

I am using YouTube API to play my video, there are

play()
and
pause()
functions in the YouTube player. I need to stop the video at particular time. I am using
pause()
but the background of the video gets buffering. How do I stop buffering the video after a particular time limit? In webview there is a option start and end tag in URL to buffer like below,


https://www.youtube.com/v/K_AdxJWFUh4&start=0&end=30


Note: I am using Youtube Player to play video not WEBVIEW

Answer

Check This Code

public class youtube extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{

public static final String API_KEY = "<---YOUR KEY--->";
public static final String VIDEO_ID = "<--VIDEO ID YOU WANT TO PLAY--->";
private static YouTubePlayer player;

TextView text;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
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, 0);

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);
}
}

Layout 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:id="@+id/text"
        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>
Comments