In my mainActivity I have the method: player.setPlayWhenReady (true) which is responsible for getting the player to start playing and continues to run in the background while I'm browsing between my Activity, the problem occurs when I try to return to my mainActivity the player is played again, thus getting 2 audios playing at the same time.
private Handler mainHandler;
private BandwidthMeter bandwidthMeter;
private TrackSelector trackSelector;
private TrackSelection.Factory trackSelectionFactory;
private LoadControl loadControl;
private SimpleExoPlayer player;
private DataSource.Factory dataSourceFactory;
private ExtractorsFactory extractorsFactory;
private MediaSource mediaSource;
private String radioUrl = "http://audio1.cmaudioevideo.com:8237/stream";
private ImageButton pause;
private ImageButton play;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
play = (ImageButton) findViewById(R.id.play) ;
pause = (ImageButton) findViewById(R.id.pause) ;
mainHandler = new Handler();
bandwidthMeter = new DefaultBandwidthMeter();
loadControl = new DefaultLoadControl();
extractorsFactory = new DefaultExtractorsFactory();
trackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(mainHandler,
trackSelectionFactory);
dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "mediaPlayerSample"),
(TransferListener<? super DataSource>) bandwidthMeter);
mediaSource = new ExtractorMediaSource(Uri.parse(radioUrl),
dataSourceFactory,
extractorsFactory,
null,
null);
player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(),
trackSelector,
loadControl);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
Log.v("TEST","playing state : " + player.getPlaybackState());
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setPlayWhenReady(true);
play.setVisibility(View.GONE);
pause.setVisibility(View.VISIBLE);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setPlayWhenReady(false);
play.setVisibility(View.VISIBLE);
pause.setVisibility(View.GONE);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_sobre) {
Intent it_sobre = new Intent(MainActivity.this,SobreActivity.class);
startActivity(it_sobre);}
else if (id == R.id.nav_equipe) {
Intent it_valores = new Intent(MainActivity.this,EquipeActivity.class);
startActivity(it_valores);}
else if (id == R.id.nav_contato) {
Intent it_contato = new Intent(MainActivity.this,ContatoActivity.class);
startActivity(it_contato);}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}