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
在這個類中,我們設置了一個佈局,這個佈局很簡單,就設置一個VideoView
,play_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的播放,暫停等操作了。