使用片段的多个Exoplayer实例

时间:2018-04-10 07:35:53

标签: android android-fragments exoplayer exoplayer2.x

我的目标是使用ExoPlayer 2同时在一个Activity显示几个视频。(我为每个视频获取了hls源代码)。我成功播放了一个视频。因此,我决定在Fragment内部实施播放器,并为每个hls源创建新的Fragment,以便将它们放在Activity中。但只有一个Fragment成功播放视频,其他Fragment看起来像黑色方块而没有任何内容。如何解决?

我使用的是ExoPlayer 2.7.2。

我的Activity代码

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

Bundle bundle1 = new Bundle();
bundle1.putString(SmallPlayerfragment.VIDEO_KEY, SmallPlayerfragment.Source1);


Bundle bundle2 = new Bundle();
bundle2.putString(SmallPlayerfragment.VIDEO_KEY, SmallPlayerfragment.Source2);

Fragment fragment1 = new SmallPlayerfragment();
fragment1.setArguments(bundle1);

Fragment fragment2 = new SmallPlayerfragment();
fragment2.setArguments(bundle2);
if (getSupportFragmentManager().findFragmentByTag(SmallPlayerfragment.TAG1) == null
        | getSupportFragmentManager().findFragmentByTag(SmallPlayerfragment.TAG2) == null)
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.test_container, fragment2, SmallPlayerfragment.TAG2)
            .replace(R.id.bottom_test_container, fragment1, SmallPlayerfragment.TAG1)
            .commit();
}

我的Fragment代码

public class SmallPlayerfragment extends Fragment {
    String mVideoURL;
    public final static String VIDEO_KEY = "videoKey";
    public final static String Source2 = "source2";
    public final static String Source1 = "source1";
    public final static String TAG1 = "fragment_1";
    public final static String TAG2 = "fragment_2";

    PlayerView mPlayerView;
    SimpleExoPlayer mPlayer;


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

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


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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPlayerView = getActivity().findViewById(R.id.small_player);

        if (getArguments() != null) {
            mVideoURL = getArguments().getString(VIDEO_KEY);
        } else {
            mVideoURL = Source1;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("test", "onStart Fragment");
        if (Util.SDK_INT > 23) {
            initializePlayer();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("test", "onResume Fragment");
//        hideSystemUi();
        if ((Util.SDK_INT <= 23 || mPlayer == null)) {
            initializePlayer();
        }
    }

    @Override
    public void onPause() {
        Log.d("test", "onPause Fragment");
        super.onPause();
        if (Util.SDK_INT <= 23) {
            releasePlayer();
        }
    }

    @Override
    public void onStop() {
        Log.d("test", "onStop Fragment");
        super.onStop();
        if (Util.SDK_INT > 23) {
            releasePlayer();
        }
    }

    @SuppressLint("InlinedApi")
    private void hideSystemUi() {
        mPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    private void initializePlayer() {
        mPlayer = ExoPlayerFactory.newSimpleInstance(
                new DefaultRenderersFactory(getContext()),
                new DefaultTrackSelector(), new DefaultLoadControl());

        mPlayerView.setPlayer(mPlayer);
        mPlayer.seekTo(0);

        Uri uri = Uri.parse(mVideoURL);
        MediaSource mediaSource = buildMediaSource(uri);
        mPlayer.prepare(mediaSource, true, false);
        mPlayer.setPlayWhenReady(true);
    }

    private MediaSource buildMediaSource(Uri uri) {

        // Measures bandwidth during playback. Can be null if not required.
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(),
                Util.getUserAgent(getContext(), "yourApplicationName"), bandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new HlsMediaSource.Factory(dataSourceFactory)
                .createMediaSource(uri);
        // Prepare the player with the source.
        return videoSource;
    }


    private void releasePlayer() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为这只是一个焦点问题。该片段需要聚焦才能播放视频。

此外,如果要在播放视频之前检查焦点,请在Exoplayer片段中稍微放一点。 焦点片段只能播放视频。