-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tan Vu
committed
Mar 29, 2017
1 parent
0685441
commit d0cac62
Showing
4 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,93 @@ | ||
package com.mrwii.exoplayer; | ||
|
||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
import com.google.android.exoplayer2.DefaultLoadControl; | ||
import com.google.android.exoplayer2.ExoPlayerFactory; | ||
import com.google.android.exoplayer2.LoadControl; | ||
import com.google.android.exoplayer2.SimpleExoPlayer; | ||
//import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; | ||
//import com.google.android.exoplayer2.source.ExtractorMediaSource; | ||
import com.google.android.exoplayer2.source.MediaSource; | ||
import com.google.android.exoplayer2.source.hls.HlsMediaSource; | ||
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; | ||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; | ||
import com.google.android.exoplayer2.trackselection.TrackSelection; | ||
import com.google.android.exoplayer2.trackselection.TrackSelector; | ||
import com.google.android.exoplayer2.ui.PlaybackControlView; | ||
import com.google.android.exoplayer2.ui.SimpleExoPlayerView; | ||
import com.google.android.exoplayer2.upstream.DataSource; | ||
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; | ||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; | ||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; | ||
import com.google.android.exoplayer2.util.Util; | ||
|
||
public class MainActivity extends AppCompatActivity implements PlaybackControlView.VisibilityListener { | ||
|
||
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter(); | ||
|
||
private SimpleExoPlayerView simpleExoPlayerView; | ||
private SimpleExoPlayer player; | ||
|
||
private TrackSelector trackSelector; | ||
private LoadControl loadControl; | ||
private DataSource.Factory mediaDataSourceFactory; | ||
|
||
private boolean shouldAutoPlay; | ||
private MediaSource mediaSource; | ||
private Uri uri; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
shouldAutoPlay = true; | ||
|
||
// 1. Create a default TrackSelector | ||
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); | ||
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); | ||
|
||
String userAgent = Util.getUserAgent(this, "ExoPlayer"); | ||
DataSource.Factory baseDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, BANDWIDTH_METER); | ||
mediaDataSourceFactory = new DefaultDataSourceFactory(this, BANDWIDTH_METER, baseDataSourceFactory); | ||
|
||
// 2. Create a default LoadControl | ||
loadControl = new DefaultLoadControl(); | ||
|
||
// 3. Create the player | ||
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); | ||
|
||
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view); | ||
simpleExoPlayerView.setControllerVisibilityListener(this); | ||
simpleExoPlayerView.requestFocus(); | ||
|
||
initializePlayer(); | ||
} | ||
|
||
private void initializePlayer() { | ||
Intent intent = getIntent(); | ||
boolean needNewPlayer = player == null; | ||
if (needNewPlayer) { | ||
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); | ||
simpleExoPlayerView.setPlayer(player); | ||
player.setPlayWhenReady(shouldAutoPlay); | ||
|
||
// uri = Uri.parse("http://vjs.zencdn.net/v/oceans.mp4"); | ||
// mediaSource = new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(), null, null); | ||
|
||
uri = Uri.parse("http://devimages.apple.com/samplecode/adDemo/ad.m3u8"); | ||
// This is the MediaSource representing the media to be played. | ||
mediaSource = new HlsMediaSource(uri, mediaDataSourceFactory, null, null); | ||
|
||
player.prepare(mediaSource, true, true); | ||
} | ||
} | ||
|
||
@Override | ||
public void onVisibilityChange(int visibility) { | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="com.mrwii.exoplayer.MainActivity"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
<com.google.android.exoplayer2.ui.SimpleExoPlayerView | ||
android:id="@+id/player_view" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
|
||
</android.support.constraint.ConstraintLayout> |