You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何覆盖Android设备视频无法播放时的默认alert行为?

Got it, let's figure out how to override that annoying default video error alert on Android and redirect users back to your main menu instead. Here's how you can do it depending on your playback setup:

1. Using VideoView (Traditional Approach)

If you're using the built-in VideoView, it automatically pops up that system alert when playback fails. To take control, you need to set a custom error listener and return true to tell the system you've handled the error yourself.

Here's the code example:

VideoView videoView = findViewById(R.id.your_video_view);
videoView.setVideoPath("your_video_file_path_or_url");

// Attach custom error listener
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // Navigate to main menu
        Intent mainMenuIntent = new Intent(YourCurrentActivity.this, MainMenuActivity.class);
        startActivity(mainMenuIntent);
        finish(); // Optional: close the current playback activity if needed
        
        // Return true to prevent system from showing default alert
        return true;
    }
});

videoView.start();

The key here is returning true from onError — this tells Android you've handled the error, so it won't display that default "Sorry this video cannot be played" alert.

If you're using ExoPlayer (the go-to library for video playback in most modern Android apps), handling errors is straightforward. You just add a player listener and handle the error in the onPlayerError callback — no need to worry about system alerts because ExoPlayer doesn't trigger them by default.

Example code:

// Initialize ExoPlayer
SimpleExoPlayer player = new SimpleExoPlayer.Builder(getApplicationContext()).build();

// Add error listener
player.addListener(new Player.Listener() {
    @Override
    public void onPlayerError(PlaybackException error) {
        // Redirect to main menu
        Intent mainMenuIntent = new Intent(YourCurrentActivity.this, MainMenuActivity.class);
        startActivity(mainMenuIntent);
        finish();
    }
});

// Prepare and start playback
MediaItem mediaItem = MediaItem.fromUri("your_video_uri");
player.setMediaItem(mediaItem);
player.prepare();
player.play();
3. If You're Using WebView for Video

If your video is loaded in a WebView, the default alert might come from JavaScript. You can override onJsAlert in WebChromeClient to catch and handle it:

WebView webView = findViewById(R.id.your_web_view);
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        // Check if the alert is the video error message (adjust the string match as needed)
        if (message.equalsIgnoreCase("Sorry this video cannot be played")) {
            Intent mainMenuIntent = new Intent(YourCurrentActivity.this, MainMenuActivity.class);
            startActivity(mainMenuIntent);
            finish();
            
            // Cancel the default alert
            result.cancel();
            return true;
        }
        // Let other alerts behave normally
        return super.onJsAlert(view, url, message, result);
    }
});

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:38:05