Play Youtube Videos in SUSI.AI Android app

SUSI.AI Android app has many response functionalities ranging from giving simple ANSWER type responses to complex TABLE and MAP type responses. Although, even after all these useful response types there were some missing action types all related to media. SUSI.AI app was not capable of playing any kind of music or video.So, to do this  the largest source of videos in the world was thought and the functionality to play youtube videos in the app was added. Since, the app now has two build flavors corresponding to the FDroid version and PlayStore version respectively it had to be considered that while adding the feature to play youtube videos any proprietary software was not included with the FDroid version. Google provides a youtube API that can be used to play videos inside the app only instead of passing an intent and playing the videos in the youtube app. Steps to integrate youtube api in SUSI.AI The first step was to create an environment variable that stores the API key, so that each developer that wants to test this functionality can just create an API key from the google API console and put it in the build.gradle file in the line below def YOUTUBE_API_KEY = System.getenv('YOUTUBE_API_KEY') ?: '"YOUR_API_KEY"'     In the build.gradle file the buildConfigfield parameter names API_KEY was created so that it can used whenever we need the API_KEY in the code. The buildConfigfield was declared for both the release and debug build types as : release {   minifyEnabled false  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  buildConfigField "String", 'YOUTUBE_API_KEY', YOUTUBE_API_KEY } debug {   buildConfigField "String", 'YOUTUBE_API_KEY', YOUTUBE_API_KEY } The second step is to catch the audio and video action type in the ParseSusiResponseHelper.kt file which was done by adding two constants “video_play” and “audio_play” in the Constant class. These actions were easily caught in the app as : Constant.VIDEOPLAY -> try {   identifier = susiResponse.answers[0].actions[i].identifier } catch (e: Exception) {   Timber.e(e) } Constant.AUDIOPLAY -> try {   identifier = susiResponse.answers[0].actions[i].identifier } catch (e: Exception) {   Timber.e(e) } The third step involves making a ViewHolder class for the audio and video play actions. A simple layout was made for this viewholder which can display the thumbnail of the video and has a play button on the thumbnail, on clicking which plays the youtube video. Also, in the ChatFeedRecyclerAdapter we need to specify the action as Audio play and video play and then load the specific viewholder for the youtube videos whenever the response from the server for this particular action is fetched. The YoutubeViewHolder.java file describes the class that displays the thumbnail of the youtube video whenever a response arrives. public void setPlayerView(ChatMessage model) {   this.model = model;   if (model != null) {       try {           videoId = model.getIdentifier();           String img_url = "http://img.youtube.com/vi/" + videoId + "/0.jpg";           Picasso.with(itemView.getContext()).load(img_url).                   placeholder(R.drawable.ic_play_circle_filled_white_24dp)                   .into(playerView);       } catch (Exception e) {           Timber.e(e);       }   } The above method shows how the thumbnail is set for a particular youtube video. The fourth step is to pass the response from the server in the ChatPresenter to…

Continue ReadingPlay Youtube Videos in SUSI.AI Android app