在片段部分使用YoutubePlayerView

问题描述 投票:0回答:1

我发现了一些类似的情况,比如thisthis,但没有一个解决了我的问题。

我想要一个带有youtube视频的屏幕和一些像这样的文字信息:

enter image description here

我正在使用一个活动和一个片段,它们都有扩展YouTubeBaseActivity和YouTubePlayerFragment的基类。

但是,当我试图打开片段时,它会显示一个npe,但它没有显示在哪里。由于我正在获得布局和视图,我现在不知道发生了什么。

希望它没有得到downvotes因为是一个npe问题,但与通常不同,这个API调用不会告诉我NPE发生在哪里,我看到其他人有这样的问题

Obs:我正在使用这个扩展基础概念,因为更多的地方会有这种视频行为,我试图避免代码重复。

Logcat enter image description here

XML

<android.support.constraint.ConstraintLayout
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".screens.mine.fragments.MineStepsFragment">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/mine_steps_youtube_player"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/background_white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
    </com.google.android.youtube.player.YouTubePlayerView>

</android.support.constraint.ConstraintLayout>

活动

public class MineAccidentActivity extends BaseYoutubeActivity {

@Override
protected void initializeActionBar() {
    actionbarLeftBtn.setVisibility(View.VISIBLE);
    actionbarTitle.setVisibility(View.VISIBLE);
}

@Override
protected int getActionbarTitle() {
    return R.string.mine_accident;
}

@Override
protected int getContentView() {
    return R.layout.mine_accident_activity;
}

@Override
protected void assignViews() {
    MineAccidentController.getInstance().attachToActivity(this, R.id.mine_container);
}

@Override
protected void prepareViews() {}

///////////////////  BACK  ////////////////////
@Override
public void onBackPressed() {
    if (!MineAccidentController.getInstance().isFirstFragmentShown()) {
        MineAccidentController.getInstance().showPreviousFragment();
    } else {
        super.onBackPressed();
    }
}

///////////////////  LIFE CYCLE  ////////////////////
@Override
protected void onDestroy() {
    MineAccidentController.getInstance().onDestroy();
    super.onDestroy();
}

}

片段控制器

public class MineAccidentController extends BaseYoutubeController {

private String accidentType;

@Override
protected ArrayList<android.app.Fragment> initFragments() {
    ArrayList<android.app.Fragment> fragments = new ArrayList<>();
    fragments.add(new MineStepsFragment());
    return fragments;
}

//create Class
public static MineAccidentController getInstance() {
    if (null == instance) {
        synchronized (MineAccidentController.class) {
            if (null == instance) {
                setInstance(new MineAccidentController());
            }
        }
    }
    return (MineAccidentController) instance;
}

public String getAccidentType() {
    return accidentType;
}

public void setAccidentType(String accidentType) {
    this.accidentType = accidentType;
}
}

分段

public class MineStepsFragment extends BaseYoutubeFragment {

//Not in Layout
private String videoUrl;

////////////// IMPLEMENT_METHODS //////////////
@Override
protected int getFragmentContentView() {
    return R.layout.mine_steps_fragment;
}

@Override
protected int getYoutubePlayerView() {
    return R.id.mine_steps_youtube_player;
}

@Override
protected void assignViews() {

}

@Override
protected void prepareViews() {

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

    if(!wasRestored){
        checkType();
        youTubePlayer.cueVideo(videoUrl);
    }
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    showErrorToast(getActivity(), R.string.error_initialize_video);
}

////////////// FUNCTIONS //////////////
private void checkType() {
    if(MineAccidentController.getInstance().getAccidentType().equals(Parameters.ACCIDENT_PERSONAL)){
        videoUrl = Properties.MINE_PERSONAL_VIDEO;
    }
    else {
        videoUrl = Properties.MINE_WORK_VIDEO;
    }
}

}

基础片段

public abstract class BaseYoutubeFragment extends YouTubePlayerFragment implements YouTubePlayer.OnInitializedListener {
protected View fragmentView = null;
protected YouTubePlayerView youTubePlayerView;

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    assignViews();
    prepareViews();
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fragmentView = inflater.inflate(getFragmentContentView(), container, false);


    youTubePlayerView = fragmentView.findViewById(getYoutubePlayerView());
    youTubePlayerView.initialize(com.can_apps.eva_ngo.properties.Properties.API_KEY, this);


    return fragmentView;
}


///////////////////  ABSTRACT METHODS  ////////////////////
protected abstract int getFragmentContentView();        //Get Layout R.layout.name_file

protected abstract int getYoutubePlayerView();  //Get Container for YOutube Video

protected abstract void assignViews();          //Used for findById the params

protected abstract void prepareViews();         //Used for start the values of params

///////////////////  SHOW MESSAGES  ////////////////////
public void showErrorToast(Context context, final int message) {
    Toast toast = Toast.makeText(context, getString(message), Toast.LENGTH_SHORT);
    View view = toast.getView();
    view.setBackgroundResource(R.color.background_red_transparent);
    toast.show();
}

///////////////////  SNACK BAR  ////////////////////
public void showSnackBar(final int text) {
    Snackbar.make(Objects.requireNonNull(getView()), getString(text), Snackbar.LENGTH_SHORT).show();
}

public void showSnackBar(final int mainTextStringId, final int actionStringId, View.OnClickListener listener) {
    Snackbar.make(Objects.requireNonNull(getView()),
            getString(mainTextStringId),
            Snackbar.LENGTH_LONG)
            .setAction(getString(actionStringId), listener).show();
}

}
android android-fragments android-youtube-api
1个回答
1
投票

看一下logcat,当你调用它的一个方法时,你的YouTubePlayerView看起来像是null。

YouTube播放器API非常错误且难以正确使用。为了解决这个问题(以及其他问题),我已经建立了一个替代播放器Android-YouTube-Player,它是开源的,你可以用它做任何你想做的事情。

在您的情况下,您不必干涉碎片和交易,因为我的YouTubePlayerView只是一个常规视图,不需要特殊的碎片或活动。你可以把它放在任何你想要的地方。

希望它对你也有用!

© www.soinside.com 2019 - 2024. All rights reserved.