Red5 Documentation

Android SDK Subscriber

  1. Right-click on the app/src/main folder in the Project Explorer and select New > Fragment > Fragment (Blank)
  2. Name the fragment SubscribeFragment which will update the layout name to fragment_subscribe
  3. Click Finish
  4. With the SubscribeFragment file open, remove most of the automated cruft so we have the following

    public class SubscribeFragment extends Fragment {
    
      public static SubscribeFragment newInstance() {
        SubscribeFragment fragment = new PublishFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
      }
    
      public SubscribeFragment() {
        // Required empty public constructor
      }
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      }
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_subscribe, container, false);
        return v;
      }
    
    }
  5. Update the getItem method of the SectionsPagerAdapter in StreamingActivity to include a return of SuscribeFragment on selection of the second tab
     @Override
    public Fragment getItem(int position) {
      switch(position) {
        case 0:
          return PublishFragment.newInstance();
        case 1:
          return SubscribeFragment.newInstance();
      }
      // getItem is called to instantiate the fragment for the given page.
      // Return a PlaceholderFragment (defined as a static inner class below).
      return PlaceholderFragment.newInstance(position + 1);
    }
  6. Open the fragment_subscribe.xml file, change the Layout to RelativeLayout and add a SurfaceView and Button control

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.infrared5.streaming.sample.red5prostreamexample.SubscribeFragment">
    
        <com.red5pro.streaming.view.R5VideoView
            android:id="@+id/subscribeView"
            android:layout_centerHorizontal="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
        <Button
            android:id="@+id/subscribeButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="start"
            android:layout_marginBottom="0dp"
            android:layout_alignParentBottom="true"
            />
    
    </RelativeLayout>
    
    </RelativeLayout>
  7. Define the R5Configuration in the onCreate override that will hold the attributes associated with the stream to consume.

    • 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());
    }
  8. 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 SubcribeFragment

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
    
      Button publishButton = (Button) getActivity().findViewById(R.id.subscribeButton);
      publishButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          onSubscribeToggle();
        }
      });
    }
  9. Add the onSubscribeToggle method that is called in the onClick override for the Button
    private void onSubscribeToggle() {
      Button subscribeButton = (Button) getActivity().findViewById(R.id.subscribeButton);
      if(isSubscribing) {
        stop();
      }
      else {
        start();
      }
      isSubscribing = !isSubscribing;
      subscribeButton.setText(isSubscribing ? "stop" : "start");
    }
  10. The start and stop methods are responsible for starting and stopping the subsciber session, respectively

    public void start() {
      R5VideoView videoView = (R5VideoView) getActivity().findViewById(R.id.subscribeView);
    
      stream = new R5Stream(new R5Connection(configuration));
      videoView.attachStream(stream);
      stream.play("red5prostream");
    }
    
    public void stop() {
      if(stream != null) {
        stream.stop();
      }
    }
  11. To manage moving to and from Publish tab we’ll add some state management with an onPause override
    @Override
    public void onPause() {
      super.onPause();
      if(isSubscribing) {
        onSubscribeToggle();
      }
    }

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