Cannot able to give start and end values using You

2019-06-14 15:02发布

问题:

I am using YouTubeAndroidPlayer api for playing youtube videos in my application. I want to start the video from the middle,but could not able to find the methods for (start and end ) as we can find in youtube api.

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

In YouTubeAndroidPlayer api, I had a code like below for playing a video. I cannot able to start from the middle of the video. I didnt find any methods under player to give start and end values.

I searched a lot for the solution,but didnt find any luck.Please suggest.

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

        player.loadVideo(xyoajjlPt_o);

      }

回答1:

Use the method with timeMillis parameter:

player.loadVideo (String videoId, int timeMillis) 

In your case:

player.loadVideo("xyoajjlPt_o", 36000); 
//this will start the video from 36th second

[EDIT]

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 = "YOUR_API_KEY_HERE";
    public static final String VIDEO_ID = "yeF_b8EQcK0";
    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);
    }
}

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