private void configurePopupWithTracks(
     PopupMenu popup,
     final PopupMenu.OnMenuItemClickListener customActionClickListener,
     final int trackType) {
   if (player == null) {
     return;
   }
   int trackCount = player.getTrackCount(trackType);
   if (trackCount == 0) {
     return;
   }
   popup.setOnMenuItemClickListener(
       new PopupMenu.OnMenuItemClickListener() {
         @Override
         public boolean onMenuItemClick(MenuItem item) {
           return (customActionClickListener != null
                   && customActionClickListener.onMenuItemClick(item))
               || onTrackItemClick(item, trackType);
         }
       });
   Menu menu = popup.getMenu();
   // ID_OFFSET ensures we avoid clashing with Menu.NONE (which equals 0)
   menu.add(MENU_GROUP_TRACKS, DemoPlayer.DISABLED_TRACK + ID_OFFSET, Menu.NONE, R.string.off);
   if (trackCount == 1 && TextUtils.isEmpty(player.getTrackName(trackType, 0))) {
     menu.add(MENU_GROUP_TRACKS, DemoPlayer.PRIMARY_TRACK + ID_OFFSET, Menu.NONE, R.string.on);
   } else {
     for (int i = 0; i < trackCount; i++) {
       menu.add(MENU_GROUP_TRACKS, i + ID_OFFSET, Menu.NONE, player.getTrackName(trackType, i));
     }
   }
   menu.setGroupCheckable(MENU_GROUP_TRACKS, true, true);
   menu.findItem(player.getSelectedTrackIndex(trackType) + ID_OFFSET).setChecked(true);
 }
 private void releasePlayer() {
   if (player != null) {
     debugViewHelper.stop();
     debugViewHelper = null;
     playerPosition = player.getCurrentPosition();
     player.release();
     player = null;
     eventLogger.endSession();
     eventLogger = null;
   }
 }
 private boolean onTrackItemClick(MenuItem item, int type) {
   if (player == null || item.getGroupId() != MENU_GROUP_TRACKS) {
     return false;
   }
   player.selectTrack(type, item.getItemId() - ID_OFFSET);
   return true;
 }
 @Override
 public void onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities) {
   boolean audioCapabilitiesChanged = !audioCapabilities.equals(this.audioCapabilities);
   if (player == null || audioCapabilitiesChanged) {
     this.audioCapabilities = audioCapabilities;
     releasePlayer();
     preparePlayer();
   } else if (player != null) {
     player.setBackgrounded(false);
   }
 }
 @Override
 public void onPause() {
   super.onPause();
   if (!enableBackgroundAudio) {
     releasePlayer();
   } else {
     player.setBackgrounded(true);
   }
   audioCapabilitiesReceiver.unregister();
   shutterView.setVisibility(View.VISIBLE);
 }
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
   if (player != null) {
     player.blockingClearSurface();
   }
 }
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
   if (player != null) {
     player.setSurface(holder.getSurface());
   }
 }
 private boolean haveTracks(int type) {
   return player != null && player.getTrackCount(type) > 0;
 }
 private void preparePlayer() {
   if (player == null) {
     player = new DemoPlayer(getRendererBuilder());
     player.addListener(this);
     player.setCaptionListener(this);
     player.setMetadataListener(this);
     player.seekTo(playerPosition);
     playerNeedsPrepare = true;
     mediaController.setMediaPlayer(player.getPlayerControl());
     mediaController.setEnabled(true);
     eventLogger = new EventLogger();
     eventLogger.startSession();
     player.addListener(eventLogger);
     player.setInfoListener(eventLogger);
     player.setInternalErrorListener(eventLogger);
     debugViewHelper = new DebugTextViewHelper(player, debugTextView);
     debugViewHelper.start();
   }
   if (playerNeedsPrepare) {
     player.prepare();
     playerNeedsPrepare = false;
     updateButtonVisibilities();
   }
   player.setSurface(surfaceView.getHolder().getSurface());
   player.setPlayWhenReady(true);
 }