能不能用YouTubeAndroidPlayer API给起始值和最终值能用、能不、API、YouTubeAndroidPlayer

2023-09-04 03:18:15 作者:ゆぺ打野被野打

我使用YouTubeAndroidPlayer API用于播放YouTube视频在我的应用程序。我要开始从中间的视频,但不能能找到方法(开始和结束),因为我们可以在YouTube上找到API

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

在YouTubeAndroidPlayer API,我有一个code像下面播放视频。我能不能够从视频的中间开始。我没有找到下玩家的任何方法给起始值和最终值。

我搜索了很多的解决方案,但没有发现任何luck.Please建议。

  @覆盖
 公共无效onInitializationSuccess(供应商提供,YouTubePlayer球员,
   布尔wasRestored){
  如果(!wasRestored){
// player.cueVideo(VIDEO_ID);

        player.loadVideo(xyoajjlPt_o);

      }
 

解决方案

使用方法 timeMillis 参数:

  player.loadVideo(字符串VIDEOID,INT timeMillis)
 
为什么我这个sdk不能下载其他把那本api呢 只有4.3

在你的情况:

  player.loadVideo(xyoajjlPt_o,36000);
//这将开始36秒的视频
 

好了,你可以利用处理器的跟踪视频的当前运行时间。当以毫秒为单位的时间已经到了,你想停止视频点,只需调用 player.pause()方法。

下面是活动的完整code:

 公共类MyActivity扩展YouTubeBaseActivity实现YouTubePlayer.OnInitializedListener {

    公共静态最后弦乐API_KEY =YOUR_API_KEY_HERE;
    公共静态最后弦乐VIDEO_ID =yeF_b8EQcK0;
    私有静态YouTubePlayer播放器;

    TextView的文字;

    //这是以毫秒为单位的结束时间(65秒)
    公众诠释endTime的= 65000;


    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.my_activity);
        文=(TextView的)findViewById(R.id.text);
        YouTubePlayerView youTubePlayerView =(YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY,这一点);
    }

    @覆盖
    公共无效onInitializationFailure(供应商提供,
            YouTubeInitializationResult结果){
        Toast.makeText(getApplicationContext(),
                onInitializationFailure(),
                Toast.LENGTH_LONG).show();
    }

    @覆盖
    公共无效onInitializationSuccess(供应商提供,YouTubePlayer球员,布尔wasRestored){

        MyActivity.player =播放器; //必要的内部Runnable的访问

        //开始36秒的视频
        player.loadVideo(VIDEO_ID,36000);

        最终的处理程序处理程序=新的处理程序();
        handler.postDelayed(新的Runnable(){
            @覆盖
            公共无效的run(){
                //对于每1秒,检查当前时间和endTime
                如果(MyActivity.player.getCurrentTimeMillis()&其中; = endTime的){
                    text.setText(+ MyActivity.player.getCurrentTimeMillis视频在播放());
                    handler.postDelayed(这一点,1000);
                } 其他 {
                    handler.removeCallbacks(本); //不再需要
                    text.setText(达+ endTime的);
                    MyActivity.player.pause(); //并暂停视频
                }
            }
        },1000);
    }
}
 

P.S。重复的问题被问here我已经更新了我的回答那里。

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

      }

解决方案

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.