这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<VideoView
android:id="@+id/geoloc_anim"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:visibility="visible" />
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</FrameLayout>
</LinearLayout>
这是我的活动代码:
public class MainActivity extends ActionBarActivity implements OnPreparedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
Uri uri = Uri.parse("android.resource://" + getPackageName()+"/raw/lst2");
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.setZOrderOnTop(true);
mVideoView.start();
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
View placeholder = (View) findViewById(R.id.placeholder);
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
// video started; hide the placeholder.
placeholder.setVisibility(View.GONE);
return true;
}
return false;
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
它在安卓4.2上运行良好,但在安卓2.3上却无法正常工作。在android 2.3上,第一次打开它时,它可以找到,但当关闭应用程序并再次打开它时,会出现一个黑屏,如下所示:
大约一分钟后,它从黑屏变成白屏,但仍然没有播放任何东西。
你能帮我解决这个问题吗?
发布于 2016-07-11 17:13:06
回答得很晚。但它肯定对任何人都有帮助。
在start()之前设置videoView.setZOrderOnTop(true)。
https://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)
videoView.setVideoURI(Uri.parse(uriString));
videoView.setZOrderOnTop(true);//this line solve the problem
videoView.start();
发布于 2017-09-06 03:45:57
我通过切换VideoView
的alpha
解决了这个问题
public class VideoPlayer extends VideoView {
....
public VideoPlayer(Context context) {
super(context);
init();
}
public void init() {
setAlpha(0); // hides view
setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
setAlpha(1); // shows view
return true;
}
return false;
}
});
}
});
我猜如果你有外部VideoView
并且可以访问它,你也可以做同样的事情。
发布于 2020-03-28 06:40:02
这段代码适用于我,在android nougat上测试过的代码。在java文件中不需要额外的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
tools:context=".SecondaryDisplay"
android:background="@color/black">
<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
https://stackoverflow.com/questions/28577704
复制相似问题