簡體   English   中英

如何從 android 中的 VLCVideoLayout 中刪除頂部和底部黑條

[英]How to remove top and bottom black bar from VLCVideoLayout in android

這是我的 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/firstLayout"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/secondView"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/videoFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <org.videolan.libvlc.util.VLCVideoLayout
            android:id="@+id/videoLayout"
            android:fitsSystemWindows="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>

</LinearLayout>



<LinearLayout
    android:id="@+id/thirdLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/secondView"
    android:layout_weight="1"
    android:background="@android:color/holo_orange_light"
    android:orientation="vertical">


</LinearLayout>

這是我的 class:

public class WifiActivity extends AppCompatActivity {
    private BroadcastReceiver MyReceiver = null;

    private static final String url = " rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";

    private LibVLC libVlc;
    private MediaPlayer mediaPlayer;
    private VLCVideoLayout videoLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        libVlc = new LibVLC(this);
        mediaPlayer = new MediaPlayer(libVlc);
        videoLayout = findViewById(R.id.videoLayout);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mediaPlayer.attachViews(videoLayout, null, false, false);

        Media media = new Media(libVlc, Uri.parse(url));
        media.setHWDecoderEnabled(true, false);
        media.addOption(":network-caching=600");

        mediaPlayer.setMedia(media);
        media.release();
        mediaPlayer.play();

    }
}

運行此代碼后,我可以使用給定的 URL 顯示實時流,如下所示。

在此處輸入圖像描述

我正在嘗試刪除給定圖像中顯示的黑條我想保持全屏請幫助我如何實現這一點我無法找到任何解決方案。

在此處輸入圖像描述

您正在為VLCVideoLayout設置layout_height="match_parent" ,因此根據編寫的代碼,目前它的行為正常

如果你想要這個視圖的不同高度,那么你應該計算它並為VLCVideoLayout設置。 這是一個有趣的事實:這個 class 將在運行時重新設置它的寬度和高度(查看onAttachedToWindow方法),所以即使你在 XML 中設置了一些固定值 - 這些也會被覆蓋。 它是由庫作者出於“某種原因”制作的,所以我們不要碰這個 class,但現在我們知道VLCVideoLayout將始終拉伸/適合父級

所以現在我將為這個視頻視圖創建一些父 class

<LinearLayout
    android:id="@+id/firstLayout"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/secondView"
    android:orientation="vertical">

        <FrameLayout
            android:id="@+id/videoFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <org.videolan.libvlc.util.VLCVideoLayout
                android:id="@+id/videoLayout"
                android:fitsSystemWindows="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
            
        </FrameLayout>

</LinearLayout>

現在我們知道幀/視頻將match_parent寬度,那么我們可以通過測量屏幕來測量它。 如果該視頻將被打包到一些更復雜的布局中,例如角落中的一些小視頻幀 - 您應該測量該區域的大小(在這種情況下通常具有固定的寬度/大小)

知道videoFrame寬度,您只需乘以視頻的比率即可輕松計算高度

int scrWidth = getScreenWidth();
FrameLayout frameLayout = findViewById(R.id.videoFrame);
double ratio = 16d/9d;  // 16:9
frameLayout.getLayoutParams().height = (int)(scrWidth / ratio);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM