libvlc android视频大小不适合surfaceview

时间:2017-12-21 12:35:45

标签: android android-layout video react-native libvlc

在这里,我成功构建了libvlc android并将其嵌入到本机项目中。

音频和视频流都可以正常工作。

令我感到困惑的是视频内容大小不合适surface& view好吧,虽然我已经设置了MATCH_PARENT

以下是一些代码实现。

package org.videolan;

import android.net.Uri;
import android.app.Activity;
import android.content.Context;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.facebook.react.bridge.ReactContext;

import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;

import java.util.ArrayList;

public class RCTVLCPlayerView extends FrameLayout {
  private final Context _context;
  private SurfaceView surfaceView;
  private LibVLC libVLC = null;
  private MediaPlayer mediaPlayer;
  private Activity activity = null;

  ArrayList<String> options = new ArrayList<>();

  public SurfaceView getPlayer() {
    return this.surfaceView;
  }

  public RCTVLCPlayerView(Context context) {
    super(context);
    this._context = context;
    this.activity = ((ReactContext) getContext()).getCurrentActivity();

    surfaceView = new SurfaceView((ReactContext) getContext());
    libVLC = new LibVLC((ReactContext) getContext(), options);

    try {
      if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
      }
      mediaPlayer = new MediaPlayer(libVLC);
      mediaPlayer.getVLCVout()
        .setVideoSurface(surfaceView.getHolder().getSurface(), 
          surfaceView.getHolder());
      mediaPlayer.getVLCVout().attachViews();

      ViewGroup.LayoutParams params =
          new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
              ViewGroup.LayoutParams.MATCH_PARENT);
      FrameLayout mFrameLayout = new FrameLayout((ReactContext) getContext());
      mFrameLayout.setLayoutParams(params);
      mFrameLayout.addView(surfaceView);
      addView(mFrameLayout);
      Media media = new Media(libVLC, 
        Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov"));
      mediaPlayer.setMedia(media);
      mediaPlayer.play();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  ... ...
}

这是应用程序运行时的快照。

任何帮助将不胜感激!

libvlc-android-video-size-does-not-fit-the-surfaceview

1 个答案:

答案 0 :(得分:3)

您可以尝试使用显示指标来获取表面视图大小并设置IVLCVout窗口大小

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

ViewGroup.LayoutParams videoParams = vidSurface.getLayoutParams();
videoParams.width = displayMetrics.widthPixels;
videoParams.height = displayMetrics.heightPixels;

最后一步是设置IVLCVount窗口大小:

IVLCVout vout = mediaPlayer .getVLCVout();
vout.setVideoView(surfaceView);
vout.setWindowSize(videoParams.width,videoParams.height);
vout.addCallback(this) //or you can try below code 
//  vout.addCallback(new IVLCVout.Callback() {
//    @Override
//    public void onSurfacesCreated(IVLCVout vlcVout) {
//
//    }
//
//    @Override
//    public void onSurfacesDestroyed(IVLCVout vlcVout) {
//
//    }
//  });
vout.attachViews();

不要忘记附上View(),这是重要的

这些代码对我有用,我希望它适合你