question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Load video playing previous video

See original GitHub issue

Using version: 8.0.1

if youtubeArray.isEmpty {
  youtubeArray.populate with videos. Array with 5 string videoIds
} else {
  var randomVid: String = youtubeArray[Random().nextInt(youtubeArray.size)]
  loadVideo(playerview, randomVid)
  val randomVidIndex = youtubeArray.indexOf(randomVid)
  youtubeArray.removeAt(randomVidIndex)
}

YouTube.loadvideo

fun loadVideo(playerView: YoutubePlayerView, videoId: String) {
  playerView.initialize({ initializedPlayerView ->
   initializedPlayerView.addListener(object: AbstractYouTubePlayerListener() {
    override fun onReady() {
      super.onReady()
      initializedPlayerView.loadVideo(videoId, 0f)
      System.out.println("Playing Video: $videoId")
    }
  }
  })
}, true)

As you can see in the code above i am trying to call random videos from an array. This works, but only once. It fetched a video and played it. When I click the button again, instead of playing a different video, it plays the same one again. With System.out.println("Playing Video: $videoId") I can see the player attempting to play another video, but changes and plays the previously played video. Any ideas on the culprit of this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Cortlanddcommented, Jun 5, 2019

@kunwarshekhar You can:

  1. Create a list of videoId’s that get played
  2. Get random videoId from said list and play it
  3. Remove it from the list
  4. Click your play button again to play another video in the list and do the same process
            // Get random string videoId from youtube array
            val randomVid: String = youtubeArray[Random().nextInt(youtubeArray.size)]

            // Play video with random array
            initializedYouTubePlayer!!.loadVideo(randomVid, 0f)
            System.out.println("Playing Video: " + randomVid)

            // Get random element position
            val randomVidIndex = youtubeArray.indexOf(randomVid)

            // Remove random element from list
            youtubeArray.removeAt(randomVidIndex)

            // Print remaining videos
            System.out.println(youtubeArray.toString())
0reactions
ghostcommented, Jun 5, 2019

I tried that but the issue is little different, can you check this: https://github.com/PierfrancescoSoffritti/android-youtube-player/issues/378

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML5 video previous - next and auto play - Stack Overflow
First step is to add an event handler to each button. For example, something like this should work for your next button. var...
Read more >
Lazy-loading video - web.dev
This post explains lazy-loading and the options available to you when lazy-loading video.
Read more >
How to preload an entire html5 video before play, SOLVED
Solution #4, ajax and bloburl (works) ... You can force the browser to download a video by making a GET request to the...
Read more >
HTMLMediaElement.load() - Web APIs - MDN Web Docs
The HTMLMediaElement method load() resets the media element to its initial state and begins the process of selecting a media source and loading...
Read more >
YouTube Player API Reference for iframe Embeds
The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript. Using the API's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found