Android 双屏异显

Android平板可以外接一块显示器,那么现在如果将VideoView显示在副屏(显示器)上,该如何实现呢?

这里要用到Android的Presentation这个API:点击查看API详情

我们现在新建一个Android项目

首先,先设置权限:

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

然后新建一个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);

    }
}

在这个类中,我们设置了一个布局,这个布局很简单,就设置一个VideoViewplay_video.xml的内容如下:


<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>

最后在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;

    }

现在VideoView就会显示在副屏上,主屏可以控制VideoView的播放,暂停等操作了。

你可能感兴趣的:(Android,Android多媒体)