适用于Android的Youtube Player api未显示视频广告

时间:2015-01-11 10:43:07

标签: java android video android-youtube-api

我们使用youtubeplayer开发了一个应用程序...代码非常简单,基于youtubeplayer api提供的示例......

以下是代码......

  
public class YouTubeVideoActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{

    private static int RECOVERY_DIALOG_REQUEST = 1;
    String strVideo_Id="";
    YouTubePlayer youTubePlayer;
    YouTubePlayerView   youTubeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_video);
        this.setFinishOnTouchOutside(false);
        /*this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.y = 80; // top margin
        this.getWindow().setAttributes(layoutParams);
        Window window = this.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.TOP;
        window.setAttributes(wlp);*/


        strVideo_Id = getIntent().getStringExtra("VIDEO_ID");
        youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        int ACTIONBAR_HEIGHT = Integer.parseInt(getIntent().getStringExtra("ACTIONBAR_HEIGHT"))-5;


        YouTubePlayerView.LayoutParams params = (YouTubePlayerView.LayoutParams)youTubeView.getLayoutParams();
        ((MarginLayoutParams) params).setMargins(0, ACTIONBAR_HEIGHT, 0, 0); 

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point p = new Point();
        int iPicHeight ;
        int iWindowWidth ;
        int iWindowHeight;

        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN)
        {
            display.getRealSize(p);
            iWindowWidth = p.x;
            iWindowHeight = p.y;
        }
        else
        {
            iWindowWidth = display.getWidth();
            iWindowHeight =display.getHeight();
        }

        params.height = (int) (iWindowHeight * 0.30);


        youTubeView.setLayoutParams(params);
        youTubeView.initialize(Constants.YT_KEY, this);
        if(GenericOnTouchListner.mediaPlayer!=null && GenericOnTouchListner.mediaPlayer.isPlaying())
            RECOVERY_DIALOG_REQUEST = 2;
        else
            RECOVERY_DIALOG_REQUEST = 1;
    }

    public static void setMargins (View v, int l, int t, int r, int b) 
    {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) 
        {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }


    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason) 
    {
        if (errorReason.isUserRecoverableError()) 
        {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        }
        else 
        {
            String errorMessage = String.format("There was an error initializing the YouTubePlayer (%1$s)", 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(Constants.YT_KEY, this);
        }
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
    //  youTubeView.setVisibility(View.VISIBLE);
        if(youTubePlayer != null)
            youTubePlayer.play();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        youTubeView.setVisibility(View.VISIBLE);

        if(youTubePlayer!=null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //youTubePlayer.play();
        }

    }

    @Override
    protected void onPause() 
    {
        super.onPause();
        youTubeView.setVisibility(View.GONE);
        /*if(youTubePlayer!=null)
            youTubePlayer.pause();*/
    }


    protected YouTubePlayer.Provider getYouTubePlayerProvider()
    {
        return (YouTubePlayerView) findViewById(R.id.youtube_view);    
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,boolean wasRestored) 
    {
        youTubePlayer = player;
        youTubePlayer.setManageAudioFocus(true);
        youTubePlayer.setFullscreen(true);

        player.setFullscreen(true);

        youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI |
                YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE); 
        PlayerStyle style = PlayerStyle.DEFAULT;
        player.setPlayerStyle(style);
        if (!wasRestored) 
        {
            if(strVideo_Id != null)
            {
                Constants.bFromYouTube = true;
                //player.cueVideo(strVideo_Id);
                player.loadVideo(strVideo_Id);
                player.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
            }
        }
        youTubePlayer.play();

    }

    @Override
    public void onBackPressed()
    {
        if(youTubePlayer != null)
        {
            youTubePlayer.setFullscreen(false);
            Intent i = new Intent();
        }
        super.onBackPressed();
    }
}

我没有做任何具体的停止视频广告......但是,当我们加载特定视频时,视频广告不会显示...

欢迎任何意见......

1 个答案:

答案 0 :(得分:0)

这是因为视频广告基于每个视频提供,而频率广告类型均由YouTube决定

我注意到有两种类型的广告:一种是前贴片广告,另一种是幻灯片横幅广告

通常,第二种类型的广告经常出现,无论方向和/或YouTube播放器风格如何。

另一方面,前贴片视频更难以获得。它们通常在观看着名视频时提供,而方向/ YouTube播放器风格确实有所作为。例如,如果您选择PlayerStyle.CHROMELESS样式,则前贴片视频通常未投放

如果您想测试前贴片视频,请尝试播放当下的热门音乐视频。