【问题标题】:YouTubeFragmentPlayer Android App - Only Black Box appearingYouTubeFragmentPlayer Android 应用程序 - 仅出现黑框
【发布时间】:2015-03-02 12:09:36
【问题描述】:

我正在尝试构建主要活动包含 YouTubePlayerFragment 的示例 Android 应用程序。我的实现没有任何错误,但是当我在 AVD 或手机上运行此应用程序时,YouTubePlayerFragment 的片段只是一个黑盒子。没有视频加载。

非常感谢任何帮助。

这是我的代码:

YouTubeFragment.java

package androidsample.example.com.fragments1;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Toast;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class YouTubeFragment extends YouTubePlayerFragment implements YouTubePlayer.OnInitializedListener {

    // TODO: Rename and change types and number of parameters
    public static YouTubeFragment newInstance() {
        YouTubeFragment fragment = new YouTubeFragment();
        return fragment;
    }

    private void init(){
        initialize(DeveloperKey.DEVELOPER_KEY, this);
    }

    public YouTubeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_main, container, false);

        YouTubeFragment ytf = newInstance();
        ytf.init();
        //inside fragment use getFragmentManager instead of getFragmentSupportManager
        getFragmentManager().beginTransaction()
                .add(R.id.youTubePlayer, ytf)
                .commit();


        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            player.cueVideo("nCgQDjiotG0");
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        Toast.makeText(getActivity(), "Failured to Initialize!", Toast.LENGTH_LONG).show();
    }


}

MainActivity.java

package androidsample.example.com.fragments1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <fragment
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:id="@+id/youTubePlayer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</RelativeLayout>

【问题讨论】:

  • 您的开发者密钥/Google API 控制台配置是否正确设置?
  • 是的,刚刚在 Google 文档中的示例应用程序中使用我的密钥检查了这一点,它可以工作。

标签: java android android-fragments youtube-api


【解决方案1】:

在一些帮助下,我有一个工作版本(目标 SDK 21 的问题,使用 19):

YouTubeFailureRecoveryActivity.java

package androidsample.example.com.fragments2;

import android.widget.Toast;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;


public class YouTubeFailureRecoveryActivity extends YouTubeBaseActivity implements
        YouTubePlayer.OnInitializedListener{

    private static final int RECOVERY_DIALOG_REQUEST = 1;

    @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(R.string.error_player), errorReason.toString());
            String errorMessage = "custom ERror MessAgE";
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer player,
                                        boolean wasRestored) {
        // TODO Auto-generated method stub
        if (!wasRestored) {
            player.cueVideo("nCgQDjiotG0");
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        // TODO Auto-generated method stub
        return (YouTubePlayerFragment) getFragmentManager().findFragmentById(
                R.id.youtube_fragment);
    }
}

MainActivity.java

package androidsample.example.com.fragments2;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class MainActivity extends YouTubeFailureRecoveryActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
                .findFragmentById(R.id.youtube_fragment);
        youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <fragment
        android:id="@+id/youtube_fragment"
        android:name="com.google.android.youtube.player.YouTubePlayerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    我认为您不需要创建 newInstance()。该实例是在您的 xml 中创建的。当您在 onCreateView 中时,这已经是一个特定的实例。 所以你应该可以替换这些行

    YouTubeFragment ytf = newInstance();
    ytf.init();
    

    this.init();
    

    或者更简单

    init();
    

    【讨论】:

    • 这是有道理的。试过了,还是一样的问题。
    • 设备是否有 4.2.16 或更高版本的 YouTube 应用?
    • 是的,我在 AVD 上安装了 YouTube 6.0.11 apk。在运行 Lollipop 的手机(Nexus 5)上也有同样的问题。
    • 你检查过这个答案吗? stackoverflow.com/questions/18664934/… 从外观上看,它们与您的主要区别在于您不需要覆盖 onCreateView() 并且它们会添加片段后记。就像用 xml 中的框架布局替换它并执行 getFragmentManager().beginTransaction() .add(R.id.youTubePlayer, ytf).commit();在主要活动中
    • 已移动我的代码以匹配您引用的示例。将片段的创建/初始化移动到 onCreate() 中的主要活动。同样在主要活动 XML 中使用空 FrameLayout 现在得到java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerView.a()' on a null object reference
    猜你喜欢
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多