Android Question webview html5 getUserMedia not work

JohnC

Well-Known Member
Licensed User

Brandsum

Well-Known Member
Licensed User
B4X:
'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
 

kkkpe

Member
Licensed User
Does not work.
I use the webviewextra2.20 library:

B4X:
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, 0, 0, 100%x, 100%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
 

Brandsum

Well-Known 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,

B4X:
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, 0, 0, 100%x, 100%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
 

kkkpe

Member
Licensed User
how strange .. I tried your code and added to the manifest this:
B4X:
'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.
 

Attachments

Brandsum

Well-Known Member
Licensed User
You need android.permission.CAMERA for picture and android.permission.RECORD_AUDIO for sound. That's it.
 

kkkpe

Member
Licensed User
I didn't put these two events:
B4X:
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.
 
Top