I'm trying to populate an array with files in my raw folder. I then pick a random slot in that array and use it to play that file.
But when I do this, every slot seems to contain whatever was the final video in the array. I'm not sure what the cause of this is...
var videos = Array(5){R.raw.c0; R.raw.c1; R.raw.c2; R.raw.c3; R.raw.c4};
videoView.setVideoPath("android.resource://" + getPackageName() + "/" + videos[Random.nextInt(0, 4)])
videoView.requestFocus()
videoView.start()
You're initializing the array incorrectly.
The {...}
block after the Array(5)
is actually a lambda that takes an integer and returns the content for that index in the array. The semicolons (rather than commas) mean that each of R.raw.c0
, R.raw.c1
, etc. is just a statement that doesn't do anything. Since R.raw.c4
is the last statement in that block, it sets all five indexes to that value.
You probably meant:
val videos = intArrayOf(R.raw.c0, R.raw.c1, R.raw.c2, R.raw.c3, R.raw.c4)
Note the replacement of semicolons with commas. I also switched the var
to a val
, as you don't appear to be changing it.