使用Share时如何让VideoView继续播放? Android

How to allow VideoView to continue playing when using Share? Android

我在 Android 应用程序中有一个全屏播放的视频。我添加了一个按钮,允许用户调出 "Share" 对话框以分享到社交媒体、短信或电子邮件。但是,当用户选择 Facebook 时,视频将停止播放。是否可以在用户执行共享时强制视频在背景中继续 playing/rendering 并且声音打开?

分享代码如下:

public static void share(Activity activity, String subject, String body, String title)
    {
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);    
        activity.startActivity(Intent.createChooser(shareIntent, title));
    }

VideoView 在生命周期方面非常挑剔,它会在托管它的 activity 暂停时停止视频。显示意向选择器对话框会有效地暂停您的 activity.

首选方法(为了用户体验和处理您的问题)是使用 ShareActionProvider 允许用户 select 共享目的地。这是一个弹出列表,通常固定在操作栏上,这是一种更现代的显示这些选项的方法。作为一个额外的好处,弹出窗口不会导致您的 activity 暂停,因此视频不会停止。

如果您仍必须使用外部对话方法,则必须离开 VideoView 并使用 MediaPlayer 自行实现视频表面。这并不像听起来那么可怕,您可以从 source code 中看到大多数 VideoView 只是将其连接到 MediaPlayer 回调并尝试在创建表面时管理状态或被摧毁。