Android Question Play YouTube video in Android TV app

hatzisn

Expert
Licensed User
Longtime User
Good afternoon everybody,

I researched what is mentioned in the title several times in the past but got dissapointed. Was anyone capable of doing this? I have used the IFrame player api and created a webpage that works flawlessly in browser but the same web page with loadHTML in an android tv webview, and execute some javascript code in it, will not work at all. This is the case even if I have set the JavaScriptEnabled to true in WebView. Has anyone been able to work make the iFrame player api work with an app in android tv?

I also found this which contains a reference to a library but I do not have the knowledge to take advantage of it and create a wrapper. If anyone is able to do it then please by any means do so...

 

hatzisn

Expert
Licensed User
Longtime User
Since Google is not available (like it was ever) to ask I asked Gemini. Here is what I have got (a bit generative but accurate at the most part or Newton's first law in action ) :

 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I Asked also this:

Please show me the licensing restrictions imposed by YouTube that prohibit me displaying a video in my Android TV app.



The result:

 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Gemini 3/3

Question:

Why then does it allow to display youtube videos with the proper library in phones?


Response:

 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Gemini 4/4

Question:

What is keeping Google from creating a similar library for Android TV and modifying the terms to allow displaying in Android TV? Isn't public reproduction possible though the original YouTube app? Isn't it the same?


Response:

 
Upvote 0

nedium

Active Member
Licensed User
Longtime User

Hello, @achank
I wanted to know how to use this library since I use the example but it gives me an error. I don't know if I'm leaving something out. Could you upload an example of everything that is needed for it to work?






Lineas log:
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
Youtube view is ready
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/ContextCompat;
    at com.pierfrancescosoffritti.youtubeplayer.PlayerControlsWrapper.onStateChange(PlayerControlsWrapper.java:243)
    at com.pierfrancescosoffritti.youtubeplayer.YouTubePlayerBridge$1.run(YouTubePlayerBridge.java:35)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:230)
    at android.os.Looper.loop(Looper.java:319)
    at android.app.ActivityThread.main(ActivityThread.java:8919)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Caused by: java.lang.ClassNotFoundException: android.support.v4.content.ContextCompat
    ... 10 more
 
Last edited:
Upvote 0

achank

New Member
Hi @nedium , Sorry, I don't always log in to this account, so I just found out that there was a message from you, okay, I'll include a snippet of my project, I hope it can help, thank you B4X

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
    #BridgeLogger: True
    #AdditionalRes: D:\B4A\JWS2024\res
    #AdditionalJar: com.android.support:support-v4
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ScrollView_slide As ScrollView
    Private Panel_slide As Panel
    Dim cs As CSBuilder
    Private PanelStream As Panel
    Dim yt As YouTubePlayerView
    Dim Link_Yotube As String
    Private ButtonCheck As Button
    Private EditTextURL As EditText
    Private ButtonSend As Button
    Private EditTextID As EditText
    Dim yt_Tutor As YouTubePlayerView
    Dim Link_Tutor = "qIRYMWLeVC0" As String
    Dim Click As Boolean
    Private PanelTutorial As Panel
    Private Panel3 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("scroll_stream")
    ScrollView_slide.Panel.LoadLayout("Stream")
    ScrollView_slide.panel.Height = Panel_slide.Height
    Activity.Title = cs.Initialize.Color(Colors.White).Append("Yotube Streaming").PopAll

    yt.Initialize("yt")
    yt_Tutor.Initialize("yt_Tutor")
    PanelStream.AddView(yt,0,0,100%x,50%y)
    Panel3.AddView(yt_Tutor,0,0,100%x,50%y)
    If File.Exists(File.DirInternal, "Link.txt") Then
        Link_Yotube = File.ReadString(File.DirInternal, "Link.txt")
        EditTextID.Text = Link_Yotube
    Else
        Link_Yotube = "qIRYMWLeVC0"
    End If

End Sub


Sub LoadYoutube(Link As String)
    yt.loadVideo(Link, 0) '4YwNW8OlvqU
    yt.playVideo
End Sub

Sub LoadYoutube1(Link As String)
    yt_Tutor.loadVideo(Link, 0) '4YwNW8OlvqU
    yt_Tutor.playVideo
End Sub

Sub yt_ready
    Log("Youtube view is ready")
    LoadYoutube(Link_Yotube)
End Sub

Sub yt_enterfullscreen
    Log("FullScreen")
'    yt.loadVideo("UnmznFua_9s", 0)
'    yt.playVideo
End Sub
Sub yt_exitfullscreen
    Log("ExitFullScreen")
End Sub

Sub Activity_Resume
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    yt.release
End Sub


Private Sub ButtonCheck_Click
    Log(EditTextURL.Text)
    If EditTextURL.Text.Length > 0 Then
        Test(EditTextURL.Text)
    End If
End Sub

Private Sub EditTextURL_TextChanged (Old As String, New As String)
    
End Sub

Private Sub ButtonSend_Click
    If EditTextID.Text.Length > 0 Then
'        CallSub2(Starter, "SendMessage", "Youtube URL" & "#" & EditTextID.Text & "#")
        File.WriteString(File.DirInternal, "Link.txt", EditTextID.Text)
    End If
End Sub

Private Sub EditTextID_TextChanged (Old As String, New As String)
    If EditTextID.Text.Length > 0 Then
        LoadYoutube(EditTextID.Text)
    End If
End Sub

Sub Test(URL As String)
    Log("URL : " & URL)
    Dim m As Map = ExtractURLStruct(URL)
 
'    For Each Key As String In m.Keys
'        Log( "Key: " & Key & " : " & m.Get(Key))       
'    Next 
'    Log(" ----- Path ----- ")
    For Each Value As String In Regex.Split("/", m.Get("path"))
        If Value.Length = 0 Then Continue
        Log("Value: " & Value)
        EditTextID.Text = Value
    Next
End Sub
Public Sub ExtractURLStruct(URL As String) As Map
    Dim Pattern As String = $"^([^:\/?#]+):\/\/([^\/:]+)(?::(\d+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?"$
    Dim Matcher As Matcher = Regex.Matcher(Pattern, URL)
    Dim Result As Map : Result.Initialize
    If Matcher.Find Then
        Result.Put("url",       Matcher.Group(0))
        Result.Put("scheme",    Matcher.Group(1))
        Result.Put("domain",     Matcher.Group(2))
        Result.Put("port",         Matcher.Group(3))
        Result.Put("path",         Matcher.Group(4))
        Result.Put("parameters",Matcher.Group(5))
        Result.Put("anchor",    Matcher.Group(6))
    End If
    Return Result
End Sub

Private Sub Button_back_Click
    Activity.Finish
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Activity.Finish
    End If
    Return False
End Sub

Private Sub LabelIconYoutube_Click
    Click = Click <> True
    If Click  Then
        PanelTutorial.Visible = True
        LoadYoutube1(Link_Tutor)
    Else
        yt_Tutor.pauseVideo
        PanelTutorial.Visible = False
    End If
End Sub

Private Sub LabelTutorial_Click
    Click = Click <> True
    If Click  Then
        PanelTutorial.Visible = True
        LoadYoutube1(Link_Tutor)
    Else
        yt_Tutor.pauseVideo
        PanelTutorial.Visible = False
    End If
End Sub

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…