I integrated the VLC SDK Android library into my Android project for streaming from an IP camera. I initiated the camera connection using the IP host, username, and password, and the connection was successful, with the video displaying correctly. However, I encountered an issue where the camera streaming view wouldn't display after the device went into standby/sleep mode or when reopening the app after closing it. Strangely, the video streaming worked fine after killing the app. What is wrong in my code.
Error Log in console logcat.
eglCreateWindowSurface: native_window_api_connect (win=0xb4000075e8334010) failed (0xffffffed) (already connected to another API?) eglCreateWindowSurfaceTmpl:689 error 3003 (EGL_BAD_ALLOC) [b400007673514f60/623d] libvlc gl: cannot create EGL window [b40000765a07b400/623d] libvlc video output: video output display creation failed
dependencies version
implementation 'org.videolan.android:libvlc-all:4.0.0-eap13'
My Activity Code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private String rtspUrl;
private LibVLC libVlc;
private MediaPlayer mediaPlayer;
private Media media;
private VLCVideoLayout videoLayout;
private SurfaceView surfaceView;
private ProgressBar progressBar;
private float buffering;
private TextView taksnapTv, recordTv;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
videoLayout = findViewById(R.id.videoLayout);
try {
String encodedUsername = URLEncoder.encode("admin", "UTF-8");
String encodedPassword = URLEncoder.encode("admin", "UTF-8");
rtspUrl = "rtsp://" + encodedUsername + ":" + encodedPassword + "@192.168.0.115:554/stream1";
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
ArrayList<String> args = new ArrayList<>();
args.add("--aout=opensles");
args.add("--audio-time-stretch");
args.add("--rtsp-tcp");
args.add("--live-caching=0");
args.add("--file-caching=0");
args.add("--network-caching=200");
args.add("-vvv");
libVlc = new LibVLC(this, args);
mediaPlayer = new MediaPlayer(libVlc);
initListener();
}
@Override
protected void onStart()
{
super.onStart();
videoLayout.clearFocus();
videoLayout.clearAnimation();
mediaPlayer.attachViews(videoLayout, null, false, false);
media = new Media(libVlc, Uri.parse(rtspUrl));
media.setHWDecoderEnabled(true, true);
mediaPlayer.setMedia(media);
media.release();
mediaPlayer.play();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop()
{
super.onStop();
mediaPlayer.stop();
mediaPlayer.detachViews();
}
@Override
protected void onDestroy()
{
super.onDestroy();
mediaPlayer.release();
libVlc.release();
// media.clearSlaves();
}
private void initListener() {
mediaPlayer.setEventListener(new MediaPlayer.EventListener() {
@Override
public void onEvent(MediaPlayer.Event event) {
if (event.type == MediaPlayer.Event.Opening) {
Log.d(TAG, "VLC Opening");
progressBar.setVisibility(View.VISIBLE);
} else if (event.type == MediaPlayer.Event.Buffering) {
Log.d(TAG, "VLC Buffering:" + event.getBuffering());
if (event.getBuffering() >= 100) {
buffering = event.getBuffering();
progressBar.setVisibility(View.GONE);
} else
progressBar.setVisibility(View.VISIBLE);
} else if (event.type == MediaPlayer.Event.Playing) {
Log.d(TAG, "VLC Playing");
} else if (event.type == MediaPlayer.Event.Stopped) {
Log.d(TAG, "VLC Stopped");
progressBar.setVisibility(View.GONE);
} else if (event.type == MediaPlayer.Event.EncounteredError) {
Log.d(TAG, "VLC EncounteredError");
progressBar.setVisibility(View.GONE);
} else if (event.type == MediaPlayer.Event.Vout) {
Log.d(TAG, "VLC Vout" + event.getVoutCount());
} else if (event.type == MediaPlayer.Event.RecordChanged) {
Log.d(TAG, "VLC RecordChanged");
}
}
});
}
}