画面共有のためのAndroidコード例

概要

このガイドでは、Android SDKを使用して画面共有ビデオを公開する方法を説明します。

プロジェクトの設定

このセクションのコードは、Screen-Sharing-Javaプロジェクトの opentok-android-sdk-samples レポジトリにクローンする必要があります。まだの場合は、レポをローカル・ディレクトリにクローンする必要がある。コマンドラインで

git clone git@github.com:opentok/opentok-android-sdk-samples.git

を開く。 スクリーン・シェアリング-Java プロジェクトをAndroid Studioで実行する。

重要だ: あなたの アプリID はあなたの APIキー.

コードを探る

について MainActivity クラス使用 WebView オブジェクトを、公開ストリームの画面共有ビデオのソースとして使用します。

の中で initializePublisher() のメソッドを使用する。 MainActivity クラスで、Publisher.Builder オブジェクトを作成した後、コードは capturer() のメソッドを使用する。 Publisher.Builder オブジェクトを渡す。 ScreenSharingCapturer オブジェクトをパラメータとして渡す:

ScreenSharingCapturer screenSharingCapturer = new ScreenSharingCapturer(MainActivity.this, webViewContainer);

publisher = new Publisher.Builder(MainActivity.this)
        .capturer(screenSharingCapturer)
        .build();

ScreenSharingCapturer を拡張したカスタムクラスです。 BaseVideoCapturer クラス (Vonage Video SDK で定義) を使用します。このクラスを使用すると、Vonage Video パブリッシャーで使用するカスタムビデオキャプチャを定義できます。このクラスのコンストラクタは ScreenSharingCapturer クラスにはAndroid Viewオブジェクトが渡され、キャプチャーはこれをビデオのソースとして使用する。

について getCaptureSettings() メソッドは、カスタムビデオキャプチャで使用するキャプチャ設定を初期化する:

@Override
public CaptureSettings getCaptureSettings() {

    CaptureSettings captureSettings = new CaptureSettings();
    captureSettings.fps = fps;
    captureSettings.width = width;
    captureSettings.height = height;
    captureSettings.format = ARGB;
    return captureSettings;
}

について startCapture() メソッドは frameProducer 1/15秒後にスレッド:

@Override
public int startCapture() {
    capturing = true;

    handler.postDelayed(newFrame, 1000 / fps);
    return 0;
}

について frameProducer スレッドは Bitmap を表現している。 contentViewオブジェクト WebViewを呼び出す。 provideIntArrayFrame() メソッドで、そのバッファをパラメータとして渡す:

private Runnable newFrame = new Runnable() {
    @Override
    public void run() {
        if (capturing) {
            int width = contentView.getWidth();
            int height = contentView.getHeight();

            if (frame == null ||
                    ScreenSharingCapturer.this.width != width ||
                    ScreenSharingCapturer.this.height != height) {

                ScreenSharingCapturer.this.width = width;
                ScreenSharingCapturer.this.height = height;

                if (bmp != null) {
                    bmp.recycle();
                    bmp = null;
                }

                bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                canvas = new Canvas(bmp);
                frame = new int[width * height];
            }
            canvas.saveLayer(0, 0, width, height, null);
            canvas.translate(-contentView.getScrollX(), - contentView.getScrollY());
            contentView.draw(canvas);

            bmp.getPixels(frame, 0, width, 0, 0, width, height);

            provideIntArrayFrame(frame, ARGB, width, height, 0, false);

            canvas.restore();

            handler.postDelayed(newFrame, 1000 / fps);

        }
    }
};

について provideIntArrayFrame<()/code> メソッドで定義されている。 BaseVideoCapturer クラスは整数配列のデータをパブリッシャーに送信します。

パブリッシャーがまだビデオをキャプチャしている場合、スレッドはさらに1/15秒後に再スタートし、キャプチャーはパブリッシャーに新しいビデオフレームを提供し続ける。