从另一个 Activity 返回后,VideoView 上的视频播放消失
我有两个活动:一个活动将 VideoView
附加到 MediaPlayer
,第二个活动。
我开始在第一个 Activity 中观看视频,然后在播放期间启动第二个 Activity。
返回第一个活动后,我可以听到声音,但看不到图像。
我的视频布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/videoView" android:layout_gravity="center" />
<ListView android:layout_width="fill_parent" android:layout_height="125dp"
android:id="@+id/ListView" />
</LinearLayout>
您对视频不显示的原因有什么想法吗?
I have two Activities: one with VideoView
attached to MediaPlayer
and the second one.
I start watching a video in the first Activity, then during playback I start second Activity.
After going back to first Activity I can hear sound but see no picture.
My Video Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/videoView" android:layout_gravity="center" />
<ListView android:layout_width="fill_parent" android:layout_height="125dp"
android:id="@+id/ListView" />
</LinearLayout>
Do you have any ideas why video doesn't appear?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能与Activity的生命周期有关。您是否在 onCreate() 方法中开始播放视频?如果是这样,请尝试将其移至 onResume(),当活动发送到后台但又返回时,该方法会被调用。 onCreate() 仅在首次创建时被调用一次。
Might be related to the Activity life cycle. Do you start playing your video in the onCreate() method? If so, try to move it to onResume(), which gets called when an activity is sent to the background but then brought back again. onCreate() only gets called once, when it's first created.
最后,我得到了
MediaPlayer
,它在onStop
方法中被销毁,并在onResume
方法中重新创建。这种方法的主要缺点是,改变方向后,MediaPlayer 必须再次缓冲视频。
我认为主要原因是对
MediaPlayer
的引用丢失了。如果不参考MediaPlayer
,我无法再次将SurfaceView
设置为视频显示。Finally I've ended up with
MediaPlayer
which is destroyed inonStop
method and recreated inonResume
method.Main drawback of this approach is that after changing orientation
MediaPlayer
has to buffer video again.I think that the main reason was that reference to
MediaPlayer
was lost. Without reference toMediaPlayer
I couldn't setSurfaceView
again, as a Video display.