【问题标题】:Cannot able to give start and end values using YouTubeAndroidPlayer api无法使用 YouTubeAndroidPlayer api 提供开始和结束值
【发布时间】:2013-12-13 13:45:37
【问题描述】:

我正在使用 YouTubeAndroidPlayer api 在我的应用程序中播放 youtube 视频。我想从中间开始播放视频,但无法找到(开始和结束)的方法,我们可以在 youtube api 中找到。

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

在 YouTubeAndroidPlayer api 中,我有一个类似下面的代码来播放视频。我无法从视频中间开始。我没有在播放器下找到任何方法来给出开始和结束值。

我搜索了很多解决方案,但没有找到任何运气。请建议。

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

        player.loadVideo(xyoajjlPt_o);

      }

【问题讨论】:

标签: android youtube-api


【解决方案1】:

使用带有timeMillis参数的方法:

player.loadVideo (String videoId, int timeMillis) 

在你的情况下:

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

[编辑]

嗯,你可以利用 Handler 来跟踪视频的当前运行时间。当以毫秒为单位的时间达到您想要停止视频的点时,只需调用player.pause() 方法即可。

这是活动的完整代码:

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

附:有人问了一个重复的问题here,我已经在那里更新了我的answer

【讨论】:

  • 如何赋予最终价值和控制
猜你喜欢
  • 2018-01-30
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多