2

我正在使用 Android YouTube API 以纵向模式显示视频。播放视频并将设备转为横向模式后,视频会从停止的位置继续播放,但会重新缓冲。我正在寻找如何避免重新缓冲的解决方案。

我知道这是可能的,因为谷歌在这里提供了一个例子,它完美地工作,除了我似乎无法让它与我的应用程序一起工作。我不确定要覆盖哪些函数以及哪些函数实际上删除了重新缓​​冲。

编辑:这是 ListView 中单元格的完整 xml;YouTubePlayerView 在最后一个 TextView 之后以编程方式添加。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/youtube_ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_shape"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/header_background"
        android:weightSum="2" >

        <TextView
            android:id="@+id/youtube_username_tv"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="top|left"
            android:paddingLeft="15dp"
            android:paddingTop="8dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/darker_gray"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/youtube_when_tv"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="top|right"
            android:paddingRight="15dp"
            android:paddingTop="8dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/darker_gray"
            android:textSize="13sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/youtube_content_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="17dp"
        android:paddingTop="8dp"
        android:paddingBottom="15dp"
        android:textSize="15sp" />

</LinearLayout>

我知道在java中你需要覆盖“onConfigurationChanged(Configuration newConfig)”,也许还需要覆盖“onFullscreen(boolean isFullscreen)”,但是你究竟如何在不重新缓冲的情况下“增长”youtube视图?我尝试使用全屏示例中的代码,但它似乎不适合我。这是播放器本身:

public class YouTubeParser extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener, YouTubePlayer.OnFullscreenListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private String youtubeUrl;
    private com.google.android.youtube.player.YouTubePlayerView youtube;
    private YouTubePlayer player;
    private Context mContext;
    private boolean fullscreen;

    private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
            ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                    : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

    public YouTubeParser(View v, Context context, Post post, String lastUrl) {
        this.mContext = context;
        youtubeUrl = post.getYoutubeUrl();
        TextView username = (TextView) v.findViewById(R.id.youtube_username_tv);
        TextView when = (TextView) v.findViewById(R.id.youtube_when_tv);
        TextView content = (TextView) v.findViewById(R.id.youtube_content_tv);


        if (username != null)
            username.setText(post.getUsername());
        if (when != null)
            when.setText(post.getWhen());
        if (content != null) {
            content.setText(Html.fromHtml(post.getContent()));
            content.setMovementMethod(LinkMovementMethod.getInstance());
        }
        if (lastUrl != youtubeUrl) {
            youtube = new com.google.android.youtube.player.YouTubePlayerView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) LayoutParams.MATCH_PARENT, (int) LayoutParams.WRAP_CONTENT);
            youtube.setLayoutParams(params);

            LinearLayout ll = (LinearLayout) v.findViewById(R.id.youtube_ll);
            if (ll.getChildCount() == 4)
                ll.removeViewAt(2);
            ll.addView(youtube, 2);
        }
        if (youtube != null) {
            youtube.initialize(DeveloperKey.DEVELOPER_KEY, this);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(getString(0), errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
        }
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
        this.player = player;
        player.setPlayerStateChangeListener(this);
        if (!wasRestored) {
            player.cueVideo(youtubeUrl);
            Log.i("Position", "video cued: " + youtubeUrl);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return youtube;
    }

    public void setNoLandscape() {
        if (player != null) {
            int controlFlags = player.getFullscreenControlFlags();
            controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
            if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                player.pause();
        }
    }

    public void setToLandscape() {
        if (player != null) {
            int controlFlags = player.getFullscreenControlFlags();
            controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
        }
    }

    @Override
    public void onAdStarted() {
    }

    @Override
    public void onError(ErrorReason arg0) {
    }

    @Override
    public void onLoaded(String arg0) {
    }

    @Override
    public void onLoading() {
    }

    @Override
    public void onVideoEnded() {
        setNoLandscape();
    }

    @Override
    public void onVideoStarted() {
        int controlFlags = player.getFullscreenControlFlags();
        controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
        player.setFullscreenControlFlags(controlFlags);
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        fullscreen = isFullscreen;
        doLayout();

    }

    private void doLayout() {
        LinearLayout.LayoutParams playerParams =
                (LinearLayout.LayoutParams) youtube.getLayoutParams();
        if (fullscreen) {
            // When in fullscreen, the visibility of all other views than the player should be set to
            // GONE and the player should be laid out across the whole screen.
            playerParams.width = LayoutParams.MATCH_PARENT;
            playerParams.height = LayoutParams.MATCH_PARENT;

        } else {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                playerParams.width = 0;
                playerParams.height = WRAP_CONTENT;
                playerParams.weight = 1;
            } else {
                playerParams.width = MATCH_PARENT;
                playerParams.height = WRAP_CONTENT;
                playerParams.weight = 0;
            }
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        doLayout();
    }


}
4

1 回答 1

6

通过将 configChanges 添加到您的 android 清单中:

<activity android:name=".InfoActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name" >
</activity>

简单地解决我的问题:

  • 再次开始缓冲
  • 重新从头开始

当屏幕旋转时。

干杯。

于 2015-09-25T22:39:52.450 回答