protected void setupBroadcaster() {
   // By making the recorder static we can allow
   // recording to continue beyond this fragment's
   // lifecycle! That means the user can minimize the app
   // or even turn off the screen without interrupting the recording!
   // If you don't want this behavior, call stopRecording
   // on your Fragment/Activity's onStop()
   if (getActivity().getResources().getConfiguration().orientation
       == Configuration.ORIENTATION_PORTRAIT) {
     if (mBroadcaster == null) {
       if (VERBOSE)
         Log.i(
             TAG,
             "Setting up Broadcaster for output "
                 + Kickflip.getSessionConfig().getOutputPath()
                 + " client key: "
                 + Kickflip.getApiKey()
                 + " secret: "
                 + Kickflip.getApiSecret());
       // TODO: Don't start recording until stream start response, so we can determine stream
       // type...
       Context context = getActivity().getApplicationContext();
       mBroadcaster =
           new Broadcaster(
               context,
               Kickflip.getSessionConfig(),
               Kickflip.getApiKey(),
               Kickflip.getApiSecret());
       mBroadcaster.getEventBus().register(this);
       mBroadcaster.setBroadcastListener(Kickflip.getBroadcastListener());
       Kickflip.clearSessionConfig();
     }
   }
 }
 @Override
 public void onClick(View v) {
   if (mBroadcaster.isRecording()) {
     showEndBroadcastConfirmationDialog();
   } else {
     setBannerToBufferingState();
     mBroadcaster.startRecording();
     stopMonitoringOrientation();
     //                v.setBackgroundResource(R.drawable.red_dot_stop);
   }
 }
  @Override
  public void onResume() {
    super.onResume();

    MainActivity.userBusyNow = true;

    if (mBroadcaster != null) {
      mBroadcaster.onHostActivityResumed();

      if (!mBroadcaster.isRecording()) {
        setBannerToBufferingState();
        mBroadcaster.startRecording();
      }
    }
    //        startMonitoringOrientation();
  }
  @Override
  public void onPause() {
    super.onPause();

    MainActivity.userBusyNow = false;

    if (mBroadcaster != null) mBroadcaster.onHostActivityPaused();
    stopMonitoringOrientation();
  }
 public static BroadcastFragment getInstance() {
   if (mFragment == null) {
     // We haven't yet created a BroadcastFragment instance
     mFragment = recreateBroadcastFragment();
   } else if (mBroadcaster != null && !mBroadcaster.isRecording()) {
     // We have a leftover BroadcastFragment but it is not recording
     // Treat it as finished, and recreate
     mFragment = recreateBroadcastFragment();
   } else {
     Log.i(TAG, "Recycling BroadcastFragment");
   }
   return mFragment;
 }
 @Override
 public void onSensorChanged(SensorEvent event) {
   if (getActivity() != null && getActivity().findViewById(R.id.rotateDeviceHint) != null) {
     // Log.i(TAG, "Sensor " + event.values[1]);
     if (event.values[1] > 10 || event.values[1] < -10) {
       // Sensor noise. Ignore.
     } else if (event.values[1] < 5.5 && event.values[1] > -5.5) {
       // Landscape
       if (orientation != 1 && readingConfirmed(1)) {
         if (mBroadcaster.getSessionConfig().isConvertingVerticalVideo()) {
           if (event.values[0] > 0) {
             mBroadcaster.signalVerticalVideo(FullFrameRect.SCREEN_ROTATION.LANDSCAPE);
           } else {
             mBroadcaster.signalVerticalVideo(
                 FullFrameRect.SCREEN_ROTATION.UPSIDEDOWN_LANDSCAPE);
           }
         } else {
           getActivity().findViewById(R.id.rotateDeviceHint).setVisibility(View.GONE);
         }
         orientation = 1;
       }
     } else if (event.values[1] > 7.5 || event.values[1] < -7.5) {
       // Portrait
       if (orientation != 0 && readingConfirmed(0)) {
         if (mBroadcaster.getSessionConfig().isConvertingVerticalVideo()) {
           if (event.values[1] > 0) {
             mBroadcaster.signalVerticalVideo(FullFrameRect.SCREEN_ROTATION.VERTICAL);
           } else {
             mBroadcaster.signalVerticalVideo(
                 FullFrameRect.SCREEN_ROTATION.UPSIDEDOWN_VERTICAL);
           }
         } else {
           getActivity().findViewById(R.id.rotateDeviceHint).setVisibility(View.VISIBLE);
         }
         orientation = 0;
       }
     }
   }
 }
 @Override
 public void doubleTapDetected() {
   if (mBroadcaster != null) {
     mBroadcaster.requestOtherCamera();
   }
 }
 /**
  * Force this fragment to stop broadcasting. Useful if your application wants to stop broadcasting
  * when a user leaves the Activity hosting this fragment
  */
 public void stopBroadcasting() {
   if (mBroadcaster.isRecording()) {
     mBroadcaster.stopRecording();
     mBroadcaster.release();
   }
 }
 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   if (((String) parent.getTag()).compareTo("filter") == 0) {
     mBroadcaster.applyFilter(position);
   }
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (VERBOSE) Log.i(TAG, "onCreateView");

    View root;
    if (mBroadcaster != null
        && getActivity().getResources().getConfiguration().orientation
            == Configuration.ORIENTATION_PORTRAIT) {
      root = inflater.inflate(R.layout.fragment_broadcast, container, false);
      initializeCommentList(root);

      CustomFrameLayout fragmentBroadcastMainLayout =
          (CustomFrameLayout) root.findViewById(R.id.fragmentBroadcastMainLayout);
      fragmentBroadcastMainLayout.setDoubleTapCallback(this);

      mCameraView = (GLCameraEncoderView) root.findViewById(R.id.cameraPreview);
      mCameraView.setKeepScreenOn(true);
      mCameraView.setBackgroundResource(R.color.transparentBlue84);

      preparingBroadcastTextView = (TextView) root.findViewById(R.id.preparingBroadcastTextView);
      mBroadcaster.setPreviewDisplay(mCameraView);

      this.mediaPlayerFooterLayout = root.findViewById(R.id.mediaPlayerFooterLayout);
      this.mediaPlayerFooterLayout.setVisibility(View.INVISIBLE);
      this.mediaPlayerHeaderLayout = root.findViewById(R.id.mediaPlayerHeaderLayout);
      this.mediaPlayerHeaderLayout.setVisibility(View.INVISIBLE);
      this.mediaPlayerLiveTextView = root.findViewById(R.id.mediaPlayerLiveTextView);

      this.mediaPlayerHeaderImageView =
          (ImageView) root.findViewById(R.id.mediaPlayerHeaderImageView);
      this.mediaPlayerUsernameTextView =
          (TextView) root.findViewById(R.id.mediaPlayerUsernameTextView);
      this.mediaPlayerLocationTextView =
          (TextView) root.findViewById(R.id.mediaPlayerLocationTextView);
      this.mediaPlayerWatchersCountTextView =
          (TextView) root.findViewById(R.id.mediaPlayerWatchersCountTextView);
      this.mediaPlayerStreamTitle = (TextView) root.findViewById(R.id.mediaPlayerStreamTitle);
      this.bandwithTooLowLayout = root.findViewById(R.id.bandwithTooLowLayout);
      View mediaPlayerAddCommentEditText = root.findViewById(R.id.mediaPlayerAddCommentEditText);
      mediaPlayerAddCommentEditText.setVisibility(View.GONE);

      //            this.broadcastingTitleEditText =
      // root.findViewById(R.id.broadcastingTitleEditText);
      //            recordButton.setOnClickListener(mRecordButtonClickListener);
      View closeViewButton = root.findViewById(R.id.mediaPlayerCloseViewButton);
      closeViewButton.setOnClickListener(mRecordButtonClickListener);

      //            mLiveBanner.setOnClickListener(mShareButtonClickListener);

      /*            if (mBroadcaster.isLive()) {
                      setGuiToReadyState();
                      mLiveBanner.setVisibility(View.VISIBLE);
                  }
      */
      if (mBroadcaster.isRecording()) {
        //                recordButton.setBackgroundResource(R.drawable.red_dot_stop);
        if (!mBroadcaster.isLive()) {
          setBannerToBufferingState();
          //                    mLiveBanner.setVisibility(View.VISIBLE);
        } else {
          setGuiToReadyState();
        }
      } else {
        handleRecordingStopped();
      }
      setupFilterSpinner(root);
      setupCameraFlipper(root);
    } else root = new View(container.getContext());
    return root;
  }
 @Override
 public void onDestroy() {
   super.onDestroy();
   if (mBroadcaster != null && !mBroadcaster.isRecording()) mBroadcaster.release();
 }