Last active
March 6, 2017 18:51
-
-
Save Evin1-/76cea0e5db261af96bfc5e6f900693f9 to your computer and use it in GitHub Desktop.
Minimal ExoPlayer
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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.example.startingtemplate.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" /> | |
<com.google.android.exoplayer2.ui.SimpleExoPlayerView | |
android:id="@+id/a_main_exoplayer" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</com.google.android.exoplayer2.ui.SimpleExoPlayerView> | |
</LinearLayout> |
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
<uses-permission android:name="android.permission.INTERNET" /> |
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
dependencies { | |
// ... | |
compile 'com.google.android.exoplayer:exoplayer:r2.2.0' | |
} |
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
public class MainActivity extends AppCompatActivity { | |
private static final String VIDEO_URI = "http://techslides.com/demos/sample-videos/small.mp4"; | |
private SimpleExoPlayer player; | |
private SimpleExoPlayerView simpleExoPlayerView; | |
private static final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.a_main_exoplayer); | |
createPlayer(); | |
attachPlayerView(); | |
preparePlayer(); | |
} | |
public void createPlayer() { | |
TrackSelection.Factory videoTrackSelectionFactory | |
= new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); | |
player = ExoPlayerFactory.newSimpleInstance(this, | |
new DefaultTrackSelector(videoTrackSelectionFactory), new DefaultLoadControl()); | |
} | |
public void attachPlayerView() { | |
simpleExoPlayerView.setPlayer(player); | |
} | |
public void preparePlayer() { | |
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, bandwidthMeter, | |
buildHttpDataSourceFactory(bandwidthMeter)); | |
MediaSource videoSource = new ExtractorMediaSource(Uri.parse(VIDEO_URI), | |
dataSourceFactory, new DefaultExtractorsFactory(), new Handler(), null); | |
player.prepare(videoSource); | |
} | |
private HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) { | |
return new DefaultHttpDataSourceFactory(Util.getUserAgent(this, | |
getString(R.string.app_name)), bandwidthMeter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment