Android Question webview html5 getUserMedia not work

Discussion in 'Android Questions' started by kkkpe, Aug 14, 2019 at 10:32 AM.

  1. kkkpe

    kkkpe Member Licensed User

    Hi, I need to view camera images in a webview, using the html5 API getUserMedia .....
    Using the chrome browser it works, while with the webview it doesn't work. I've tried several examples posted on B4a but none works for me.
    You can take the URL test:
    https://pellegrino.vendingmanager.it/mobile/scattofoto2.php
    Thanks to those who can help me.
     
  2. JohnC

    JohnC Well-Known Member Licensed User

  3. kkkpe

    kkkpe Member Licensed User

    yes I have already tried but it does not work
     
  4. Brandsum

    Brandsum Active Member Licensed User

    Use chrome client to grant permission of user media access.
     
  5. kkkpe

    kkkpe Member Licensed User

    I don't understand .. can you give me an example?
     
  6. Brandsum

    Brandsum Active Member Licensed User

    Code:
    'grant camera and mic access permissions
    Sub WebChromeClient_PermissionRequest(PermissionRequest1 As PermissionRequest)
        PermissionRequest1.Grant(PermissionRequest1.GetResources)
    End Sub

    'check console message for more information
    Sub WebChromeClient_ConsoleMessage(ConsoleMessage1 As ConsoleMessage) As Boolean
        
    Log(ConsoleMessage1.Message)
        
    Return True
    End Sub
     
  7. kkkpe

    kkkpe Member Licensed User

    Does not work.
    I use the webviewextra2.20 library:

    Code:
    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
       Dim WebView1 As WebView
       
    Dim WebViewExtras1 As WebViewExtras
       
    Dim WebViewSetting1 As WebViewSettings
       
    Dim WebChromeClient1 As DefaultWebChromeClient  '---lib2
      
     
    Dim rp As RuntimePermissions
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        WebView1.Initialize(
    "WebView1")
        WebView1.JavaScriptEnabled=
    True
        
        WebChromeClient1.Initialize(
    "test")
        
        WebViewExtras1.Initialize(WebView1)              
    '---lib2
         WebViewExtras1.addJavascriptInterface(WebView1, "B4A"' NOT ESSENTIAL
        WebViewExtras1.SetWebChromeClient(WebChromeClient1)    '---lib2
      
     
       WebViewSetting1.setDatabaseEnabled(WebView1, 
    True)
       WebViewSetting1.setAppCacheEnabled(WebView1,
    True)
       WebViewSetting1.setDOMStorageEnabled(WebView1,
    True)
       WebViewSetting1.setGeolocationEnabled(WebView1,
    True)
       WebViewSetting1.setJavaScriptCanOpenWindowsAutomatically(WebView1,
    True)
       WebViewSetting1.setLoadWithOverviewMode(WebView1,
    True)
       WebViewSetting1.setDatabasePath(WebView1,
    "")
        WebViewSetting1.setAllowFileAccess(WebView1,
    True)
        
        
        
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
        
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        
    If Result Then
            rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
            
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
            
    If Result Then
                
                    
    Dim client As JavaObject
                    client.InitializeNewInstance(
    Application.PackageName & ".main$MyChromeClient"Null)
                    
    Dim jo As JavaObject = WebView1
                    jo.RunMethod(
    "setWebChromeClient"Array(client))
                    WebView1.LoadUrl(
    "https://pellegrino.vendingmanager.it/mobile/scattofoto2.php")
            
                
            
    End If
        
    End If

     
      
       
    Activity.AddView(WebView1, 00100%x100%y)
       
    Activity.AddMenuItem("TEST","testmenu")
     
     
    End Sub

    Sub Activity_Resume

    End Sub



    Sub Activity_Pause (UserClosed As Boolean)

    End Sub


    #if Java
    import android.webkit.*;
    public static class MyChromeClient extends WebChromeClient {
    @Override
         public void onPermissionRequest(PermissionRequest request) {
            request.grant(request.getResources());
        }
    }
    #End If
     
  8. Brandsum

    Brandsum Active Member Licensed User

    That link you are trying to load has an SSL error. Though you can ignore that as follows. Here is the working example,

    Code:
    Sub Globals
        
    Dim webview1 As WebView
        
    Private WVE As WebViewExtras
        
    Private DWVC As DefaultWebViewClient
        
    Private DWCC As DefaultWebChromeClient
        
    Private JSClient As DefaultJavascriptInterface
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        webview1.Initialize(
    "WebView1")
        
    Activity.AddView(webview1, 00100%x100%y)
        
    Activity.AddMenuItem("TEST","testmenu")
        
        JSClient.Initialize
        DWVC.Initialize(
    "WebViewClient")
        DWCC.Initialize(
    "WebChromeClient")
        WVE.Initialize(webview1)
        WVE.GetSettings.SetMediaPlaybackRequiresUserGesture(
    False)
        WVE.getsettings.SetAllowContentAccess(
    True)
        WVE.GetSettings.SetAllowFileAccess(
    True)
        WVE.GetSettings.setDOMStorageEnabled(
    True)
        WVE.GetSettings.setDatabaseEnabled(
    True)
        WVE.SetWebViewClient(DWVC)
        WVE.SetWebChromeClient(DWCC)
        WVE.AddJavascriptInterface(JSClient,
    "B4A")
        
    End Sub

    Sub Activity_Resume
        
    Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Activity_PermissionResult (Permission As String, Result As Boolean)
        
    If Result Then WVE.LoadUrl("https://pellegrino.vendingmanager.it/mobile/scattofoto2.php")
    End Sub

    Sub WebChromeClient_PermissionRequest(PermissionRequest1 As PermissionRequest)
        PermissionRequest1.Grant(PermissionRequest1.GetResources)
    End Sub

    Sub WebChromeClient_ConsoleMessage(ConsoleMessage1 As ConsoleMessage) As Boolean
        
    Log(ConsoleMessage1.Message)
        
    Return True
    End Sub

    Sub WebViewClient_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
        
    Select SslError1.GetPrimaryError
            
    Case SslError1.SSL_DATE_INVALID
                
    Log("SSL error: SSL_DATE_INVALID")
            
    Case SslError1.SSL_EXPIRED
                
    Log("SSL error: SSL_EXPIRED")
            
    Case SslError1.SSL_IDMISMATCH
                
    Log("SSL error: SSL_IDMISMATCH")
            
    Case SslError1.SSL_INVALID
                
    Log("SSL error: SSL_INVALID")
            
    Case SslError1.SSL_MAX_ERROR
                
    Log("SSL error: SSL_MAX_ERROR")
            
    Case SslError1.SSL_NOTYETVALID
                
    Log("SSL error: SSL_NOTYETVALID")
            
    Case SslError1.SSL_UNTRUSTED
                
    Log("SSL error: SSL_UNTRUSTED")
        
    End Select
        SslErrorHandler1.Proceed
    End Sub
    Add camera permission to project manifest
     
  9. kkkpe

    kkkpe Member Licensed User

    how strange .. I tried your code and added to the manifest this:
    Code:
    'End of default text.
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "5" android:targetSdkVersion="26"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    SetApplicationAttribute(android:theme, 
    "@android:style/Theme.Material")
    'End of default text.

    AddPermission(android.permission.CAMERA)
    AddPermission(android.permission.CAPTURE_AUDIO_OUTPUT) 
    ' Allows an application to capture audio output.
    AddPermission(android.permission.CAPTURE_SECURE_VIDEO_OUTPUT) ' Allows an application to capture secure video output.
    AddPermission(android.permission.CAPTURE_VIDEO_OUTPUT)
    AddPermission(android.permission.RECORD_AUDIO)
    AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
    the result you see in the attached image.
     

    Attached Files:

  10. Brandsum

    Brandsum Active Member Licensed User

    You need android.permission.CAMERA for picture and android.permission.RECORD_AUDIO for sound. That's it.
     
  11. Brandsum

    Brandsum Active Member Licensed User

    Its working for me. Check this example.
     

    Attached Files:

    DonManfred likes this.
  12. kkkpe

    kkkpe Member Licensed User

    I left only the CAMERA and AUDIO RECORD permissions. But it does not work..
     
  13. Brandsum

    Brandsum Active Member Licensed User

    Check the attached example. Also check IDE log
     
  14. kkkpe

    kkkpe Member Licensed User

    your works .. check the code ...
     
  15. kkkpe

    kkkpe Member Licensed User

    I didn't put these two events:
    Code:
    Sub WebChromeClient_PermissionRequest(PermissionRequest1 As PermissionRequest)
        PermissionRequest1.Grant(PermissionRequest1.GetResources)
    End Sub

    Sub WebChromeClient_ConsoleMessage(ConsoleMessage1 As ConsoleMessage) As Boolean
        
    Log(ConsoleMessage1.Message)
        
    Return True
    End Sub
    THANK THANK you so much for your help.
     
    moster67 and Brandsum like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice