You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Android中如何用WebView打开Blob链接并实现视频播放?

Hey there! I’ve helped many developers tackle Blob video playback in Android WebView, so let’s break this down clearly for you—this is totally achievable with the right settings and a bit of code.

1. First: Basic WebView Setup

First, you’ll need a WebView in your layout and some core configurations to enable Blob support, since Blob operations rely heavily on JavaScript and DOM features.

Layout File (e.g., activity_blob_video.xml)

Add a WebView and a fullscreen container for video playback (critical for handling fullscreen mode):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Container for fullscreen video -->
    <FrameLayout
        android:id="@+id/fullscreen_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

</RelativeLayout>
2. Key Configurations for Blob Support

In your Activity, configure the WebView to enable the features Blob video needs:

  • JavaScript: Blob URLs are managed via JavaScript, so this is non-negotiable.
  • DOM Storage: Required for Blob objects to persist and be accessed correctly.
  • Mixed Content Mode: If your Blob comes from a different scheme (e.g., HTTP vs HTTPS), this lets WebView load it safely.
  • WebChromeClient: Handles media playback events like fullscreen mode.

Full Activity Code Example

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

public class BlobVideoActivity extends AppCompatActivity {
    private WebView webView;
    private FrameLayout fullScreenContainer;
    private View customVideoView;
    private WebChromeClient.CustomViewCallback fullscreenCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blob_video);

        webView = findViewById(R.id.webview);
        fullScreenContainer = findViewById(R.id.fullscreen_container);

        WebSettings webSettings = webView.getSettings();
        // Enable JavaScript (mandatory for Blob handling)
        webSettings.setJavaScriptEnabled(true);
        // Enable DOM storage for Blob operations
        webSettings.setDomStorageEnabled(true);
        // Allow mixed content if your Blob uses a different scheme than the parent page
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        // Optional: Disable user gesture requirement if you want auto-play
        webSettings.setMediaPlaybackRequiresUserGesture(false);

        // Set WebChromeClient to handle fullscreen video playback
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {
                super.onShowCustomView(view, callback);
                if (customVideoView != null) {
                    callback.onCustomViewHidden();
                    return;
                }
                // Switch to fullscreen mode
                customVideoView = view;
                fullscreenCallback = callback;
                webView.setVisibility(View.GONE);
                fullScreenContainer.setVisibility(View.VISIBLE);
                fullScreenContainer.addView(view);
            }

            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
                if (customVideoView == null) return;
                // Exit fullscreen mode
                webView.setVisibility(View.VISIBLE);
                fullScreenContainer.setVisibility(View.GONE);
                fullScreenContainer.removeView(customVideoView);
                fullscreenCallback.onCustomViewHidden();
                customVideoView = null;
            }
        });

        // Choose one of these two options to load your Blob video:

        // Option 1: Load a remote HTML page that already includes the Blob video
        // webView.loadUrl("https://your-domain.com/page-with-blob-video.html");

        // Option 2: Load a local HTML string with your Blob URL directly
        String yourBlobUrl = "blob:https://example.com/your-unique-blob-id";
        String htmlPlayer = "<!DOCTYPE html>" +
                "<html>" +
                "<body style='margin:0; padding:0;'>" +
                "<video width='100%' height='100%' controls autoplay>" +
                "<source src='" + yourBlobUrl + "' type='video/mp4'>" +
                "</video>" +
                "</body>" +
                "</html>";
        webView.loadDataWithBaseURL(null, htmlPlayer, "text/html", "UTF-8", null);
    }

    @Override
    public void onBackPressed() {
        // Handle back button to exit fullscreen first
        if (customVideoView != null) {
            webView.getWebChromeClient().onHideCustomView();
        } else {
            super.onBackPressed();
        }
    }
}
3. Critical Checklist Before Testing
  • Permissions: Add the internet permission to your AndroidManifest.xml if your Blob comes from a remote source:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • Security Note: Only use MIXED_CONTENT_ALWAYS_ALLOW if absolutely necessary. If your app handles sensitive data, ensure all resources (including the Blob’s source) use HTTPS to avoid security risks.
  • Version Testing: Test on different Android versions—WebView behavior can vary slightly, especially on older devices (pre-Android 7.0).

内容的提问来源于stack exchange,提问作者mdtuyen

火山引擎 最新活动