Last active
March 9, 2017 11:12
-
-
Save niusounds/28307faab4e5bee0ee41c076fcd5ca08 to your computer and use it in GitHub Desktop.
Minimum ExoPlayer video playing
This file contains hidden or 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
import android.app.Activity; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.annotation.Nullable; | |
import android.view.SurfaceView; | |
import com.google.android.exoplayer2.C; | |
import com.google.android.exoplayer2.DefaultLoadControl; | |
import com.google.android.exoplayer2.ExoPlayerFactory; | |
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.trackselection.DefaultTrackSelector; | |
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; | |
import com.google.android.exoplayer2.util.Util; | |
import org.androidannotations.annotations.AfterViews; | |
import org.androidannotations.annotations.EActivity; | |
import org.androidannotations.annotations.ViewById; | |
import java.io.IOException; | |
@EActivity(R.layout.activity_main) | |
public class MainActivity extends Activity { | |
private SimpleExoPlayer player; | |
@ViewById | |
SurfaceView surfaceView; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(), new DefaultLoadControl()); | |
player.setAudioStreamType(C.STREAM_TYPE_MUSIC); | |
player.prepare(new ExtractorMediaSource(Uri.parse(VIDEO_URL), new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Test")), new DefaultExtractorsFactory(), new Handler(), new ExtractorMediaSource.EventListener() { | |
@Override | |
public void onLoadError(IOException error) { | |
error.printStackTrace(); | |
} | |
})); | |
} | |
@AfterViews | |
void init() { | |
player.setVideoSurfaceView(surfaceView); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
player.setPlayWhenReady(true); | |
} | |
@Override | |
protected void onPause() { | |
player.setPlayWhenReady(false); | |
super.onPause(); | |
} | |
@Override | |
protected void onDestroy() { | |
player.release(); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment