我正在使用VideoView播放视频。在视频的中间暂停该视频,并在该VideoView上方加载该Frame Layout内的Frame Layout并具有另一个VideoView。在该顶部VideoView流中,另一个视频却无法正常工作,第二个VideoView是透明的,请参见底部VideoView。
这是我用于调试的xml,为Framelayout提供了绿色背景。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="320dp">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="300dp"
android:contentDescription="@string/app_name"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/ic_launcher_background"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:id="@+id/imageView"
android:visibility="gone"
android:background="#f2f2">
<VideoView
android:id="@+id/videoOver"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="250dp"
android:contentDescription="@string/app_name"
/>
</FrameLayout>
</RelativeLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout >
这是我的Java代码
public class MainActivity extends AppCompatActivity {
boolean running = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView mVideoView = findViewById(R.id.videoView);
final VideoView mVideoOver = findViewById(R.id.videoOver);
final TextView text = findViewById(R.id.textView);
final FrameLayout overLay = findViewById(R.id.imageView);
mVideoView.setVideoURI(Uri.parse("https://ia600201.us.archive.org/22/items/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"));
mVideoView.requestFocus();
mVideoView.start();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
running = true;
final int duration = mVideoView.getDuration();
new Thread(new Runnable() {
public void run() {
do{
text.post(new Runnable() {
public void run() {
// int time = (duration - mVideoView.getCurrentPosition())/1000;
int time = (mVideoView.getCurrentPosition())/1000;
if(time == 15){
mVideoView.pause();
overLay.setVisibility(View.VISIBLE);
mVideoOver.setVideoURI(Uri.parse("http://techslides.com/demos/sample-videos/small.mp4"));
mVideoView.clearFocus();
mVideoOver.requestFocus();
mVideoOver.start();
}
text.setText(time+"");
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!running) break;
}
while(mVideoView.getCurrentPosition()<duration);
}
}).start();
}
});
}
}