Вы можете использовать exo player, как показано ниже. enter code here
public class ExoPlayerVideoHandler {
public static ExoPlayerVideoHandler instance;
static Context context;
private static FrameLayout mFullScreenButton;
public static ImageView mFullScreenIcon;
public static ImageButton mMuteButton;
public static SimpleExoPlayerView mPlayerView;
public static SimpleExoPlayer mPlayer;
static FrameLayout frameLayout;
public static boolean mExoPlayerFullscreen = false;
public static ProgressBar mPb_exoLoading;
private boolean isPlayerPlaying;
public static Dialog mFullScreenDialog;
public static ExoPlayerVideoHandler getInstance() {
if (instance == null) {
instance = new ExoPlayerVideoHandler();
}
return instance;
}
public ExoPlayerVideoHandler(Context context, SimpleExoPlayerView mPlayerView, FrameLayout frameLayout,SimpleExoPlayer mPlayer) {
this.context = context;
this.mPlayerView = mPlayerView;
this.frameLayout = frameLayout;
this.mPlayer=mPlayer;
}
private ExoPlayerVideoHandler() {
}
private static MediaSource buildMediaSource(Uri uri) {
DefaultExtractorsFactory extractorSourceFactory = new DefaultExtractorsFactory();
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ua");
ExtractorMediaSource audioSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorSourceFactory, null, null);
return new ExtractorMediaSource(uri, dataSourceFactory, extractorSourceFactory, null, null);
}
public static void initExoPlayer(String mVideoSource) {
Uri uri = Uri.parse(mVideoSource);
MediaSource mediaSource = buildMediaSource(uri);
mPlayer.prepare(mediaSource);
if (mPlayer.getVolume()> 0) {
mPlayer.setVolume(mPlayer.getVolume());
mMuteButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_volume_up_black_24dp));
}
else {
mPlayer.setVolume(0);
mMuteButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_volume_off_black_24dp));
}
mPlayerView.setControllerShowTimeoutMs(60000);
mPlayer.addListener(new ExoPlayer.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
frameLayout.setVisibility(View.VISIBLE);
if (mPlayerView != null)
if (playbackState == ExoPlayer.STATE_BUFFERING) {
if (mPb_exoLoading != null && mPlayerView != null) {
mPb_exoLoading.setVisibility(View.VISIBLE);
mPlayerView.showController();
}
} else {
if (mPb_exoLoading != null && mPlayerView != null) {
mPb_exoLoading.setVisibility(View.INVISIBLE);
mPlayerView.hideController();
}
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
@Override
public void onPositionDiscontinuity(int reason) {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
@Override
public void onSeekProcessed() {
}
});
boolean haveResumePosition = mResumeWindow != C.INDEX_UNSET;
if (haveResumePosition) {
mPlayer.seekTo(mResumeWindow, mResumePosition);
}
mPlayerView.setPlayer(mPlayer);
mPlayer.setPlayWhenReady(true);
}
public void releaseVideoPlayer() {
if (mPlayer != null) {
mPlayer.release();
}
mPlayer = null;
}
public void goToBackground() {
if (mPlayer != null) {
isPlayerPlaying = mPlayer.getPlayWhenReady();
mPlayer.setPlayWhenReady(false);
}
}
public void goToForeground() {
if (mPlayer != null) {
mPlayer.setPlayWhenReady(isPlayerPlaying);
}
}
public static void initFullscreenDialog() {
mFullScreenDialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
public void onBackPressed() {
if (mExoPlayerFullscreen)
closeFullscreenDialog(frameLayout);
super.onBackPressed();
}
};
}
public static boolean openFullscreenDialog() {
((ViewGroup) mPlayerView.getParent()).removeView(mPlayerView);
mFullScreenDialog.addContentView(mPlayerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mFullScreenIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_fullscreen_skrink));
mExoPlayerFullscreen = true;
mFullScreenDialog.show();
return mExoPlayerFullscreen;
}
public static boolean closeFullscreenDialog(FrameLayout frameLayout) {
((ViewGroup) mPlayerView.getParent()).removeView(mPlayerView);
frameLayout.addView(mPlayerView);
mExoPlayerFullscreen = false;
mFullScreenDialog.dismiss();
mFullScreenIcon.setImageDrawable(ContextCompat.getDrawable(
context, R.drawable.ic_fullscreen_expand));
return mExoPlayerFullscreen;
}
public static boolean initFullscreenButton() {
PlaybackControlView controlView = mPlayerView.findViewById(R.id.exo_controller);
mFullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon);
mPb_exoLoading = controlView.findViewById(R.id.exo_loading);
mMuteButton = controlView.findViewById(R.id.exo_mute_button);
mFullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button);
mMuteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPlayer != null) {
if (mPlayer.getVolume() == 0) {
mPlayer.setVolume(100);
mMuteButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_volume_up_black_24dp));
}
else {
mPlayer.setVolume(0);
mMuteButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_volume_off_black_24dp));
}
}
}
});
mFullScreenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mExoPlayerFullscreen) {
openFullscreenDialog();
mExoPlayerFullscreen = true;
} else {
closeFullscreenDialog(frameLayout);
mExoPlayerFullscreen = false;
}
}
});
return mExoPlayerFullscreen;
}
}
фрагмент или действие, которое вы хотите использовать ниже кода
public static boolean mExoPlayerFullscreen = false;
public static String STATE_RESUME_WINDOW = "resumeWindow";
public static String STATE_RESUME_POSITION = "resumePosition";
public static String STATE_PLAYER_FULLSCREEN = "playerFullscreen";
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_RESUME_WINDOW, mResumeWindow);
outState.putLong(STATE_RESUME_POSITION, mResumePosition);
outState.putBoolean(STATE_PLAYER_FULLSCREEN, mExoPlayerFullscreen);
super.onSaveInstanceState(outState);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onResume() {
super.onResume();
if (detail_video == null) {
detail_video = detailFragment.findViewById(R.id.detail_video_simple);
exoplayer = new ExoPlayerVideoHandler(getContext(), detail_video, frameLayout, simpleExoPlayer);
ExoPlayerVideoHandler.initFullscreenDialog();
mExoPlayerFullscreen = ExoPlayerVideoHandler.initFullscreenButton();
exoplayer.initExoPlayer(videoUrl);
}
if (videoUrl != null && detail_video != null) {
ExoPlayerVideoHandler.getInstance().goToForeground();
}
}
@Override
public void onPause() {
super.onPause();
ExoPlayerVideoHandler.getInstance().goToBackground();
if (mFullScreenDialog != null)
mFullScreenDialog.dismiss();
}