tcking / GiraffePlayer

android video player base on ijkplayer
Apache License 2.0
681 stars 199 forks source link

在Fragment中无法播放 #52

Open sendtion opened 8 years ago

sendtion commented 8 years ago

在ViewPager+Fragment中无法新建播放器,报错! 在Activity中正常,错误指示无法获取videoview的xml中的控件。

tcking commented 8 years ago

支持fragment,你需要在onViewCreated中初始化GiraffePlayer

sendtion commented 8 years ago

@tcking 我就是在onViewCreated中初始化的,GiraffePlayer初始化需要传入Activity,我使用getActivity传入不可行,或者有什么别的方法传入activity?

tcking commented 8 years ago

fragment加载的布局需要include giraffe_player.xml,在onViewCreated初始化传入getActivity ()就行

sendtion commented 8 years ago

@tcking 没错啊,我就是这样做的。

fragment_tab_learning.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" tools:context="com.bri.cdstm.ui.fragment.TabLearningFragment">

<include
    android:id="@+id/player_learning"
    layout="@layout/giraffe_player"
    android:layout_width="match_parent"
    android:layout_height="200dp" />

<com.jcodecraeer.xrecyclerview.XRecyclerView
    android:id="@+id/rv_list_learning"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/player_learning" />

**TabLearningFargment.java** @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tab_learning, null); //iv_learning_banner = (ImageView) view.findViewById(R.id.iv_learning_banner); rv_list_learning = (XRecyclerView) view.findViewById(R.id.rv_list_learning); player = new GiraffePlayer(getActivity()); }`

但是仍然报错。 videoView = (IjkVideoView) activity.findViewById(R.id.video_view); videoView.setAspectRatio(IRenderView.AR_MATCH_PARENT); 在上面一句,videoview报空指针异常。应该是不能通过传过来的activity获得videoview。

tcking commented 8 years ago

注意,是在onViewCreated,不是onCreatView

sendtion commented 8 years ago

@tcking 仍然不行。

@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); player = new GiraffePlayer(getActivity()); }

View liveBox = activity.findViewById(R.id.app_video_box); liveBox.setClickable(true); 这里报错,错误如下。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setClickable(boolean)' on a null object reference at tcking.github.com.giraffeplayer.GiraffePlayer.<init>(GiraffePlayer.java:332) at com.bri.cdstm.ui.fragment.TabLearningFragment.onViewCreated(TabLearningFragment.java:134)

hisonedumobile commented 7 years ago

@tcking @sendtion "fragment加载的布局需要include giraffe_player.xml ,在 onViewCreated 初始化传入 getActivity () 就行",我的项目中这样做以后,确实可以

hisonedumobile commented 7 years ago

@sendtion 我的xml文件: <include layout="@layout/giraffe_player" android:layout_width="match_parent" android:layout_height="210dp"/> 我的fragment文件中: @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mVedioPlayer = new GiraffePlayer(this.getActivity()); if (mVideoPath == "") { // Tell the user to provide a media file URL/path. Toast.makeText( getActivity(), "No Video Found! Press Back Button To Exit", Toast.LENGTH_LONG).show(); } else { mVedioPlayer.play(mVideoPath); } }