Search code examples
androidandroid-videoviewandroid-video-playerandroid-sharing

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


I have a video playing full screen in the Android application. I've added a button to allow the user to bring up the "Share" dialog to share to social media, texting, or e-mail. However, when the user selects, for example Facebook, the video will stop playback. Is it possible to force the video to continue playing/rendering in the background with sound ON while the user is performing the share?

The code to Share is below:

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));
    }

Solution

  • VideoView is extremely finicky when it comes to lifecycle, and it stops the video the moment the activity hosting it is paused. Showing an intent chooser dialog effectively pauses your activity.

    The preferred method (for both user experience and to handle your issue) would be to use ShareActionProvider to allow the user to select the sharing destination. This is a pop-up list, typically anchored to the action bar, which is a more modern method of showing these options. As an added benefit, the pop-up won't cause your activity to pause, so the video should not stop.

    If you must still use the external dialog method, you will have to move away from VideoView and implement the video surface yourself with MediaPlayer. This isn't as scary as it sounds, you can see from the source code that most of VideoView is just wiring it up to MediaPlayer callbacks and attempting to manage state when the surface is created or destroyed.