Skip to content

Commit

Permalink
feat: Play videos in the app fossasia#2109
Browse files Browse the repository at this point in the history
Add a fragment which plays YouTube videos in the app
YoutubeFragment.kt: A the fragment to play Youtube Videos
FullscreenListener.kt: Listens for full screen event
fragment_youtube.xml: Fragment layout

REQUIRED: Give YouTube API key for proper functioning
  • Loading branch information
shubhamkumar1739 committed Feb 9, 2020
1 parent 388d20b commit 1ebc893
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import android.widget.Toast
import io.realm.RealmResults
import java.util.Locale
import kotlinx.android.synthetic.main.activity_chat.*
import kotlinx.android.synthetic.main.fragment_youtube.*
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.adapters.recycleradapters.ChatFeedRecyclerAdapter
import org.fossasia.susi.ai.chat.contract.IChatPresenter
Expand Down Expand Up @@ -90,6 +91,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
}
}

@SuppressLint("RestrictedApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
Expand Down Expand Up @@ -364,6 +366,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
}

// Take user's speech as input and send the message
@SuppressLint("RestrictedApi")
override fun promptSpeechInput() {
if (recordingThread != null) {
chatPresenter.stopHotwordDetection()
Expand All @@ -379,6 +382,7 @@ class ChatActivity : AppCompatActivity(), IChatView {
}

// Replies user with Speech
@SuppressLint("RestrictedApi")
override fun voiceReply(reply: String, language: String) {
searchChat.visibility = View.VISIBLE
fabsetting.visibility = View.VISIBLE
Expand Down Expand Up @@ -633,7 +637,11 @@ class ChatActivity : AppCompatActivity(), IChatView {

override fun playVideo(videoId: String) {
Timber.d(videoId)
youtubeVid.playYoutubeVid(videoId)
var bundle = Bundle()
bundle.putString("video",videoId)
var youtubeFragment = YoutubeFragment.newInstance()
youtubeFragment.arguments = bundle
supportFragmentManager.beginTransaction().add(R.id.coordinator_layout,youtubeFragment,"youtube").commit()
}

companion object {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/org/fossasia/susi/ai/chat/ChatPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
var id: Long = 0
var identifier: String = ""
var tableItem: TableItem? = null
private val youtubeVid: IYoutubeVid = YoutubeVid(context)
private var textToSpeech: TextToSpeech? = null
private val context: Context = context
private lateinit var handler: Handler
Expand Down Expand Up @@ -503,7 +502,7 @@ class ChatPresenter(context: Context, private val chatView: IChatView?) :
} else if (parseSusiHelper.actionType == Constant.VIDEOPLAY || parseSusiHelper.actionType == Constant.AUDIOPLAY) {
// Play youtube video
identifier = parseSusiHelper.identifier
youtubeVid.playYoutubeVid(identifier)
chatView?.playVideo(identifier)
} else if (parseSusiHelper.actionType == Constant.ANSWER && (PrefManager.checkSpeechOutputPref() && check || PrefManager.checkSpeechAlwaysPref())) {
setMessage = parseSusiHelper.answer
try {
Expand Down
77 changes: 77 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/chat/YoutubeFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.fossasia.susi.ai.chat

import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerSupportFragment

import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.listeners.FullscreenListener

class YoutubeFragment : Fragment() {
private var API_KEY: String? = null
private var m_RootView: View? = null
private var m_StopButton: View? = null
var YPlayer: YouTubePlayer? = null
private var VIDEO:String = ""

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var rootView = inflater.inflate(R.layout.fragment_youtube, container, false)
m_RootView = rootView
m_StopButton = m_RootView?.findViewById(R.id.youtube_stop_btn)
var bundle = activity?.packageManager?.getApplicationInfo(activity?.applicationContext?.packageName,PackageManager.GET_META_DATA)?.metaData
API_KEY = bundle?.getString("com.google.android.youtube.API_KEY")
if(arguments!=null){
VIDEO = arguments!!.getString("video")
}
m_StopButton?.setOnClickListener({
YPlayer?.release()
activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
})
initYoutube()
return rootView
}

private fun initYoutube() {
val youTubePlayerFragment: YouTubePlayerSupportFragment =
YouTubePlayerSupportFragment.newInstance()
val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
transaction.add(R.id.youtube_fragment, youTubePlayerFragment as Fragment)
transaction.commit()

youTubePlayerFragment.initialize(API_KEY, object : YouTubePlayer.OnInitializedListener {

override fun onInitializationSuccess(
provider: YouTubePlayer.Provider?,
player: YouTubePlayer,
wasRestored: Boolean
) {
if (!wasRestored) {
YPlayer = player
YPlayer?.loadVideo(VIDEO)
YPlayer?.setOnFullscreenListener(FullscreenListener(activity,this@YoutubeFragment))
YPlayer?.play()
}
}

override fun onInitializationFailure(
arg0: YouTubePlayer.Provider?,
arg1: YouTubeInitializationResult?
) {
}
})
}

companion object {
fun newInstance() = YoutubeFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.widget.ImageView
import com.squareup.picasso.Picasso
import kotterknife.bindView
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.ChatActivity
import org.fossasia.susi.ai.chat.YoutubeVid
import org.fossasia.susi.ai.data.model.ChatMessage
import timber.log.Timber
Expand Down Expand Up @@ -43,6 +44,13 @@ class YoutubeVideoViewHolder(
}

val youtubeVid = YoutubeVid(itemView.context)
playButton.setOnClickListener { videoId?.let { id -> youtubeVid.playYoutubeVid(id) } }
playButton.setOnClickListener(YoutubeClickListener())
}

inner class YoutubeClickListener: View.OnClickListener{
override fun onClick(v: View?) {
(itemView.context as ChatActivity).playVideo(videoId!!)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.fossasia.susi.ai.chat.listeners

import android.content.pm.ActivityInfo
import android.support.v4.app.FragmentActivity
import com.google.android.youtube.player.YouTubePlayer
import org.fossasia.susi.ai.chat.YoutubeFragment

class FullscreenListener : YouTubePlayer.OnFullscreenListener{
private var mContext: FragmentActivity? = null
private var mFragment: YoutubeFragment? = null

constructor(context : FragmentActivity?,fragment : YoutubeFragment?)
{
mContext = context
mFragment = fragment
}

override fun onFullscreen(p0: Boolean) {
if(!p0){
mContext?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}

}
22 changes: 22 additions & 0 deletions app/src/main/res/layout/fragment_youtube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
tools:context="org.fossasia.susi.ai.chat.YoutubeFragment">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/close"
android:textColor="@color/md_black_1000"
android:layout_alignParentRight="true"
android:background="@color/translucent_black"
android:id="@+id/youtube_stop_btn"
android:layout_below="@id/youtube_fragment"/>
<FrameLayout
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:layout_width="match_parent"
android:id="@+id/youtube_fragment"
android:layout_height="wrap_content"/>

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,5 @@

<!-- Shimmer background-->
<color name="shimmer_background">#dddddd</color>
<color name="translucent_black">#80000000</color>
</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
<string name="device_setting_up">Setting up your device</string>
<string name="susi_ai_hotspot" translatable="false">SUSI.AI</string>
<string name="enter_wifi_password">Enter Wi-Fi password</string>

<!-- Device Setup Screen -->
<string name="connect">1. Connect</string>
<string name="wifi">2. Wi-Fi</string>
Expand Down Expand Up @@ -599,4 +599,8 @@
<string name="alarm_query_executed">Executed planned task</string>
<string name="set_plan_action">Plan action has been set</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="close">X</string>

</resources>

0 comments on commit 1ebc893

Please sign in to comment.