Make a method on onCreate run once

Asked

Viewed 176 times

0

In my mainActivity I have the method: player.setPlayWhenReady(true) which is responsible for making the player start playing and continues to run in the background while I am browsing between my Activity, the problem occurs when I try to return to my mainActivity the player runs 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();
}

}

1 answer

1

You can do an if to see if the song is playing, example:

if(player.isPlaying(){
     //Ja ta tocando então não faz nada
}else{
     //inicia o player e toca a música
}

The method isPlaying() is internal to Simpleexoplayer

  • He did not recognize isPlaying() as a method, I located isLoading(), but I believe it would not be the same.

  • The error persists

  • This can never work. If the onCreate() was called because a new instance of Activity was created. Being a new instance the variable player, or any other, no longer references the value assigned in the previous instance,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.