title: Leaking activity context when implementing exoplayer in a separate class
body: <p>I use the exoplayer 3 times in my application so I thought it would be better to implement it in its own class and just use it 3 times. </p>
  
  <p>In doing this I introduced a memory leak so it is leaking the activity and causing the app to crash.</p>
  
  <p>I am not really sure why this would be happening because I haven't closed the activity. I guess it is because there is some change in the activity or the view.</p>
  
  <p>If you could tell me what is wrong with my implementation to cause a memory leak it would be greatly appreciated.</p>
  
  <p>This is my exoplayer control class: </p>
  
  <pre><code>    public class ExoPlayerControls {
  
          Context context;
          SimpleExoPlayerView simpleExoplayerView;
          SimpleExoPlayer player;
          boolean preview;
  
          public ExoPlayerControls(Context context, SimpleExoPlayerView simpleExoplayerView, SimpleExoPlayer player, Boolean preview) {
              this.context = context;
              this.simpleExoplayerView = simpleExoplayerView;
              this.player = player;
              this.preview = preview;
  
          }
  
          public boolean setUPExoPlayer (String alarmMediaLocation){
              try {
  
                  if (player != null){
                      player.setPlayWhenReady(false);
                      //URI location of audio
                      Uri mediaURI = Uri.parse(alarmMediaLocation);
  
                      // Data source Factory
                      DefaultHttpDataSourceFactory datasourceFactory = new DefaultHttpDataSourceFactory("alarm video");
  
                      // Extractor Factory
                      ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
  
                      // Media source
                      MediaSource mediasource = new ExtractorMediaSource(mediaURI, datasourceFactory, extractorsFactory, null, null);
  
                      // Prepare the exoplayer
                      player.prepare(mediasource);
  
                      player.setPlayWhenReady(false);
  
  
                  } else {
  
                      // Create a bandwith meter
                      BandwidthMeter bandwidthmeter = new DefaultBandwidthMeter();
  
                      // Create a track selector
                      TrackSelector trackselector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthmeter));
  
                      // Create the player
                      player = ExoPlayerFactory.newSimpleInstance(context, trackselector);
  
                      //URI location of audio
                      Uri mediaURI = Uri.parse(alarmMediaLocation);
  
                      // Data source Factory
                      DefaultHttpDataSourceFactory datasourceFactory = new DefaultHttpDataSourceFactory("alarm video");
  
                      // Extractor Factory
                      ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
  
                      // Media source
                      MediaSource mediasource = new ExtractorMediaSource(mediaURI, datasourceFactory, extractorsFactory, null, null);
  
                      // Bind the player to the view.
                      simpleExoplayerView.setPlayer(player);
  
                      // Prepare the exoplayer
                      player.prepare(mediasource);
  
                      player.setPlayWhenReady(false);
                  }
  
  
              } catch (Error e){
                  Toast.makeText(context, "Exoplayer error" + e, Toast.LENGTH_SHORT).show();
              }
  
              return  true;
          }
  
          public void playAlarmMedia(String alarmMediaLocation){
  
              try {
  
                  boolean exoPlayerSet = setUPExoPlayer(alarmMediaLocation);
                  if (exoPlayerSet){
                      // Tell exoplayer to start playing immediately
                      player.setPlayWhenReady(true);
                  }
  
              } catch (Error e){
                  Toast.makeText(context, "Exoplayer error" + e, Toast.LENGTH_SHORT).show();
              }
          }
  }
  </code></pre>
  
  <p>And I implement it in my activity like this:</p>
  
  <pre><code>  // Initialise exoplayer controls
          exoPlayerControls = new ExoPlayerControls(this, simpleExoplayerView, player, preview);
          String alarmMediaLocation = downloadedAlarmToPlay.getAlarmMediaLocation();
          exoPlayerControls.setUPExoPlayer(alarmMediaLocation);
  </code></pre>