Я довольно новичок в Android Studio и пробую проект, в котором на основе события клика videoView (2 видео) изменяется с vid1 на vid2, vid2 на vid1 и так далее, и оба одновременно воспроизводятся и имеют одинаковый длина. Когда я использую встроенную функцию yieldToFront (), видеоролики не меняются местами, и я вижу только первое видео.
Button button;
button = (Button) findViewById(R.id.button);
vd1 = (VideoView) findViewById(R.id.videoView6);
vd1.setVideoURI(Uri.parse("android.resource://com.example.venapp/" + R.raw.first));
vd1.start();
vd1.setOnCompletionListener (new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
vd1.start();
}
});
vd2 = (VideoView) findViewById(R.id.videoView5);
vd2.setVideoURI(Uri.parse("android.resource://com.example.venapp/" + R.raw.second));
vd2.start();
vd2.setOnCompletionListener (new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
vd2.start();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter % 2 == 0) { // even # of click
counter++;
vd2.bringToFront();
}
else if (counter % 2 != 0) { // odd # of click
counter++;
vd1.bringToFront();
}
}
});
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="60dp"
android:layout_marginBottom="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<VideoView
android:id="@+id/videoView5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<VideoView
android:id="@+id/videoView6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="#00FFFFFF" />
</RelativeLayout>