Add your SDK license key
-
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 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()); }
-
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 broadcastprotected 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); }
-
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
andsurfaceDestroyed
@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) { }
- When the SurfaceHolder is created, we start the preview on the Camera instance created in the
preview
method - Since we will show the preview on enter of this fragment on selection in our Tab Activity, add a
onResume
override and callpreview
@Override public void onResume() { super.onResume(); preview(); }
-
Now we will create a delegate for the Button added to the fragment that will invoke
start
andstop
methods. To start, create anonActivityCreated
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(); } }); }
- Add the
onPublishToggle
method that is called in theonClick
override for the Buttonprivate void onPublishToggle() { Button publishButton = (Button) getActivity().findViewById(R.id.publishButton); if(isPublishing) { stop(); } else { start(); } isPublishing = !isPublishing; publishButton.setText(isPublishing ? "stop" : "start"); }
-
The
start
andstop
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(); }
- When it comes time to stop the broadcast
public void stop() { if(stream != null) { stream.stop(); camera.startPreview(); } }
- To manage moving to and from
Subscribe
tab we’ll add some state management with anonPause
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 entering the fragment.
With that, the Publisher tab section is complete to start broadcasting a video stream.