通过SurfaceView(CameraPreview)

时间:2016-02-09 00:00:29

标签: android youtube-api android-youtube-api

我的活动有一个必须在SurfaceView(相机预览版)上显示的YouTubePlayerView。

但是,由于YouTubePlayerView的视频视图也使用SurfaceView,因此不会显示Youtube视频。控制和播放按钮是可见的,您可以在播放视频时听到音频,当它停止时,其缩略图可见。

我知道处理这种情况的正确方法是使用setZOrderMediaOverlay。我可以通过Camera Preview上的常规VideoView使其正常工作。但是,我如何在YouTubePlayerView中使用setZOrderMediaOverlay?

这个问题有解决办法吗?

以下是代码的精简版本:

package com.moahh.yc;

import android.os.Bundle;
import android.widget.RelativeLayout;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;

public class CameraActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener {

    private CameraPreview cameraPreview;
    private YouTubePlayerView youTubePlayerView;
    private final String videoId = "jNQXAC9IVRw";

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

        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
        cameraPreview = new CameraPreview(this);
        relativeLayout.addView(cameraPreview);

        youTubePlayerView = new YouTubePlayerView(this);
        relativeLayout.addView(youTubePlayerView);

        youTubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraPreview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraPreview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraPreview.onDestroy();
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer youTubePlayer, boolean restored) {
        if (restored) {
            youTubePlayer.play();
        } else {
            youTubePlayer.loadVideo(videoId);
            youTubePlayer.setShowFullscreenButton(false);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult youTubeInitializationResult) {
    }
}

活动的布局只是一个RelativeLayout:

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

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,当PlayerStateChangeListener调用onVideoStarted时,我解决了添加视图的问题。

youTubePlayerView.setPlayerStateChangeListener(new YouTubePlayer.PlayerStateChangeListener() {

           @Override
            public void onVideoStarted() {
                cameraPreview = new CameraPreview(this);
                relativeLayout.addView(cameraPreview);
            }
            [...]
        });

答案 1 :(得分:0)

我有同样的问题.. 启动YouTube视图后,尝试启动CameraPreview。 意味着onInitializationSuccess就像这样:

@Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                        YouTubePlayer youTubePlayer, boolean restored) {
    if (restored) {
        youTubePlayer.play();
        cameraPreview = new CameraPreview(this);
        relativeLayout.addView(cameraPreview);        
} else {
        youTubePlayer.loadVideo(videoId);
        youTubePlayer.setShowFullscreenButton(false);
    }
}