Last active
May 10, 2020 09:32
-
-
Save bapspatil/2613edaac7230dc5ea48091302a3250c to your computer and use it in GitHub Desktop.
Generic code to set up ExoPlayer in a Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** Created by bapspatil | |
*/ | |
public class ExoPlayerDemoFragment extends Fragment { | |
@BindView(R.id.video_exoplayer_view) SimpleExoPlayerView mPlayerView; | |
private SimpleExoPlayer mPlayer; | |
private Unbinder unbinder; | |
public ExoPlayerDemoFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_exoplayer_demo, container, false); | |
unbinder = ButterKnife.bind(this, rootView); | |
getPlayer(); | |
return rootView; | |
} | |
private void getPlayer() { | |
// URL of the video to stream | |
String videoURL = "https://www.youtube.com/watch?v=3tmd-ClpJxA"; | |
// Handler for the video player | |
Handler mainHandler = new Handler(); | |
/* A TrackSelector that selects tracks provided by the MediaSource to be consumed by each of the available Renderers. | |
A TrackSelector is injected when the player is created. */ | |
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); | |
TrackSelection.Factory videoTrackSelectionFactory = | |
new AdaptiveTrackSelection.Factory(bandwidthMeter); | |
TrackSelector trackSelector = | |
new DefaultTrackSelector(videoTrackSelectionFactory); | |
// Create the player with previously created TrackSelector | |
mPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector); | |
// Load the default controller | |
mPlayerView.setUseController(true); | |
mPlayerView.requestFocus(); | |
// Load the SimpleExoPlayerView with the created player | |
mPlayerView.setPlayer(mPlayer); | |
// Measures bandwidth during playback. Can be null if not required. | |
DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter(); | |
// Produces DataSource instances through which media data is loaded. | |
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory( | |
getContext(), | |
Util.getUserAgent(getContext(), "MyAppName"), | |
defaultBandwidthMeter); | |
// Produces Extractor instances for parsing the media data. | |
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); | |
// This is the MediaSource representing the media to be played. | |
MediaSource videoSource = new ExtractorMediaSource( | |
Uri.parse(videoURL), | |
dataSourceFactory, | |
extractorsFactory, | |
null, | |
null); | |
// Prepare the player with the source. | |
mPlayer.prepare(videoSource); | |
// Autoplay the video when the player is ready | |
mPlayer.setPlayWhenReady(true); | |
} | |
@Override | |
public void onDestroyView() { | |
super.onDestroyView(); | |
unbinder.unbind(); | |
// Release the player when it is not needed | |
mPlayer.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment