/

Add your SDK license key


  1. Define the R5Configuration in the onCreate override that will hold the attributes associated with the stream to broadcast.

    • Change the host attribute to point to the location of your Red5 Pro server.
    • Change the SDK license key to the one provided from your <a href="https://account.red5pro.com" target="blank">Red5 Pro Account._
    public R5Configuration configuration;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      configuration = new R5Configuration(R5StreamProtocol.RTSP, "localhost",  8554, "live", 1.0f);
      configuration.setLicenseKey("YOUR-SDK-LICENSE-KEY");
      configuration.setBundleID(getActivity().getPackageName());
    }
  2. Declare some class-level variables related to Camera and Stream and create a new method named preview that will be responsible for setting up a stream to be displayed and broadcast

    protected Camera camera;
    protected boolean isPublishing = false;
    protected R5Stream stream;
    
    private void preview() {
      camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
      SurfaceView surface = (SurfaceView) getActivity().findViewById(R.id.surfaceView);
      surface.getHolder().addCallback(this);
    }
  3. Because we have added the PublishFragment class as a callback for the SurfaceHolder, set the class as an implementation of SurfaceHolder.Callback and add the delegates surfaceCreated, surfaceChanged and surfaceDestroyed

    @Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
          camera.setPreviewDisplay(surfaceHolder);
          camera.startPreview();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
    
      @Override
      public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    
      }
    
      @Override
      public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    
      }
  4. When the SurfaceHolder is created, we start the preview on the Camera instance created in the preview method
  5. Since we will show the preview on enter of this fragment on selection in our Tab Activity, add a onResume override and call preview

    @Override
    public void onResume() {
      super.onResume();
      preview();
    }
  6. Now we will create a delegate for the Button added to the fragment that will invoke start and stop methods. To start, create an onActivityCreated override in PublishFragment

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
    
      Button publishButton = (Button) getActivity().findViewById(R.id.publishButton);
      publishButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          onPublishToggle();
        }
      });
    }
  7. Add the onPublishToggle method that is called in the onClick override for the Button

    private void onPublishToggle() {
      Button publishButton = (Button) getActivity().findViewById(R.id.publishButton);
      if(isPublishing) {
        stop();
      }
      else {
        start();
      }
      isPublishing = !isPublishing;
      publishButton.setText(isPublishing ? "stop" : "start");
    }
  8. The start and stop methods are responsible for starting and stopping the broadcasting session, respectively. Creating a broadcast session is more involved than stopping one, so we will tackle that first.

    public void start() {
      camera.stopPreview();
    
      stream = new R5Stream(new R5Connection(configuration));
      stream.setView((SurfaceView) getActivity().findViewById(R.id.surfaceView));
    
      R5Camera r5Camera = new R5Camera(camera, 320, 240);
      R5Microphone r5Microphone = new R5Microphone();
    
      stream.attachCamera(r5Camera);
      stream.attachMic(r5Microphone);
    
      stream.publish("red5prostream", RecordType.Live);
      camera.startPreview();
    }
  9. When it comes time to stop the broadcast

     public void stop() {
      if(stream != null) {
        stream.stop();
        camera.startPreview();
      }
    }
  10. To manage moving to and from Subscribe tab we'll add some state management with an onPause override

    @Override
    public void onPause() {
      super.onPause();
      if(isPublishing) {
        onPublishToggle();
      }
    }

If you remember from a previous step, we defined an onResume override that will start the Camera preview on enter of the fragment.

With that, the Publisher tab section is complete to start broadcasting a video stream.