Reputation: 2634
I am using String Arrays
to perform some task [assume to play videos] in a correct sequence and it works as per the expectations
, see the below code:
String[] videoUriList = {str1, str2, str3};
if(null != videoUriList && videoUriList.length > 1) {
// Plays the first video, then the second video.
mediaSource = new ConcatenatingMediaSource(
buildMediaSource(Uri.parse(videoUriList[0])),
buildMediaSource(Uri.parse(videoUriList[1])),
buildMediaSource(Uri.parse(videoUriList[2]))
);
}
RESULT:
Using above scenario its playing videos in a correct sequence only, first of all it starts with str1... then after completion of str1, it moves to str2 and at the end it plays str3
But, If I am using for loop to achieve same result, I am facing an issue
, it directly plays str3 (3rd item of the list [i.e. - 2nd index]) in short, skipping str1 and str2 to play.
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add(str1);
arrayList.add(str2);
arrayList.add(str3);
if( null != arrayList) {
Log.d("arrayList::", String.valueOf(arrayList.size()));
for(int i=0; i<arrayList.size(); i++)
{
String string = arrayList.get(i);
Log.d("string::", string+", "+String.valueOf(i));
mediaSource = new ConcatenatingMediaSource(buildMediaSource(Uri.parse(string)));
}
}
My LOG says:
D/arrayList::: 3
D/string::: http://www.storiesinflight.com/js_videosub/jellies.mp4, 0
D/string::: http://clips.vorwaerts-gmbh.de/VfE_html5.mp4, 1
D/string::: http://www.html5videoplayer.net/videos/toystory.mp4, 2
so where I am doing mistake, what went wrong in case of for loop
Upvotes: 0
Views: 135
Reputation: 50716
You can't call a constructor incrementally. You need to collect the arguments and then create the result. In this case, we can still use the loop to collect the values to an array, then pass that array to the constructor:
MediaSource[] mediaSources = new MediaSource[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
String string = arrayList.get(i);
Log.d("string::", string+", "+String.valueOf(i));
mediaSources[i] = buildMediaSource(Uri.parse(string));
}
mediaSource = new ConcatenatingMediaSource(mediaSources);
Upvotes: 2
Reputation: 201439
Your for
loop sets your singular mediaSource
on each iteration. However, there is only one - so after the loop executes, you have the last mediaSource
. If you want to keep all of the mediaSource
(s), you could use a corresponding List
. Something like,
List<String> arrayList = Arrays.asList(str1, str2, str2);
List<ConcatenatingMediaSource> mediaSources = new ArrayList<>();
if (null != arrayList) {
Log.d("arrayList::", String.valueOf(arrayList.size()));
for (int i = 0; i < arrayList.size(); i++) {
String string = arrayList.get(i);
Log.d("string::", string + ", " + String.valueOf(i));
mediaSources.add(new ConcatenatingMediaSource(
buildMediaSource(Uri.parse(string))));
}
}
Upvotes: 1