使用碎片的多个Exoplayer实例

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

我的目的是在一个地方播放几个视频 Activity 同时使用ExoPlayer 2。(我得到了每个视频的hls源)。我成功地播放了一个视频。因此,我决定使实现的播放器内 Fragment 并创造新 Fragment 将每个hls源放入 Activity. 但只有一个 Fragment 顺利播放视频,其他 Fragments看起来像黑色的方块,没有任何内容。如何解决这个问题?

我使用的是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;
        }
    }
android android-fragments exoplayer exoplayer2.x
1个回答
0
投票

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

此外,在你的Exoplayer片段下降一点点,如果在播放视频之前检查焦点.该片段在焦点应该只播放视频。

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