-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
388d20b
commit 79df4c8
Showing
8 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/java/org/fossasia/susi/ai/chat/YoutubeFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.fossasia.susi.ai.chat | ||
|
||
import android.content.pm.PackageManager | ||
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/org/fossasia/susi/ai/chat/listeners/FullscreenListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters