Home » Android » android – webview camera streams aren't added to video html tag on resuming activity-Exceptionshub

android – webview camera streams aren't added to video html tag on resuming activity-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have designed an app which opens the camera and add the camera stream to html video tag inside android webview
javascript code to open camera
navigator.mediaDevices.getUserMedia({video:true,audio:true}).then(function(stream){$("video")[0].srcObject = stream})
The issues with my app is that whenever the activity is resumed the camera streams is removed from video tag of webview and showing only a grey play button over the video player.

Android code is as follows

public class CameraStreamActivity {
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

   public void initializeWebView(){
     mWebView =  view.findViewById(R.id.webview);



    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

    mWebView.addJavascriptInterface(new 
    JavaScriptInterface(getActivity()),"Android");
    // mWebView.getSettings().setLoadWithOverviewMode(true);


    WebView.setWebContentsDebuggingEnabled(true);
    if(Build.VERSION.SDK_INT >= 19){
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
    }

    else 
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mWebView.setWebContentsDebuggingEnabled(true);
    }
    mWebView.setWebChromeClient(new WebChromeClient(){
             @Override
             public void onPermissionRequest(PermissionRequest request) {

                    request.grant(request.getResources());

            }
       }

   }
 }

permissions used in AndroidManifest.xml


 <uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

<uses-feature android:name="android.hardware.audio.low_latency" />
<uses-feature android:name="android.hardware.audio.pro" />
<uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" 
/>
<uses-permission android:name="android.permission.MICROPHONE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
How to&Answers: