user3345767
user3345767

Reputation: 349

Android youtube Api skip video

i working android youtube Api.i successfully create youtube Api project.i can play video with youtube Api.now i want to skip video in button click.i wrote some code witch can to skip video ,but i when i click in button i have bed result.YouTubePlayerView loading twice. this is a my code

public class MainActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "***********";
private String VIDEO = "*******";
private Button b1;
private YouTubePlayer player1;
public int endTime = 65000;
private PlayerStyle style;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    b1 = (Button) findViewById(R.id.button1);
    youTubeView.initialize(DEVELOPER_KEY, this);
    youTubeView.setEnabled(false);
    style = PlayerStyle.CHROMELESS;

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            int[] array = new int[] { 36000, 40000, 50000, 60000, 70000,
                    80000 };

            Random random = new Random();
            final int index = array[random.nextInt(array.length)];

            Log.e("randommmmmmmmmmmm", String.valueOf(index));

            player1.loadVideo(VIDEO, index);

        }
    });
}

@Override
public void onInitializationFailure(Provider provider,
        YouTubeInitializationResult error) {
    Toast.makeText(this, "Oh no! " + error.toString(), Toast.LENGTH_LONG)
            .show();
}

@Override
public void onInitializationSuccess(Provider provider,
        YouTubePlayer player, boolean wasRestored) {
    this.player1 = player;
    player1.setPlayerStyle(style);

    player.loadVideo(VIDEO);
}

}

what am i doing wrong? if anyone knows solution please help me

Upvotes: 0

Views: 933

Answers (1)

KungFu_Panda
KungFu_Panda

Reputation: 101

sorry i didnt understand the question completely but i think you are trying to forward the video by some time. I dont know if this will work but you should look at method onSeekTo():

private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {

        @Override
        public void onBuffering(boolean arg0) {
        }

        @Override
        public void onPaused() {
        }

        @Override
        public void onPlaying() {
        }

        @Override
        public void onSeekTo(int arg0) {
        }

        @Override
        public void onStopped() {
        }

    };

and you can add this listener to your youtubePlayer instance. or if you are trying to seek to a particular time by clicking the button then you can do something like this:

YouTubePlayer.seekToMillis (int milliSeconds);

Upvotes: 0

Related Questions