【我的Android進階之旅】安卓應用如何顯示在外接屏幕上

Android平板可以外接一塊顯示器,那麼現在如果將顯示內容顯示在副屏(顯示器)上,該如何實現呢?

這裏要用到Android的Presentation這個API:點擊查看API詳情

我們現在新建一個Android項目

首先,先設置權限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  • 1

然後新建一個DifferentDislay類,:

package com.remixdemo;

import android.app.Presentation;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.widget.LinearLayout;
import android.widget.VideoView;

import com.remixdemo.media.IjkVideoView;

/**
 * Created by waynian on 2017/9/6.
 */

public class DifferentDisplay extends Presentation {
    public VideoView videoView;

    public DifferentDisplay(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_video);

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

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在這個類中,我們設置了一個佈局,這個佈局很簡單,就設置一個VideoViewplay_video.xml的內容如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rn_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#299361"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </VideoView>

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

最後在MainActivity文件中設置顯示:

 public class MainActivity extends AppCompatActivity {
    private DifferentDisplay presentation;
    private Display[] presentationDisplays;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayManager displayManager = (DisplayManager)   reactContext.getSystemService(Context.DISPLAY_SERVICE);
        //獲取屏幕數量
        presentationDisplays = displayManager.getDisplays();
        if (presentationDisplays.length > 1) {
            presentation = new DifferentDisplay(reactContext, presentationDisplays[1]);
        } else {
            presentation = new DifferentDisplay(reactContext, presentationDisplays[0]);
        }
        presentation.show();
        videoView = presentation.videoView;

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

現在VideoView就會顯示在副屏上,主屏可以控制VideoView的播放,暫停等操作了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章