Android YouTube API - 无缓冲处理方向更改

时间:2013-02-04 16:36:06

标签: java android youtube-api buffer android-youtube-api

我正在使用Android YouTube API以纵向模式显示视频。在播放视频并将设备转换为横向模式时,视频将从现场停止但重新缓冲。我正在寻找一个解决方案,以避免重新缓冲。

我知道事实是可能的,因为Google提供了一个示例here,它可以完美无缺地工作,除非我似乎无法使其与我的应用程序一起工作。我不确定要覆盖哪些函数以及哪些函数实际上删除了重新缓​​冲。

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

<?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();
    }


}

1 个答案:

答案 0 :(得分:5)

将configChanges添加到你的android manifest:

let defaults = NSUserDefaults.standardUserDefaults()

 if let savedPeople = defaults.objectForKey("people") as? NSData 

     people = NSKeyedUnarchiver.unarchiveObjectWithData(savedPeople) as! [Person] 

简单地解决我的问题:

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

旋转屏幕时。

干杯。