【问题标题】:Android Youtube Player API activity finish and createAndroid Youtube Player API 活动完成并创建
【发布时间】:2013-10-19 13:14:22
【问题描述】:

我们使用了适用于 android 的 YouTube API,但是在快速关闭和打开 YouTube 播放器和 YouTube 播放器视图的相同活动时存在问题。此问题也出现在示例应用程序中,当我尝试打开 Fullscreen 活动(不单击 fullscreenbutton)然后使用后退按钮关闭活动时,一次又一次。

YouTube 应用程序崩溃了:

10-11 15:14:53.313: E/ActivityThread(22537): Activity com.example.myvideo.FullscreenDemoActivity 已泄露 ServiceConnection com.google.android.youtube.player.internal.r$e@46095818 原来绑定在这里

我试图覆盖OnStop 以释放播放器,但没有得到肯定的结果。请有人帮忙!

Fullscreen 活动修改 - 原版和这个有几行区别:

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


/**
 * Sample activity showing how to properly enable custom fullscreen behavior.
 * <p>
 * This is the preferred way of handling fullscreen because of the default fullscreen implementation
 * will cause re-buffering of the video.
 */
public class FullscreenDemoActivity extends YouTubeFailureRecoveryActivity implements
        View.OnClickListener,
        CompoundButton.OnCheckedChangeListener,
        YouTubePlayer.OnFullscreenListener {

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

    private LinearLayout baseLayout;
    private YouTubePlayerView playerView;
    private YouTubePlayer player;
    private Button fullscreenButton;
    private CompoundButton checkbox;
    private View otherViews;
    public boolean CanClose = false;
    private boolean fullscreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.fullscreen_demo);
            baseLayout = (LinearLayout) findViewById(R.id.layout);
            playerView = (YouTubePlayerView) findViewById(R.id.player);
            fullscreenButton = (Button) findViewById(R.id.fullscreen_button);
            checkbox = (CompoundButton) findViewById(R.id.landscape_fullscreen_checkbox);
            otherViews = findViewById(R.id.other_views);
            checkbox.setOnCheckedChangeListener(this);
            // You can use your own button to switch to fullscreen too
            fullscreenButton.setOnClickListener(this);
            playerView.initialize(DeveloperKey.DEVELOPER_KEY, this);
            doLayout();
        } catch (Exception e) { }
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
        try {
            this.player = player;
            player.setPlayerStyle(PlayerStyle.MINIMAL);
            //player.setShowFullscreenButton(true);
            setControlsEnabled();
            // Specify that we want to handle fullscreen behavior ourselves.
            player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
            player.setOnFullscreenListener(this);
            if (!wasRestored) {
                player.cueVideo(MainActivity.CurrentVideo);
            }
        } catch (Exception e) { }
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return playerView;
    }

    @Override
    public void onClick(View v) {
        player.setFullscreen(!fullscreen);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        try {
            int controlFlags = player.getFullscreenControlFlags();
            if (isChecked) {
                // If you use the FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE, your activity's normal UI
                // should never be laid out in landscape mode (since the video will be fullscreen whenever the
                // activity is in landscape orientation). Therefore you should set the activity's requested
                // orientation to portrait. Typically you would do this in your AndroidManifest.xml, we do it
                // programmatically here since this activity demos fullscreen behavior both with and without
                // this flag).
                setRequestedOrientation(PORTRAIT_ORIENTATION);
                controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            }
            player.setFullscreenControlFlags(controlFlags);
        } catch (Exception e) { }
    }

    private void doLayout() {
        try {
            LinearLayout.LayoutParams playerParams = (LinearLayout.LayoutParams) playerView.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;
                otherViews.setVisibility(View.GONE);
            } else {
                // This layout is up to you - this is just a simple example (vertically stacked boxes in
                // portrait, horizontally stacked in landscape).
                otherViews.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams otherViewsParams = otherViews.getLayoutParams();
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    playerParams.width = otherViewsParams.width = 0;
                    playerParams.height = WRAP_CONTENT;
                    otherViewsParams.height = MATCH_PARENT;
                    playerParams.weight = 1;
                    baseLayout.setOrientation(LinearLayout.HORIZONTAL);
                } else {
                    playerParams.width = otherViewsParams.width = MATCH_PARENT;
                    playerParams.height = WRAP_CONTENT;
                    playerParams.weight = 0;
                    otherViewsParams.height = 0;
                    baseLayout.setOrientation(LinearLayout.VERTICAL);
                }
                setControlsEnabled();
            }
        } catch (Exception e) { }
    }

    private void setControlsEnabled() {
        checkbox.setEnabled(player != null &&
                getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
        fullscreenButton.setEnabled(player != null);
    }

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

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

    @Override
    public void onStop() {
        if (player != null) {
            player.release();
            player = null;
        }
        super.onStop();
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        setResult(RESULT_OK, data);
        super.finish();
    }
}

【问题讨论】:

  • 你有没有发现如何解决这个问题?放入整个 applicationContext 不起作用,因为它没有实现 OnInitializedListener。

标签: android android-activity youtube youtube-api android-appcompat


【解决方案1】:

您可以执行以下步骤来确保内存泄漏问题得到解决:

  • 使用应用程序上下文
    • 创建一个名为 YoutubeMyAPI 的类,它实现了 YouTubePlayer.OnFullscreenListener
    • Application 类中创建该YoutubeMyAPI 的实例
  • 无论你在哪里传递 onInitializedListener 通过获取 ((MyApp)getApplicationContext()).instanceOfYoutubeMyAPI 传递 YoutubeMyAPI 的实例

希望您有实现此的总体思路显然这只是一种解决方法,有更好的方法可以做到这一点...

【讨论】:

    【解决方案2】:

    我知道这不是对您问题的正确答案,但大约 10 个月前,我自己在 YoutubeAPI 上苦苦挣扎。我真的很想使用来自 google 的 propper YoutubeAPI,因为那是官方 API 并且因为未来的支持,它正在内部制作等等。
    在面对你自己的许多挑战后,我终于给了另一个第三方图书馆一个机会,它是白天和黑夜

    com.pierfrancescosoffritti.androidyoutubeplayer 这是 pier francesco soffritti 的 depencency 的名称,如果您继续面临挑战,我强烈建议您尝试一下。当我尝试使用 YoutubeAPI 时,它已经有好几年没有得到维护了,我阅读并亲自面对到处的错误。

    Here 由该人本人撰写的一篇关于为什么第三方库是这里的必经之路的文章。

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 2014-09-13
      • 2017-11-15
      • 2015-02-10
      • 2017-10-12
      • 2014-05-27
      • 1970-01-01
      • 2012-07-01
      相关资源
      最近更新 更多