Skip to content

Instantly share code, notes, and snippets.

@lobothijau
Created January 18, 2018 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobothijau/d0b3652026efd5ca935895ff0bdb9fdc to your computer and use it in GitHub Desktop.
Save lobothijau/d0b3652026efd5ca935895ff0bdb9fdc to your computer and use it in GitHub Desktop.
ExoPlayer
public class MainActivity extends AppCompatActivity {
SimpleExoPlayer simpleExoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
/**
* Dipakai untuk menyiapkan data audio dan memutarnya di ExoPlayer
*/
private void initExoPlayer() {
// Persiapan
DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(
this,
null,
DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF
);
TrackSelector trackSelector = new DefaultTrackSelector();
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(
renderersFactory,
trackSelector
);
// Sebagai identifikasi player kita
String userAgent = Util.getUserAgent(this, "Pemutar Audio Sederhana");
// Menyiapkan MediaSource, jenis data yang dapat diputar oleh ExoPlayer
ExtractorMediaSource mediaSource = new ExtractorMediaSource(
Uri.parse("asset:///detoursting.mp3"), // file audio ada di folder assets
new DefaultDataSourceFactory(this, userAgent),
new DefaultExtractorsFactory(),
null,
null
);
simpleExoPlayer.prepare(mediaSource);
simpleExoPlayer.setPlayWhenReady(true);
}
/**
* Dipakai untuk me-release ExoPlayer apabila sudah tidak dipergunakan
* agar penggunaan memori lebih efisien.
*/
private void releaseExoPlayer() {
simpleExoPlayer.release();
}
@Override
protected void onStart() {
super.onStart();
// Di Android Nougat ke atas, lalukan inisialisasi di onStart()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
initExoPlayer();
}
}
@Override
protected void onResume() {
super.onResume();
// Apabila menggunakan Android Marshmallow ke bawah lakukan inisalisasi di onResume()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
initExoPlayer();
}
}
@Override
protected void onPause() {
super.onPause();
// Apabila menggunakan Android Marshmallow ke bawah lakukan release di onPause()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
releaseExoPlayer();
}
}
@Override
protected void onStop() {
super.onStop();
// Di Android Nougat ke atas, lalukan inisialisasi di onStart()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
releaseExoPlayer();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment