mrmaffen / vlc-android-sdk

VLC Android SDK pushed to Maven Central. Primarily used in project tomahawk-android.
792 stars 244 forks source link

Stretch RTSP stream in VideoView #141

Closed jacobusp closed 4 years ago

jacobusp commented 4 years ago

How to stretch an RTSP stream in a VideoView? To be more specific, I've configured an IP camera (Foscam FI9800P) to stream at 640x480, but I want it to stretch over the entire VideoViewin my app.

Currently my activity code is like this:

VideoView videoView = findViewById(R.id.videoView);

ArrayList<String> options = new ArrayList<>();
LibVLC libVlc = new LibVLC(this, options);
MediaPlayer mediaPlayer = new MediaPlayer(libVlc);
final IVLCVout vOut = mediaPlayer.getVLCVout();
vOut.addCallback(this);
vOut.setVideoView(videoView); //videoView is a pre-defined view which is part of the layout
vOut.attachViews();
mediaPlayer.setEventListener(this);

Media videoMedia = new Media(libVlc, Uri.parse("myRtspUrl"));
mediaPlayer.setMedia(videoMedia);
mediaPlayer.play();

The layout is a constraint layout containing the VideoView:

<VideoView
 android:id="@+id/videoView"
 android:layout_width="0dp"
 android:layout_height="0dp"
 app:layout_constraintBottom_toTopOf="@+id/startButton"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="@+id/guidelineVertical"
 app:layout_constraintTop_toTopOf="parent" />

The layout_widthand layout_height of 0dp stretches the VideoViewover the screen (black area), but the streamed content within the VideoViewdoes not get stretched: Stretch RTSP stream in VideoView

I am not sure if this question should be posted here or somewhere else. Please point me in the right direction if you can.

jacobusp commented 4 years ago

Ater some digging I've solved it with some help from this issue: https://github.com/mrmaffen/vlc-android-sdk/issues/104 The solution is to set aspect ratio and window size:

mediaPlayer.setAspectRatio(videoView.getWidth() + ":" + videoView.getHeight());
vOut.setWindowSize(videoView.getWidth(), videoView.getHeight());

The new activity code:

VideoView videoView = findViewById(R.id.videoView);

ArrayList<String> options = new ArrayList<>();
LibVLC libVlc = new LibVLC(this, options);
MediaPlayer mediaPlayer = new MediaPlayer(libVlc);
final IVLCVout vOut = mediaPlayer.getVLCVout();
vOut.setVideoView(videoView); //videoView is a pre-defined view which is part of the layout
mediaPlayer.setAspectRatio(videoView.getWidth() + ":" + videoView.getHeight());
vOut.setWindowSize(videoView.getWidth(), videoView.getHeight());
vOut.addCallback(this);
vOut.attachViews();
mediaPlayer.setEventListener(this);

Media videoMedia = new Media(libVlc, Uri.parse("myRtspUrl"));
mediaPlayer.setMedia(videoMedia);
mediaPlayer.play();

Note!

You can't get the width and height of the VideoView in an Activity.onCreate() method (they will be 0 then). I made it work by running this code in an Handler.postdelayed(), so the width and height are known by then.