首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Youtube Player API活动完成并创建

Android Youtube Player API活动完成并创建
EN

Stack Overflow用户
提问于 2013-10-11 20:42:33
回答 2查看 4.3K关注 0票数 54

我们已经使用了android的YouTube API,但是在YouTube播放器和YouTube播放器视图中快速关闭和打开相同的activity时会出现问题。这个问题也出现在示例应用程序中,当我尝试打开Fullscreen activity (不单击fullscreenbutton),然后用back按钮关闭activity,然后一次又一次地关闭。

YouTube应用程序崩溃的原因如下:

10-11 15:14:53.313: E/ActivityThread(22537):Activity com.example.myvideo.FullscreenDemoActivity泄漏了最初绑定在此处的ServiceConnection线程

我试图重写OnStop来释放播放器,但是没有得到积极的结果。快来人帮帮忙!

Fullscreen活动修改-原始活动和此活动之间的几行差异:

代码语言:javascript
复制
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();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2021-08-16 08:22:04

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

  • 使用应用程序实现YouTubePlayer.OnFullscreenListener

的类,例如名为YoutubeMyAPI的类

    • create Class

中该YoutubeMyAPIApplication实例

通过获取((MyApp)getApplicationContext()).instanceOfYoutubeMyAPI传递YoutubeMyAPIonInitializedListener实例的任何位置的

希望你对实现这个有了大概的想法,显然这只是一个变通办法,有更好的方法可以做到这一点...

票数 1
EN

Stack Overflow用户

发布于 2021-10-10 01:18:45

我知道这对你的问题不是一个合适的答案,但大约10个月前,我自己也在为YoutubeAPI而苦苦挣扎。我真的很想使用谷歌的propper,因为那是官方的YoutubeAPI,因为它是未来的支持,它是内部制造的,等等。

在面对许多挑战之后,比如你自己的,我终于给了另一个第三方库一个机会,这是日以继夜的

这是pier francesco soffritti的depencency的名字,如果你继续面临挑战,我强烈建议你试一试。当我尝试使用YoutubeAPI的时候,它已经有好几年没有维护过了,我在书中读到过,我个人也发现到处都是bug。

Here是他自己写的一篇文章,讲述了为什么第三方库是这里的首选。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19318442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档