libvlc android视频大小不适合SurfaceView [英] libvlc android video size does not fit the surfaceview

查看:406
本文介绍了libvlc android视频大小不适合SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我已经成功构建了 libvlc android并将其嵌入到react native项目中.

Here I've build libvlc android successfully and embed it to a react native project.

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

The audio and video stream both work fine.

让我感到困惑的是,视频内容的大小不适合 surface & view 很好,尽管我已经将它们设置为 MATCH_PARENT .

Things make me puzzled is the video content size not fit surface & view well, although I've set them 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();
    }
  }
  ... ...
}

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

Here is a snapshot while the app running.

任何帮助将不胜感激!

推荐答案

您可以尝试使用显示"指标来获取您的Surfaceview尺寸并设置IVLCVout窗口尺寸

You can try to use Display metrics it's getting your surfaceview size and setting IVLCVout windows size

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

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

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

and final step is setting IVLCVount windows size :

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();

别忘了attachView()很重要

Don't forget to attachView() it's important

这些代码对我有用,希望对您有用

These code work for me I hope it works for you

这篇关于libvlc android视频大小不适合SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆