I'm using ViewPager2 for playing in MediaPlayer (libVLC) videostream from cameras. When I swiped forward everything works good. But wheh I swiped back got no video in Fragment only sound and TextView.
我正在使用ViewPager2从摄像头播放MediaPlayer(LibVLC)视频流。当我向前滑动时,一切都很正常。但是,当我滑动回来时,没有视频片段,只有声音和文本视图。
Here is code of Fragment
以下是片段的代码
`public class PageFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = new MediaPlayer[3];
pageNumber = getArguments() != null ? getArguments().getInt("num") : 1;
libVlc = new LibVLC(getActivity());
mediaPlayer[pageNumber] = new MediaPlayer(libVlc);
urls = getResources().getStringArray(R.array.urls);
}
@Override
public void onResume() {
super.onResume();
if (!mediaPlayer[pageNumber].isPlaying()) {
mediaPlayer[pageNumber].play();
int st=mediaPlayer[pageNumber].getPlayerState();
Log.i("Play"+pageNumber, String.valueOf(st));
}
}
@Override
public void onPause() {
super.onPause();
if (mediaPlayer[pageNumber]!=null && mediaPlayer[pageNumber].isPlaying()) {
mediaPlayer[pageNumber].pause();
int st=mediaPlayer[pageNumber].getPlayerState();
Log.i("Pause"+pageNumber, String.valueOf(st));
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.fragment_page, container, false);
TextView pageHeader = result.findViewById(R.id.displayText);
String header = "Camera " + (pageNumber+1);
pageHeader.setText(header);
videoLayout = result.findViewById(R.id.videoLayout);
mediaPlayer[pageNumber].attachViews(videoLayout, null, false, false);
Media media = new Media(libVlc, Uri.parse(urls[pageNumber]));
media.setHWDecoderEnabled(true, false);
media.addOption(":network-caching=600");
mediaPlayer[pageNumber].setMedia(media);
media.release();
mediaPlayer[pageNumber].play();
return result;
}
.....
}
And here layout for Fragment
这里是碎片的布局
<TextView
android:id="@+id/displayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="500dp"
android:textSize="22sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<org.videolan.libvlc.util.VLCVideoLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/displayText"
android:layout_marginTop="50dp"/>
Same happens when MainActivity goes in Pause and than come back to the foreground.
当MainActivity暂停,然后返回到前台时也会发生同样的情况。
Know some body how to resolve this trouble ?
Thank You.
有人知道如何解决这个问题吗?谢谢。
P.S.
After backswaping I got this in log:
附注:在回调后,我在日志中记录了以下内容:
E/VLC: [945e9a00/65e2] libvlc decoder: buffer deadlock prevented
E/VLC: [94b4db00/65f9] libvlc vout display: can't get Video Surface
E/VLC: [94812a80/65f9] libvlc video output: video output display creation failed
E/VLC: [936b4000/65fa] libvlc vout display: can't get Video Surface
E/VLC: [94812a80/65fa] libvlc video output: video output display creation failed
E/VLC: [945e9a00/65f2] libvlc decoder: avcodec_send_packet critical error
E/VLC: [94b4e900/65fb] libvlc vout display: can't get Video Surface
E/VLC: [94812a80/65fb] libvlc video output: video output display creation failed
更多回答
优秀答案推荐
LibVLC gets discarded in these cases, you need to renew the libvlc objects.
在这些情况下,libvlc被丢弃,您需要续订libvlc对象。
更多回答
Hi! What You mean, recreate libvlc objects or redraw ?
嗨!您是什么意思,重新创建libvlc对象还是重新绘制?
yes thats what I mean
是的,这就是我的意思
我是一名优秀的程序员,十分优秀!