簡體   English   中英

縱向模式下的Android Landscape VideoView

[英]Android Landscape VideoView in portrait mode

我有一個活動,該活動應該以橫向顯示VideoView,但是活動本身不能處於橫向模式,所以這就是我所擁有的。

    <activity
        android:name=".gui.VideoPlayer"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >
    </activity>

<?xml version="1.0" encoding="utf-8"?>

活動

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

    <VideoView
        android:id="@+id/myvideoview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" />

</LinearLayout>

(活動本身必須保持為縱向模式,因為設備將放置在覆蓋導航和狀態欄的外殼中,但是在橫向模式下,它將不再覆蓋它們)

我知道這不是最佳答案,但前一段時間我遇到了同樣的問題,並且找到了一個非常方便的解決方法,它也可能會對您有所幫助。

嘗試使用TextureView而不是VideoView,因為旋轉它時,里面的內容也會旋轉。

 <TextureView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_texture_view"
    android:rotation="90"
    android:scaleX="1.8"
    />

接下來,您必須創建一個SurfaceTextureListener並將其設置為您的視圖,如下所示:

 TextureView.SurfaceTextureListener mTextureListener = new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {

        Surface s = new Surface(surfaceTexture);
        try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(currentPageVideoUrl);
        mMediaPlayer.setSurface(s);
        mMediaPlayer.prepare();
        }catch (Exception e){e.printStackTrace();}
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }
};

現在,只需將偵聽器設置為您的TextureView:

 mTextureView.setSurfaceTextureListener(mTextureListener);

剩下要做的就是調用它來開始播放視頻:

mMediaPlayer.start();

獎金:如果您想進行更復雜的調整,例如調整視頻寬高比,則可以查看此庫以獲取更多詳細信息。

暫無
暫無

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

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